about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorStjepan Glavina <stjepang@gmail.com>2017-03-31 16:59:01 +0200
committerStjepan Glavina <stjepang@gmail.com>2017-03-31 17:00:24 +0200
commit0e2d3d41bb42abe1c40585d2ed06aea2840e664f (patch)
treeaf4dfaafb7d896e28ef0772d41f26b9676704fa9 /src/libcoretest
parentc6df67afca788453a3c494899fbf5295992bcfba (diff)
downloadrust-0e2d3d41bb42abe1c40585d2ed06aea2840e664f.tar.gz
rust-0e2d3d41bb42abe1c40585d2ed06aea2840e664f.zip
Test sort algorithms using a random cmp function
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/slice.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/libcoretest/slice.rs b/src/libcoretest/slice.rs
index 89bd3be0851..ec38345030f 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::cmp::Ordering::{Equal, Greater, Less};
 use core::slice::heapsort;
 use core::result::Result::{Ok, Err};
 use rand::{Rng, XorShiftRng};
@@ -268,6 +269,17 @@ fn sort_unstable() {
         }
     }
 
+    // Sort using a completely random comparison function.
+    // This will reorder the elements *somehow*, but won't panic.
+    for i in 0..v.len() {
+        v[i] = i as i32;
+    }
+    v.sort_unstable_by(|_, _| *rng.choose(&[Less, Equal, Greater]).unwrap());
+    v.sort_unstable();
+    for i in 0..v.len() {
+        assert_eq!(v[i], i as i32);
+    }
+
     // Should not panic.
     [0i32; 0].sort_unstable();
     [(); 10].sort_unstable();