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