]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
1 | use rayon::prelude::*; |
2 | ||
3 | const OCTILLION: u128 = 1_000_000_000_000_000_000_000_000_000; | |
4 | ||
5 | /// Produce a parallel iterator for 0u128..10²⁷ | |
6 | fn octillion() -> rayon::range::Iter<u128> { | |
7 | (0..OCTILLION).into_par_iter() | |
8 | } | |
9 | ||
e74abb32 XL |
10 | /// Produce a parallel iterator for 0u128..=10²⁷ |
11 | fn octillion_inclusive() -> rayon::range_inclusive::Iter<u128> { | |
12 | (0..=OCTILLION).into_par_iter() | |
13 | } | |
14 | ||
532ac7d7 XL |
15 | /// Produce a parallel iterator for 0u128..10²⁷ using `flat_map` |
16 | fn octillion_flat() -> impl ParallelIterator<Item = u128> { | |
17 | (0u32..1_000_000_000) | |
18 | .into_par_iter() | |
19 | .with_max_len(1_000) | |
e74abb32 | 20 | .map(|i| u64::from(i) * 1_000_000_000) |
532ac7d7 XL |
21 | .flat_map(|i| { |
22 | (0u32..1_000_000_000) | |
23 | .into_par_iter() | |
24 | .with_max_len(1_000) | |
e74abb32 | 25 | .map(move |j| i + u64::from(j)) |
532ac7d7 | 26 | }) |
e74abb32 | 27 | .map(|i| u128::from(i) * 1_000_000_000) |
532ac7d7 XL |
28 | .flat_map(|i| { |
29 | (0u32..1_000_000_000) | |
30 | .into_par_iter() | |
31 | .with_max_len(1_000) | |
e74abb32 | 32 | .map(move |j| i + u128::from(j)) |
532ac7d7 XL |
33 | }) |
34 | } | |
35 | ||
36 | #[test] | |
37 | fn find_first_octillion() { | |
38 | let x = octillion().find_first(|_| true); | |
39 | assert_eq!(x, Some(0)); | |
40 | } | |
41 | ||
e74abb32 XL |
42 | #[test] |
43 | fn find_first_octillion_inclusive() { | |
44 | let x = octillion_inclusive().find_first(|_| true); | |
45 | assert_eq!(x, Some(0)); | |
46 | } | |
47 | ||
532ac7d7 XL |
48 | #[test] |
49 | fn find_first_octillion_flat() { | |
50 | let x = octillion_flat().find_first(|_| true); | |
51 | assert_eq!(x, Some(0)); | |
52 | } | |
53 | ||
e74abb32 | 54 | fn two_threads<F: Send + FnOnce() -> R, R: Send>(f: F) -> R { |
532ac7d7 XL |
55 | // FIXME: If we don't use at least two threads, then we end up walking |
56 | // through the entire iterator sequentially, without the benefit of any | |
57 | // short-circuiting. We probably don't want testing to wait that long. ;) | |
532ac7d7 XL |
58 | let builder = rayon::ThreadPoolBuilder::new().num_threads(2); |
59 | let pool = builder.build().unwrap(); | |
60 | ||
e74abb32 XL |
61 | pool.install(f) |
62 | } | |
63 | ||
64 | #[test] | |
65 | fn find_last_octillion() { | |
66 | // It would be nice if `find_last` could prioritize the later splits, | |
67 | // basically flipping the `join` args, without needing indexed `rev`. | |
68 | // (or could we have an unindexed `rev`?) | |
69 | let x = two_threads(|| octillion().find_last(|_| true)); | |
532ac7d7 XL |
70 | assert_eq!(x, Some(OCTILLION - 1)); |
71 | } | |
72 | ||
73 | #[test] | |
e74abb32 XL |
74 | fn find_last_octillion_inclusive() { |
75 | let x = two_threads(|| octillion_inclusive().find_last(|_| true)); | |
76 | assert_eq!(x, Some(OCTILLION)); | |
77 | } | |
532ac7d7 | 78 | |
e74abb32 XL |
79 | #[test] |
80 | fn find_last_octillion_flat() { | |
81 | let x = two_threads(|| octillion_flat().find_last(|_| true)); | |
532ac7d7 XL |
82 | assert_eq!(x, Some(OCTILLION - 1)); |
83 | } | |
e74abb32 XL |
84 | |
85 | #[test] | |
86 | fn find_any_octillion() { | |
87 | let x = two_threads(|| octillion().find_any(|x| *x > OCTILLION / 2)); | |
88 | assert!(x.is_some()); | |
89 | } | |
90 | ||
91 | #[test] | |
92 | fn find_any_octillion_flat() { | |
93 | let x = two_threads(|| octillion_flat().find_any(|x| *x > OCTILLION / 2)); | |
94 | assert!(x.is_some()); | |
95 | } | |
96 | ||
97 | #[test] | |
98 | fn filter_find_any_octillion() { | |
99 | let x = two_threads(|| { | |
100 | octillion() | |
101 | .filter(|x| *x > OCTILLION / 2) | |
102 | .find_any(|_| true) | |
103 | }); | |
104 | assert!(x.is_some()); | |
105 | } | |
106 | ||
107 | #[test] | |
108 | fn filter_find_any_octillion_flat() { | |
109 | let x = two_threads(|| { | |
110 | octillion_flat() | |
111 | .filter(|x| *x > OCTILLION / 2) | |
112 | .find_any(|_| true) | |
113 | }); | |
114 | assert!(x.is_some()); | |
115 | } | |
116 | ||
117 | #[test] | |
118 | fn fold_find_any_octillion_flat() { | |
119 | let x = two_threads(|| octillion_flat().fold(|| (), |_, _| ()).find_any(|_| true)); | |
120 | assert!(x.is_some()); | |
121 | } |