diff options
| author | Stjepan Glavina <stjepang@gmail.com> | 2017-03-21 02:38:03 +0100 |
|---|---|---|
| committer | Stjepan Glavina <stjepang@gmail.com> | 2017-03-21 20:46:20 +0100 |
| commit | a718051f63308638ecf40a7570dbd18f4fc99703 (patch) | |
| tree | 4a0c488b1fa8fcef586b4fc81459675435134e60 /src/libcoretest | |
| parent | a18b2aa641da7c000fea9e9ac62d2da89fa034ad (diff) | |
Unit test heapsort
Diffstat (limited to 'src/libcoretest')
| -rw-r--r-- | src/libcoretest/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcoretest/slice.rs | 44 |
2 files changed, 32 insertions, 13 deletions
diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 4f3b775ceb2..d92c378160d 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -26,6 +26,7 @@ #![feature(raw)] #![feature(sip_hash_13)] #![feature(slice_patterns)] +#![feature(sort_internals)] #![feature(sort_unstable)] #![feature(step_by)] #![feature(test)] diff --git a/src/libcoretest/slice.rs b/src/libcoretest/slice.rs index b51bae4db22..89bd3be0851 100644 --- a/src/libcoretest/slice.rs +++ b/src/libcoretest/slice.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use core::slice::heapsort; use core::result::Result::{Ok, Err}; use rand::{Rng, XorShiftRng}; @@ -226,26 +227,43 @@ fn get_unchecked_mut_range() { #[test] fn sort_unstable() { let mut v = [0; 600]; - let mut v1 = [0; 600]; + let mut tmp = [0; 600]; let mut rng = XorShiftRng::new_unseeded(); for len in (2..25).chain(500..510) { - for &modulus in &[10, 1000] { + let v = &mut v[0..len]; + let tmp = &mut tmp[0..len]; + + for &modulus in &[5, 10, 100, 1000] { for _ in 0..100 { for i in 0..len { - let num = rng.gen::<i32>() % modulus; - v[i] = num; - v1[i] = num; + v[i] = rng.gen::<i32>() % modulus; } - v.sort_unstable(); - assert!(v.windows(2).all(|w| w[0] <= w[1])); - - v1.sort_unstable_by(|a, b| a.cmp(b)); - assert!(v1.windows(2).all(|w| w[0] <= w[1])); - - v1.sort_unstable_by(|a, b| b.cmp(a)); - assert!(v1.windows(2).all(|w| w[0] >= w[1])); + // Sort in default order. + tmp.copy_from_slice(v); + tmp.sort_unstable(); + assert!(tmp.windows(2).all(|w| w[0] <= w[1])); + + // Sort in ascending order. + tmp.copy_from_slice(v); + tmp.sort_unstable_by(|a, b| a.cmp(b)); + assert!(tmp.windows(2).all(|w| w[0] <= w[1])); + + // Sort in descending order. + tmp.copy_from_slice(v); + tmp.sort_unstable_by(|a, b| b.cmp(a)); + assert!(tmp.windows(2).all(|w| w[0] >= w[1])); + + // Test heapsort using `<` operator. + tmp.copy_from_slice(v); + heapsort(tmp, |a, b| a < b); + assert!(tmp.windows(2).all(|w| w[0] <= w[1])); + + // Test heapsort using `>` operator. + tmp.copy_from_slice(v); + heapsort(tmp, |a, b| a > b); + assert!(tmp.windows(2).all(|w| w[0] >= w[1])); } } } |
