reproducible rand for testcases
This commit is contained in:
parent
1533c87775
commit
003c346439
@ -327,12 +327,12 @@ mod tests {
|
||||
use crate::corpus::{Corpus, InMemoryCorpus};
|
||||
use crate::inputs::{BytesInput, HasBytesVec};
|
||||
use crate::mutators::scheduled::mutation_splice;
|
||||
use crate::utils::{DefaultHasRand, DefaultRand};
|
||||
use crate::utils::{DefaultHasRand, Rand, XKCDRand};
|
||||
|
||||
#[test]
|
||||
fn test_mut_splice() {
|
||||
// With the current impl, seed of 1 will result in a split at pos 2.
|
||||
let rand = &DefaultRand::new_rr(1);
|
||||
let rand = &XKCDRand::new_rr();
|
||||
let mut has_rand = DefaultHasRand::new(&rand);
|
||||
let mut corpus = InMemoryCorpus::new(&rand);
|
||||
corpus.add_input(BytesInput::new(vec!['a' as u8, 'b' as u8, 'c' as u8]));
|
||||
@ -342,6 +342,7 @@ mod tests {
|
||||
let mut testcase = testcase_rr.borrow_mut();
|
||||
let mut input = testcase.load_input().expect("No input in testcase").clone();
|
||||
|
||||
rand.borrow_mut().set_seed(5);
|
||||
mutation_splice(&mut has_rand, &mut corpus, &mut input).unwrap();
|
||||
|
||||
println!("{:?}", input.bytes());
|
||||
|
29
src/utils.rs
29
src/utils.rs
@ -137,6 +137,35 @@ impl Xoshiro256StarRand {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
/// fake rand, for testing purposes
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
pub struct XKCDRand {
|
||||
val: u64,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Rand for XKCDRand {
|
||||
fn set_seed(&mut self, val: u64) {
|
||||
self.val = val
|
||||
}
|
||||
|
||||
fn next(&mut self) -> u64 {
|
||||
self.val
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl XKCDRand {
|
||||
pub fn new() -> Self {
|
||||
Self { val: 4 }
|
||||
}
|
||||
|
||||
pub fn new_rr() -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self::new()))
|
||||
}
|
||||
}
|
||||
|
||||
/// A very basic HasRand
|
||||
pub struct DefaultHasRand<R>
|
||||
where
|
||||
|
Loading…
x
Reference in New Issue
Block a user