reproducible rand for testcases

This commit is contained in:
Dominik Maier 2020-11-13 02:55:08 +01:00
parent 1533c87775
commit 003c346439
2 changed files with 32 additions and 2 deletions

View File

@ -327,12 +327,12 @@ mod tests {
use crate::corpus::{Corpus, InMemoryCorpus}; use crate::corpus::{Corpus, InMemoryCorpus};
use crate::inputs::{BytesInput, HasBytesVec}; use crate::inputs::{BytesInput, HasBytesVec};
use crate::mutators::scheduled::mutation_splice; use crate::mutators::scheduled::mutation_splice;
use crate::utils::{DefaultHasRand, DefaultRand}; use crate::utils::{DefaultHasRand, Rand, XKCDRand};
#[test] #[test]
fn test_mut_splice() { fn test_mut_splice() {
// With the current impl, seed of 1 will result in a split at pos 2. // 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 has_rand = DefaultHasRand::new(&rand);
let mut corpus = InMemoryCorpus::new(&rand); let mut corpus = InMemoryCorpus::new(&rand);
corpus.add_input(BytesInput::new(vec!['a' as u8, 'b' as u8, 'c' as u8])); 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 testcase = testcase_rr.borrow_mut();
let mut input = testcase.load_input().expect("No input in testcase").clone(); 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(); mutation_splice(&mut has_rand, &mut corpus, &mut input).unwrap();
println!("{:?}", input.bytes()); println!("{:?}", input.bytes());

View File

@ -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 /// A very basic HasRand
pub struct DefaultHasRand<R> pub struct DefaultHasRand<R>
where where