|
| 1 | +use std::str::pattern::*; |
| 2 | + |
| 3 | +// This macro makes it easier to write |
| 4 | +// tests that do a series of iterations |
| 5 | +macro_rules! search_asserts { |
| 6 | + ($haystack:expr, $needle:expr, $testname:expr, [$($func:ident),*], $result:expr) => { |
| 7 | + let mut searcher = $needle.into_searcher($haystack); |
| 8 | + let arr = [$( Step::from(searcher.$func()) ),+]; |
| 9 | + assert_eq!(&arr[..], &$result, $testname); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +/// Combined enum for the results of next() and next_match()/next_reject() |
| 14 | +#[derive(Debug, PartialEq, Eq)] |
| 15 | +enum Step { |
| 16 | + // variant names purposely chosen to |
| 17 | + // be the same length for easy alignment |
| 18 | + Matches(usize, usize), |
| 19 | + Rejects(usize, usize), |
| 20 | + InRange(usize, usize), |
| 21 | + Done |
| 22 | +} |
| 23 | + |
| 24 | +use Step::*; |
| 25 | + |
| 26 | +impl From<SearchStep> for Step { |
| 27 | + fn from(x: SearchStep) -> Self { |
| 28 | + match x { |
| 29 | + SearchStep::Match(a, b) => Matches(a, b), |
| 30 | + SearchStep::Reject(a, b) => Rejects(a, b), |
| 31 | + SearchStep::Done => Done |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl From<Option<(usize, usize)>> for Step { |
| 37 | + fn from(x: Option<(usize, usize)>) -> Self { |
| 38 | + match x { |
| 39 | + Some((a, b)) => InRange(a, b), |
| 40 | + None => Done |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_simple_iteration() { |
| 47 | + search_asserts! ("abcdeabcd", 'a', "forward iteration for ASCII string", |
| 48 | + // a b c d e a b c d EOF |
| 49 | + [next, next, next, next, next, next, next, next, next, next], |
| 50 | + [Matches(0, 1), Rejects(1, 2), Rejects(2, 3), Rejects(3, 4), Rejects(4, 5), Matches(5, 6), Rejects(6, 7), Rejects(7, 8), Rejects(8, 9), Done] |
| 51 | + ); |
| 52 | + |
| 53 | + search_asserts! ("abcdeabcd", 'a', "reverse iteration for ASCII string", |
| 54 | + // d c b a e d c b a EOF |
| 55 | + [next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back], |
| 56 | + [Rejects(8, 9), Rejects(7, 8), Rejects(6, 7), Matches(5, 6), Rejects(4, 5), Rejects(3, 4), Rejects(2, 3), Rejects(1, 2), Matches(0, 1), Done] |
| 57 | + ); |
| 58 | + |
| 59 | + search_asserts! ("我爱我的猫", '我', "forward iteration for Chinese string", |
| 60 | + // 我 愛 我 的 貓 EOF |
| 61 | + [next, next, next, next, next, next], |
| 62 | + [Matches(0, 3), Rejects(3, 6), Matches(6, 9), Rejects(9, 12), Rejects(12, 15), Done] |
| 63 | + ); |
| 64 | + |
| 65 | + search_asserts! ("我的猫说meow", 'm', "forward iteration for mixed string", |
| 66 | + // 我 的 猫 说 m e o w EOF |
| 67 | + [next, next, next, next, next, next, next, next, next], |
| 68 | + [Rejects(0, 3), Rejects(3, 6), Rejects(6, 9), Rejects(9, 12), Matches(12, 13), Rejects(13, 14), Rejects(14, 15), Rejects(15, 16), Done] |
| 69 | + ); |
| 70 | + |
| 71 | + search_asserts! ("我的猫说meow", '猫', "reverse iteration for mixed string", |
| 72 | + // w o e m 说 猫 的 我 EOF |
| 73 | + [next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back], |
| 74 | + [Rejects(15, 16), Rejects(14, 15), Rejects(13, 14), Rejects(12, 13), Rejects(9, 12), Matches(6, 9), Rejects(3, 6), Rejects(0, 3), Done] |
| 75 | + ); |
| 76 | +} |
0 commit comments