1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
use crate::DefaultMutator; use crate::Mutator; #[derive(Default)] pub struct BoolMutator { rng: fastrand::Rng, } impl DefaultMutator for bool { type Mutator = BoolMutator; #[no_coverage] fn default_mutator() -> Self::Mutator { <_>::default() } } pub enum ArbitraryStep { Never = 0, Once = 1, Twice = 2, } impl Default for ArbitraryStep { #[no_coverage] fn default() -> Self { Self::Never } } const BOOL_COMPLEXITY: f64 = 1.0; const INITIAL_MUTATION_STEP: bool = false; impl Mutator<bool> for BoolMutator { type Cache = (); type MutationStep = bool; type ArbitraryStep = ArbitraryStep; type UnmutateToken = bool; #[no_coverage] fn default_arbitrary_step(&self) -> Self::ArbitraryStep { <_>::default() } #[no_coverage] fn max_complexity(&self) -> f64 { BOOL_COMPLEXITY } #[no_coverage] fn min_complexity(&self) -> f64 { BOOL_COMPLEXITY } #[no_coverage] fn validate_value(&self, _value: &bool) -> Option<(Self::Cache, Self::MutationStep)> { Some(((), INITIAL_MUTATION_STEP)) } #[no_coverage] fn complexity(&self, _value: &bool, _cache: &Self::Cache) -> f64 { BOOL_COMPLEXITY } #[no_coverage] fn ordered_arbitrary(&self, step: &mut Self::ArbitraryStep, max_cplx: f64) -> Option<(bool, f64)> { if max_cplx < self.min_complexity() { return None; } match step { ArbitraryStep::Never => { *step = ArbitraryStep::Once; Some((false, BOOL_COMPLEXITY)) } ArbitraryStep::Once => { *step = ArbitraryStep::Twice; Some((true, BOOL_COMPLEXITY)) } ArbitraryStep::Twice => None, } } #[no_coverage] fn random_arbitrary(&self, _max_cplx: f64) -> (bool, f64) { (self.rng.bool(), BOOL_COMPLEXITY) } #[no_coverage] fn ordered_mutate( &self, value: &mut bool, _cache: &mut Self::Cache, step: &mut Self::MutationStep, max_cplx: f64, ) -> Option<(Self::UnmutateToken, f64)> { if max_cplx < self.min_complexity() { return None; } if !*step { *step = !*step; Some((std::mem::replace(value, !*value), BOOL_COMPLEXITY)) } else { None } } #[no_coverage] fn random_mutate(&self, value: &mut bool, _cache: &mut Self::Cache, _max_cplx: f64) -> (Self::UnmutateToken, f64) { (std::mem::replace(value, !*value), BOOL_COMPLEXITY) } #[no_coverage] fn unmutate(&self, value: &mut bool, _cache: &mut Self::Cache, t: Self::UnmutateToken) { *value = t; } }