Debian Codegraph @

debian/missing-sources/rayon/src/iter/find_first_last/test.rs

raw
1 use super::*;
2
3 #[test]
4 fn same_range_first_consumers_return_correct_answer() {
5 let find_op = |x: &i32| x % 2 == 0;
6 let first_found = AtomicUsize::new(usize::MAX);
7 let far_right_consumer = FindConsumer::new(&find_op, MatchPosition::Leftmost, &first_found);
8
9 // We save a consumer that will be far to the right of the main consumer (and therefore not
10 // sharing an index range with that consumer) for fullness testing
11 let consumer = far_right_consumer.split_off_left();
12
13 // split until we have an indivisible range
14 for _ in 0..usize::BITS {
15 consumer.split_off_left();
16 }
17
18 let reducer = consumer.to_reducer();
19 // the left and right folders should now have the same range, having
20 // exhausted the resolution of usize
21 let left_folder = consumer.split_off_left().into_folder();
22 let right_folder = consumer.into_folder();
23
24 let left_folder = left_folder.consume(0).consume(1);
25 assert_eq!(left_folder.boundary, right_folder.boundary);
26 // expect not full even though a better match has been found because the
27 // ranges are the same
28 assert!(!right_folder.full());
29 assert!(far_right_consumer.full());
30 let right_folder = right_folder.consume(2).consume(3);
31 assert_eq!(
32 reducer.reduce(left_folder.complete(), right_folder.complete()),
33 Some(0)
34 );
35 }
36
37 #[test]
38 fn same_range_last_consumers_return_correct_answer() {
39 let find_op = |x: &i32| x % 2 == 0;
40 let last_found = AtomicUsize::new(0);
41 let consumer = FindConsumer::new(&find_op, MatchPosition::Rightmost, &last_found);
42
43 // We save a consumer that will be far to the left of the main consumer (and therefore not
44 // sharing an index range with that consumer) for fullness testing
45 let far_left_consumer = consumer.split_off_left();
46
47 // split until we have an indivisible range
48 for _ in 0..usize::BITS {
49 consumer.split_off_left();
50 }
51
52 let reducer = consumer.to_reducer();
53 // due to the exact calculation in split_off_left, the very last consumer has a
54 // range of width 2, so we use the second-to-last consumer instead to get
55 // the same boundary on both folders
56 let consumer = consumer.split_off_left();
57 let left_folder = consumer.split_off_left().into_folder();
58 let right_folder = consumer.into_folder();
59 let right_folder = right_folder.consume(2).consume(3);
60 assert_eq!(left_folder.boundary, right_folder.boundary);
61 // expect not full even though a better match has been found because the
62 // ranges are the same
63 assert!(!left_folder.full());
64 assert!(far_left_consumer.full());
65 let left_folder = left_folder.consume(0).consume(1);
66 assert_eq!(
67 reducer.reduce(left_folder.complete(), right_folder.complete()),
68 Some(2)
69 );
70 }
71
72 // These tests requires that a folder be assigned to an iterator with more than
73 // one element. We can't necessarily determine when that will happen for a given
74 // input to find_first/find_last, so we test the folder directly here instead.
75 #[test]
76 fn find_first_folder_does_not_clobber_first_found() {
77 let best_found = AtomicUsize::new(usize::MAX);
78 let f = FindFolder {
79 find_op: &(|&_: &i32| -> bool { true }),
80 boundary: 0,
81 match_position: MatchPosition::Leftmost,
82 best_found: &best_found,
83 item: None,
84 };
85 let f = f.consume(0_i32).consume(1_i32).consume(2_i32);
86 assert!(f.full());
87 assert_eq!(f.complete(), Some(0_i32));
88 }
89
90 #[test]
91 fn find_last_folder_yields_last_match() {
92 let best_found = AtomicUsize::new(0);
93 let f = FindFolder {
94 find_op: &(|&_: &i32| -> bool { true }),
95 boundary: 0,
96 match_position: MatchPosition::Rightmost,
97 best_found: &best_found,
98 item: None,
99 };
100 let f = f.consume(0_i32).consume(1_i32).consume(2_i32);
101 assert_eq!(f.complete(), Some(2_i32));
102 }