Debian Codegraph @

debian/rust-vendor/rayon-core/src/join/test.rs

raw
1 //! Tests for the join code.
2
3 use super::*;
4 use crate::ThreadPoolBuilder;
5 use rand::distr::StandardUniform;
6 use rand::{Rng, SeedableRng};
7 use rand_xorshift::XorShiftRng;
8
9 fn quick_sort<T: PartialOrd + Send>(v: &mut [T]) {
10 if v.len() <= 1 {
11 return;
12 }
13
14 let mid = partition(v);
15 let (lo, hi) = v.split_at_mut(mid);
16 join(|| quick_sort(lo), || quick_sort(hi));
17 }
18
19 fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
20 let pivot = v.len() - 1;
21 let mut i = 0;
22 for j in 0..pivot {
23 if v[j] <= v[pivot] {
24 v.swap(i, j);
25 i += 1;
26 }
27 }
28 v.swap(i, pivot);
29 i
30 }
31
32 fn seeded_rng() -> XorShiftRng {
33 let mut seed = <XorShiftRng as SeedableRng>::Seed::default();
34 (0..).zip(seed.as_mut()).for_each(|(i, x)| *x = i);
35 XorShiftRng::from_seed(seed)
36 }
37
38 #[test]
39 fn sort() {
40 let rng = seeded_rng();
41 let mut data: Vec<u32> = rng.sample_iter(&StandardUniform).take(6 * 1024).collect();
42 let mut sorted_data = data.clone();
43 sorted_data.sort();
44 quick_sort(&mut data);
45 assert_eq!(data, sorted_data);
46 }
47
48 #[test]
49 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
50 fn sort_in_pool() {
51 let rng = seeded_rng();
52 let mut data: Vec<u32> = rng.sample_iter(&StandardUniform).take(12 * 1024).collect();
53
54 let pool = ThreadPoolBuilder::new().build().unwrap();
55 let mut sorted_data = data.clone();
56 sorted_data.sort();
57 pool.install(|| quick_sort(&mut data));
58 assert_eq!(data, sorted_data);
59 }
60
61 #[test]
62 #[should_panic(expected = "Hello, world!")]
63 fn panic_propagate_a() {
64 join(|| panic!("Hello, world!"), || ());
65 }
66
67 #[test]
68 #[should_panic(expected = "Hello, world!")]
69 fn panic_propagate_b() {
70 join(|| (), || panic!("Hello, world!"));
71 }
72
73 #[test]
74 #[should_panic(expected = "Hello, world!")]
75 fn panic_propagate_both() {
76 join(|| panic!("Hello, world!"), || panic!("Goodbye, world!"));
77 }
78
79 #[test]
80 #[cfg_attr(not(panic = "unwind"), ignore)]
81 fn panic_b_still_executes() {
82 let mut x = false;
83 match unwind::halt_unwinding(|| join(|| panic!("Hello, world!"), || x = true)) {
84 Ok(_) => panic!("failed to propagate panic from closure A,"),
85 Err(_) => assert!(x, "closure b failed to execute"),
86 }
87 }
88
89 #[test]
90 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
91 fn join_context_both() {
92 // If we're not in a pool, both should be marked stolen as they're injected.
93 let (a_migrated, b_migrated) = join_context(|a| a.migrated(), |b| b.migrated());
94 assert!(a_migrated);
95 assert!(b_migrated);
96 }
97
98 #[test]
99 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
100 fn join_context_neither() {
101 // If we're already in a 1-thread pool, neither job should be stolen.
102 let pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
103 let (a_migrated, b_migrated) =
104 pool.install(|| join_context(|a| a.migrated(), |b| b.migrated()));
105 assert!(!a_migrated);
106 assert!(!b_migrated);
107 }
108
109 #[test]
110 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
111 fn join_context_second() {
112 use std::sync::Barrier;
113
114 // If we're already in a 2-thread pool, the second job should be stolen.
115 let barrier = Barrier::new(2);
116 let pool = ThreadPoolBuilder::new().num_threads(2).build().unwrap();
117 let (a_migrated, b_migrated) = pool.install(|| {
118 join_context(
119 |a| {
120 barrier.wait();
121 a.migrated()
122 },
123 |b| {
124 barrier.wait();
125 b.migrated()
126 },
127 )
128 });
129 assert!(!a_migrated);
130 assert!(b_migrated);
131 }
132
133 #[test]
134 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
135 fn join_counter_overflow() {
136 const MAX: u32 = 500_000;
137
138 let mut i = 0;
139 let mut j = 0;
140 let pool = ThreadPoolBuilder::new().num_threads(2).build().unwrap();
141
142 // Hammer on join a bunch of times -- used to hit overflow debug-assertions
143 // in JEC on 32-bit targets: https://github.com/rayon-rs/rayon/issues/797
144 for _ in 0..MAX {
145 pool.join(|| i += 1, || j += 1);
146 }
147
148 assert_eq!(i, MAX);
149 assert_eq!(j, MAX);
150 }