]> git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon/tests/run-pass/scope_join.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / vendor / rustc-rayon / tests / run-pass / scope_join.rs
1 extern crate rayon;
2
3 /// Test that one can emulate join with `scope`:
4 fn pseudo_join<F, G>(f: F, g: G)
5 where F: FnOnce() + Send,
6 G: FnOnce() + Send,
7 {
8 rayon::scope(|s| {
9 s.spawn(|_| g());
10 f();
11 });
12 }
13
14 fn quick_sort<T:PartialOrd+Send>(v: &mut [T]) {
15 if v.len() <= 1 {
16 return;
17 }
18
19 let mid = partition(v);
20 let (lo, hi) = v.split_at_mut(mid);
21 pseudo_join(|| quick_sort(lo), || quick_sort(hi));
22 }
23
24 fn partition<T:PartialOrd+Send>(v: &mut [T]) -> usize {
25 let pivot = v.len() - 1;
26 let mut i = 0;
27 for j in 0..pivot {
28 if v[j] <= v[pivot] {
29 v.swap(i, j);
30 i += 1;
31 }
32 }
33 v.swap(i, pivot);
34 i
35 }
36
37 pub fn is_sorted<T: Send + Ord>(v: &[T]) -> bool {
38 (1..v.len()).all(|i| v[i-1] <= v[i])
39 }
40
41 fn main() {
42 let mut v: Vec<i32> = (0 .. 256).rev().collect();
43 quick_sort(&mut v);
44 assert!(is_sorted(&v));
45 }