| 1 |
use alloc::{boxed::Box, vec, vec::Vec}; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const SEEDS: &[Seed] = &[ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
Seed { first: b'x', index1: b'y', index2: b'z' }, |
| 17 |
Seed { first: b'x', index1: b'x', index2: b'z' }, |
| 18 |
Seed { first: b'x', index1: b'y', index2: b'x' }, |
| 19 |
Seed { first: b'x', index1: b'x', index2: b'x' }, |
| 20 |
Seed { first: b'x', index1: b'y', index2: b'y' }, |
| 21 |
]; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
pub(crate) struct Runner { |
| 28 |
fwd: Option< |
| 29 |
Box< |
| 30 |
dyn FnMut(&[u8], &[u8], u8, u8) -> Option<Option<usize>> + 'static, |
| 31 |
>, |
| 32 |
>, |
| 33 |
} |
| 34 |
|
| 35 |
impl Runner { |
| 36 |
|
| 37 |
pub(crate) fn new() -> Runner { |
| 38 |
Runner { fwd: None } |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
pub(crate) fn run(self) { |
| 50 |
if let Some(mut fwd) = self.fwd { |
| 51 |
for seed in SEEDS.iter() { |
| 52 |
for t in seed.generate() { |
| 53 |
match fwd(&t.haystack, &t.needle, t.index1, t.index2) { |
| 54 |
None => continue, |
| 55 |
Some(result) => { |
| 56 |
assert_eq!( |
| 57 |
t.fwd, result, |
| 58 |
"FORWARD, needle: {:?}, haystack: {:?}, \ |
| 59 |
index1: {:?}, index2: {:?}", |
| 60 |
t.needle, t.haystack, t.index1, t.index2, |
| 61 |
) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
pub(crate) fn fwd( |
| 78 |
mut self, |
| 79 |
search: impl FnMut(&[u8], &[u8], u8, u8) -> Option<Option<usize>> + 'static, |
| 80 |
) -> Runner { |
| 81 |
self.fwd = Some(Box::new(search)); |
| 82 |
self |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
struct Test { |
| 90 |
haystack: Vec<u8>, |
| 91 |
needle: Vec<u8>, |
| 92 |
index1: u8, |
| 93 |
index2: u8, |
| 94 |
fwd: Option<usize>, |
| 95 |
} |
| 96 |
|
| 97 |
impl Test { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
fn new( |
| 105 |
seed: Seed, |
| 106 |
index1: usize, |
| 107 |
index2: usize, |
| 108 |
haystack_len: usize, |
| 109 |
needle_len: usize, |
| 110 |
fwd: Option<usize>, |
| 111 |
) -> Option<Test> { |
| 112 |
let mut index1: u8 = index1.try_into().unwrap(); |
| 113 |
let mut index2: u8 = index2.try_into().unwrap(); |
| 114 |
|
| 115 |
|
| 116 |
let mut haystack = vec![b'@'; haystack_len]; |
| 117 |
let mut needle = vec![b'#'; needle_len]; |
| 118 |
needle[0] = seed.first; |
| 119 |
needle[index1 as usize] = seed.index1; |
| 120 |
needle[index2 as usize] = seed.index2; |
| 121 |
|
| 122 |
|
| 123 |
if let Some(i) = fwd { |
| 124 |
haystack[i..i + needle.len()].copy_from_slice(&needle); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if let Some(i) = crate::memchr(seed.index1, &needle) { |
| 131 |
index1 = u8::try_from(i).unwrap(); |
| 132 |
} |
| 133 |
if let Some(i) = crate::memchr(seed.index2, &needle) { |
| 134 |
index2 = u8::try_from(i).unwrap(); |
| 135 |
} |
| 136 |
Some(Test { haystack, needle, index1, index2, fwd }) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
#[derive(Clone, Copy)] |
| 142 |
struct Seed { |
| 143 |
first: u8, |
| 144 |
index1: u8, |
| 145 |
index2: u8, |
| 146 |
} |
| 147 |
|
| 148 |
impl Seed { |
| 149 |
const NEEDLE_LENGTH_LIMIT: usize = { |
| 150 |
#[cfg(not(miri))] |
| 151 |
{ |
| 152 |
33 |
| 153 |
} |
| 154 |
#[cfg(miri)] |
| 155 |
{ |
| 156 |
5 |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
const HAYSTACK_LENGTH_LIMIT: usize = { |
| 161 |
#[cfg(not(miri))] |
| 162 |
{ |
| 163 |
65 |
| 164 |
} |
| 165 |
#[cfg(miri)] |
| 166 |
{ |
| 167 |
8 |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
fn generate(self) -> impl Iterator<Item = Test> { |
| 173 |
let len_start = 2; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
(len_start..=Seed::NEEDLE_LENGTH_LIMIT).flat_map(move |needle_len| { |
| 182 |
let index_start = len_start - 1; |
| 183 |
(index_start..needle_len).flat_map(move |index1| { |
| 184 |
(index1..needle_len).flat_map(move |index2| { |
| 185 |
(needle_len..=Seed::HAYSTACK_LENGTH_LIMIT).flat_map( |
| 186 |
move |haystack_len| { |
| 187 |
Test::new( |
| 188 |
self, |
| 189 |
index1, |
| 190 |
index2, |
| 191 |
haystack_len, |
| 192 |
needle_len, |
| 193 |
None, |
| 194 |
) |
| 195 |
.into_iter() |
| 196 |
.chain( |
| 197 |
(0..=(haystack_len - needle_len)).flat_map( |
| 198 |
move |output| { |
| 199 |
Test::new( |
| 200 |
self, |
| 201 |
index1, |
| 202 |
index2, |
| 203 |
haystack_len, |
| 204 |
needle_len, |
| 205 |
Some(output), |
| 206 |
) |
| 207 |
}, |
| 208 |
), |
| 209 |
) |
| 210 |
}, |
| 211 |
) |
| 212 |
}) |
| 213 |
}) |
| 214 |
}) |
| 215 |
} |
| 216 |
} |