From 003c346439ae59b76111e28be810fadf76d72769 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 13 Nov 2020 02:55:08 +0100 Subject: [PATCH] reproducible rand for testcases --- src/mutators/scheduled.rs | 5 +++-- src/utils.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index f98be1fb66..e211dd578f 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -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()); diff --git a/src/utils.rs b/src/utils.rs index 51bed50a56..0b6d3eac91 100644 --- a/src/utils.rs +++ b/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> { + Rc::new(RefCell::new(Self::new())) + } +} + /// A very basic HasRand pub struct DefaultHasRand where