Add random mutation tests

This commit is contained in:
Max Brunsfeld 2019-01-25 12:05:21 -08:00
parent e305012b31
commit 233d616ebf
12 changed files with 443 additions and 127 deletions

41
cli/src/tests/random.rs Normal file
View file

@ -0,0 +1,41 @@
use rand::distributions::Alphanumeric;
use rand::prelude::{Rng, SeedableRng, SmallRng};
const OPERATORS: &[char] = &[
'+', '-', '<', '>', '(', ')', '*', '/', '&', '|', '!', ',', '.',
];
pub struct Rand(SmallRng);
impl Rand {
pub fn new(seed: usize) -> Self {
Rand(SmallRng::seed_from_u64(seed as u64))
}
pub fn unsigned(&mut self, max: usize) -> usize {
self.0.gen_range(0, max + 1)
}
pub fn words(&mut self, max_count: usize) -> Vec<u8> {
let mut result = Vec::new();
let word_count = self.unsigned(max_count);
for i in 0..word_count {
if i > 0 {
if self.unsigned(5) == 0 {
result.push('\n' as u8);
} else {
result.push(' ' as u8);
}
}
if self.unsigned(3) == 0 {
let index = self.unsigned(OPERATORS.len() - 1);
result.push(OPERATORS[index] as u8);
} else {
for _ in 0..self.unsigned(8) {
result.push(self.0.sample(Alphanumeric) as u8);
}
}
}
result
}
}