Skip to content

Commit ffe6514

Browse files
committed
Merge 'simulator: add counterexample minimization' from Alperen Keleş
This PR introduces counterexample minimization(shrinking) in the simulator. It will require changes to the current structure in various places, so I've opened it as a draft PR for now, in order to not overwhelm the reviewers all at once. - [x] Turn interactions plans into sequences of properties instead of sequences of interactions, adding a semantic layer. - [x] Add assumptions to the properties, rendering a property invalid if its assumptions are not valid. - [x] Record the failure point in a failing assertion, as shrinking by definition works when the same assertion fails for a smaller input. - [ ] Add shrinking at three levels, - [x] top level(removing whole properties), - [x] property level(removing interactions within properties), - [ ] interaction level(shrinking values in interactions to smaller ones). - [ ] Add [marauders](https://github.com/alpaylan/marauders) as a dev dependency, inject custom mutations to a testing branch for evaluating the simulator performance. - [ ] Integrate the simulator evaluation with the CI. Closes #623
2 parents d0b5f50 + ea6ad8d commit ffe6514

File tree

13 files changed

+1480
-432
lines changed

13 files changed

+1480
-432
lines changed

simulator/generation/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,41 @@ use anarchist_readable_name_generator_lib::readable_name_custom;
44
use rand::{distributions::uniform::SampleUniform, Rng};
55

66
pub mod plan;
7+
pub mod property;
78
pub mod query;
89
pub mod table;
910

11+
/// Arbitrary trait for generating random values
12+
/// An implementation of arbitrary is assumed to be a uniform sampling of
13+
/// the possible values of the type, with a bias towards smaller values for
14+
/// practicality.
1015
pub trait Arbitrary {
1116
fn arbitrary<R: Rng>(rng: &mut R) -> Self;
1217
}
1318

19+
/// ArbitraryFrom trait for generating random values from a given value
20+
/// ArbitraryFrom allows for constructing relations, where the generated
21+
/// value is dependent on the given value. These relations could be constraints
22+
/// such as generating an integer within an interval, or a value that fits in a table,
23+
/// or a predicate satisfying a given table row.
1424
pub trait ArbitraryFrom<T> {
15-
fn arbitrary_from<R: Rng>(rng: &mut R, t: &T) -> Self;
25+
fn arbitrary_from<R: Rng>(rng: &mut R, t: T) -> Self;
1626
}
1727

28+
/// Frequency is a helper function for composing different generators with different frequency
29+
/// of occurences.
30+
/// The type signature for the `N` parameter is a bit complex, but it
31+
/// roughly corresponds to a type that can be summed, compared, subtracted and sampled, which are
32+
/// the operations we require for the implementation.
33+
// todo: switch to a simpler type signature that can accomodate all integer and float types, which
34+
// should be enough for our purposes.
1835
pub(crate) fn frequency<
1936
'a,
2037
T,
2138
R: rand::Rng,
2239
N: Sum + PartialOrd + Copy + Default + SampleUniform + SubAssign,
2340
>(
24-
choices: Vec<(N, Box<dyn FnOnce(&mut R) -> T + 'a>)>,
41+
choices: Vec<(N, Box<dyn Fn(&mut R) -> T + 'a>)>,
2542
rng: &mut R,
2643
) -> T {
2744
let total = choices.iter().map(|(weight, _)| *weight).sum::<N>();
@@ -37,6 +54,7 @@ pub(crate) fn frequency<
3754
unreachable!()
3855
}
3956

57+
/// one_of is a helper function for composing different generators with equal probability of occurence.
4058
pub(crate) fn one_of<'a, T, R: rand::Rng>(
4159
choices: Vec<Box<dyn Fn(&mut R) -> T + 'a>>,
4260
rng: &mut R,
@@ -45,15 +63,20 @@ pub(crate) fn one_of<'a, T, R: rand::Rng>(
4563
choices[index](rng)
4664
}
4765

66+
/// pick is a helper function for uniformly picking a random element from a slice
4867
pub(crate) fn pick<'a, T, R: rand::Rng>(choices: &'a [T], rng: &mut R) -> &'a T {
4968
let index = rng.gen_range(0..choices.len());
5069
&choices[index]
5170
}
5271

72+
/// pick_index is typically used for picking an index from a slice to later refer to the element
73+
/// at that index.
5374
pub(crate) fn pick_index<R: rand::Rng>(choices: usize, rng: &mut R) -> usize {
5475
rng.gen_range(0..choices)
5576
}
5677

78+
/// gen_random_text uses `anarchist_readable_name_generator_lib` to generate random
79+
/// readable names for tables, columns, text values etc.
5780
fn gen_random_text<T: Rng>(rng: &mut T) -> String {
5881
let big_text = rng.gen_ratio(1, 1000);
5982
if big_text {

0 commit comments

Comments
 (0)