Use u8 for fuzzer choices instead of u32

Value is bounded between 0 and 255.
This commit is contained in:
KtorZ 2024-03-03 19:38:49 +01:00
parent 30841fe000
commit a7b9d4bb22
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
1 changed files with 18 additions and 12 deletions

View File

@ -319,11 +319,11 @@ impl PropertyTest {
pub enum Prng { pub enum Prng {
Seeded { Seeded {
seed: u32, seed: u32,
choices: Vec<u32>, choices: Vec<u8>,
uplc: PlutusData, uplc: PlutusData,
}, },
Replayed { Replayed {
choices: Vec<u32>, choices: Vec<u8>,
uplc: PlutusData, uplc: PlutusData,
}, },
} }
@ -346,7 +346,7 @@ impl Prng {
} }
} }
pub fn choices(&self) -> Vec<u32> { pub fn choices(&self) -> Vec<u8> {
match self { match self {
Prng::Seeded { choices, .. } => { Prng::Seeded { choices, .. } => {
let mut choices = choices.to_vec(); let mut choices = choices.to_vec();
@ -373,7 +373,7 @@ impl Prng {
} }
/// Construct a Pseudo-random number generator from a pre-defined list of choices. /// Construct a Pseudo-random number generator from a pre-defined list of choices.
pub fn from_choices(choices: &[u32]) -> Prng { pub fn from_choices(choices: &[u8]) -> Prng {
Prng::Replayed { Prng::Replayed {
choices: choices.to_vec(), choices: choices.to_vec(),
uplc: Data::constr( uplc: Data::constr(
@ -414,7 +414,7 @@ impl Prng {
if let [seed, PlutusData::Array(choices)] = &fields[..] { if let [seed, PlutusData::Array(choices)] = &fields[..] {
return Prng::Seeded { return Prng::Seeded {
seed: as_u32(seed), seed: as_u32(seed),
choices: choices.iter().map(as_u32).collect(), choices: choices.iter().map(as_u8).collect(),
uplc: cst.clone(), uplc: cst.clone(),
}; };
} }
@ -423,7 +423,7 @@ impl Prng {
if *tag == 121 + Prng::REPLAYED { if *tag == 121 + Prng::REPLAYED {
if let [PlutusData::Array(choices)] = &fields[..] { if let [PlutusData::Array(choices)] = &fields[..] {
return Prng::Replayed { return Prng::Replayed {
choices: choices.iter().map(as_u32).collect(), choices: choices.iter().map(as_u8).collect(),
uplc: cst.clone(), uplc: cst.clone(),
}; };
} }
@ -437,7 +437,13 @@ impl Prng {
if let PlutusData::BigInt(BigInt::Int(Int(i))) = field { if let PlutusData::BigInt(BigInt::Int(Int(i))) = field {
return u32::try_from(*i).expect("Choice doesn't fit in u32?"); return u32::try_from(*i).expect("Choice doesn't fit in u32?");
} }
unreachable!("Malformed choice's value: {field:#?}")
}
fn as_u8(field: &PlutusData) -> u8 {
if let PlutusData::BigInt(BigInt::Int(Int(i))) = field {
return u8::try_from(*i).expect("Choice doesn't fit in u8?");
}
unreachable!("Malformed choice's value: {field:#?}") unreachable!("Malformed choice's value: {field:#?}")
} }
@ -474,12 +480,12 @@ impl Prng {
#[derive(Debug)] #[derive(Debug)]
pub struct Counterexample<'a> { pub struct Counterexample<'a> {
pub value: PlutusData, pub value: PlutusData,
pub choices: Vec<u32>, pub choices: Vec<u8>,
pub property: &'a PropertyTest, pub property: &'a PropertyTest,
} }
impl<'a> Counterexample<'a> { impl<'a> Counterexample<'a> {
fn consider(&mut self, choices: &[u32]) -> bool { fn consider(&mut self, choices: &[u8]) -> bool {
if choices == self.choices { if choices == self.choices {
return true; return true;
} }
@ -505,7 +511,7 @@ impl<'a> Counterexample<'a> {
// If these new choices are shorter or smaller, then we pick them // If these new choices are shorter or smaller, then we pick them
// as new choices and inform that it's been an improvement. // as new choices and inform that it's been an improvement.
if choices.len() <= self.choices.len() || choices < &self.choices { if choices.len() <= self.choices.len() || choices < &self.choices[..] {
self.value = value; self.value = value;
self.choices = choices.to_vec(); self.choices = choices.to_vec();
true true
@ -635,9 +641,9 @@ impl<'a> Counterexample<'a> {
/// Try to replace a value with a smaller value by doing a binary search between /// Try to replace a value with a smaller value by doing a binary search between
/// two extremes. This converges relatively fast in order to shrink down values. /// two extremes. This converges relatively fast in order to shrink down values.
/// fast. /// fast.
fn binary_search_replace<F>(&mut self, lo: u32, hi: u32, f: F) -> u32 fn binary_search_replace<F>(&mut self, lo: u8, hi: u8, f: F) -> u8
where where
F: Fn(u32) -> Vec<(usize, u32)>, F: Fn(u8) -> Vec<(usize, u8)>,
{ {
if self.replace(f(lo)) { if self.replace(f(lo)) {
return lo; return lo;
@ -660,7 +666,7 @@ impl<'a> Counterexample<'a> {
// Replace values in the choices vector, based on the index-value list provided // Replace values in the choices vector, based on the index-value list provided
// and consider the resulting choices. // and consider the resulting choices.
fn replace(&mut self, ivs: Vec<(usize, u32)>) -> bool { fn replace(&mut self, ivs: Vec<(usize, u8)>) -> bool {
let mut choices = self.choices.clone(); let mut choices = self.choices.clone();
for (i, v) in ivs { for (i, v) in ivs {