Skip to content

Commit

Permalink
Unit test heapsort
Browse files Browse the repository at this point in the history
  • Loading branch information
Stjepan Glavina committed Mar 21, 2017
1 parent a18b2aa commit a718051
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/libcore/slice/mod.rs
Expand Up @@ -2253,6 +2253,15 @@ pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] {
mem::transmute(Repr { data: p, len: len })
}

// This function is public only because there is no other way to unit test heapsort.
#[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "0")]
#[doc(hidden)]
pub fn heapsort<T, F>(v: &mut [T], mut is_less: F)
where F: FnMut(&T, &T) -> bool
{
sort::heapsort(v, &mut is_less);
}

//
// Comparison traits
//
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/slice/sort.rs
Expand Up @@ -57,7 +57,7 @@ fn shift_head<T, F>(v: &mut [T], is_less: &mut F)
ptr::copy_nonoverlapping(v.get_unchecked(1), v.get_unchecked_mut(0), 1);

for i in 2..len {
if !is_less(&v[i], &tmp.value) {
if !is_less(v.get_unchecked(i), &tmp.value) {
break;
}

Expand Down Expand Up @@ -159,7 +159,7 @@ fn insertion_sort<T, F>(v: &mut [T], is_less: &mut F)

/// Sorts `v` using heapsort, which guarantees `O(n log n)` worst-case.
#[cold]
fn heapsort<T, F>(v: &mut [T], is_less: &mut F)
pub fn heapsort<T, F>(v: &mut [T], is_less: &mut F)
where F: FnMut(&T, &T) -> bool
{
// This binary heap respects the invariant `parent >= child`.
Expand Down
1 change: 1 addition & 0 deletions src/libcoretest/lib.rs
Expand Up @@ -26,6 +26,7 @@
#![feature(raw)]
#![feature(sip_hash_13)]
#![feature(slice_patterns)]
#![feature(sort_internals)]
#![feature(sort_unstable)]
#![feature(step_by)]
#![feature(test)]
Expand Down
44 changes: 31 additions & 13 deletions src/libcoretest/slice.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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]));
}
}
}
Expand Down

0 comments on commit a718051

Please sign in to comment.