-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move find_first/last tests to a separate file
- Loading branch information
Showing
2 changed files
with
72 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::sync::atomic::AtomicUsize; | ||
use super::*; | ||
|
||
#[test] | ||
fn same_range_first_consumers_return_correct_answer() { | ||
let find_op = |x: &i32| x % 2 == 0; | ||
let first_found = AtomicUsize::new(usize::max_value()); | ||
let far_right_consumer = FindConsumer::new(&find_op, MatchPosition::Leftmost, &first_found); | ||
|
||
// We save a consumer that will be far to the right of the main consumer (and therefore not | ||
// sharing an index range with that consumer) for fullness testing | ||
let consumer = far_right_consumer.split_off(); | ||
|
||
// split until we have an indivisible range | ||
let bits_in_usize = usize::min_value().count_zeros(); | ||
for i in 0..bits_in_usize { | ||
consumer.split_off(); | ||
} | ||
|
||
let reducer = consumer.to_reducer(); | ||
// the left and right folders should now have the same range, having | ||
// exhausted the resolution of usize | ||
let left_folder = consumer.split_off().into_folder(); | ||
let right_folder = consumer.into_folder(); | ||
|
||
let left_folder = left_folder.consume(0).consume(1); | ||
assert_eq!(left_folder.boundary, right_folder.boundary); | ||
// expect not full even though a better match has been found because the | ||
// ranges are the same | ||
assert!(!right_folder.full()); | ||
assert!(far_right_consumer.full()); | ||
let right_folder = right_folder.consume(2).consume(3); | ||
assert_eq!(reducer.reduce(left_folder.complete(), right_folder.complete()), | ||
Some(0)); | ||
} | ||
|
||
#[test] | ||
fn same_range_last_consumers_return_correct_answer() { | ||
let find_op = |x: &i32| x % 2 == 0; | ||
let last_found = AtomicUsize::new(0); | ||
let consumer = FindConsumer::new(&find_op, MatchPosition::Rightmost, &last_found); | ||
|
||
// We save a consumer that will be far to the left of the main consumer (and therefore not | ||
// sharing an index range with that consumer) for fullness testing | ||
let far_left_consumer = consumer.split_off(); | ||
|
||
// split until we have an indivisible range | ||
let bits_in_usize = usize::min_value().count_zeros(); | ||
for i in 0..bits_in_usize { | ||
consumer.split_off(); | ||
} | ||
|
||
let reducer = consumer.to_reducer(); | ||
// due to the exact calculation in split_off, the very last consumer has a | ||
// range of width 2, so we use the second-to-last consumer instead to get | ||
// the same boundary on both folders | ||
let consumer = consumer.split_off(); | ||
let left_folder = consumer.split_off().into_folder(); | ||
let right_folder = consumer.into_folder(); | ||
let right_folder = right_folder.consume(2).consume(3); | ||
assert_eq!(left_folder.boundary, right_folder.boundary); | ||
// expect not full even though a better match has been found because the | ||
// ranges are the same | ||
assert!(!left_folder.full()); | ||
assert!(far_left_consumer.full()); | ||
let left_folder = left_folder.consume(0).consume(1); | ||
assert_eq!(reducer.reduce(left_folder.complete(), right_folder.complete()), | ||
Some(2)); | ||
} |