]> git.proxmox.com Git - rustc.git/blame - vendor/rustc-rayon/tests/octillion.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / vendor / rustc-rayon / tests / octillion.rs
CommitLineData
532ac7d7
XL
1use rayon::prelude::*;
2
3const OCTILLION: u128 = 1_000_000_000_000_000_000_000_000_000;
4
5/// Produce a parallel iterator for 0u128..10²⁷
6fn octillion() -> rayon::range::Iter<u128> {
7 (0..OCTILLION).into_par_iter()
8}
9
e74abb32
XL
10/// Produce a parallel iterator for 0u128..=10²⁷
11fn 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`
16fn 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]
37fn find_first_octillion() {
38 let x = octillion().find_first(|_| true);
39 assert_eq!(x, Some(0));
40}
41
e74abb32
XL
42#[test]
43fn find_first_octillion_inclusive() {
44 let x = octillion_inclusive().find_first(|_| true);
45 assert_eq!(x, Some(0));
46}
47
532ac7d7
XL
48#[test]
49fn find_first_octillion_flat() {
50 let x = octillion_flat().find_first(|_| true);
51 assert_eq!(x, Some(0));
52}
53
e74abb32 54fn 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]
65fn 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
74fn 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]
80fn 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]
86fn find_any_octillion() {
87 let x = two_threads(|| octillion().find_any(|x| *x > OCTILLION / 2));
88 assert!(x.is_some());
89}
90
91#[test]
92fn 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]
98fn 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]
108fn 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]
118fn fold_find_any_octillion_flat() {
119 let x = two_threads(|| octillion_flat().fold(|| (), |_, _| ()).find_any(|_| true));
120 assert!(x.is_some());
121}