Merge pull request #49 from japaric/edge

qc: bias towards generation of edge cases
This commit is contained in:
Jorge Aparicio 2016-08-21 11:03:44 -05:00 committed by GitHub
commit c427112561
1 changed files with 28 additions and 7 deletions

View File

@ -20,7 +20,18 @@ macro_rules! arbitrary {
fn arbitrary<G>(g: &mut G) -> $TY fn arbitrary<G>(g: &mut G) -> $TY
where G: Gen where G: Gen
{ {
$TY(g.gen()) // NOTE Generate edge cases with a 10% chance
let t = if g.gen_weighted_bool(10) {
*g.choose(&[
$ty::min_value(),
0,
$ty::max_value(),
]).unwrap()
} else {
g.gen()
};
$TY(t)
} }
fn shrink(&self) -> Box<Iterator<Item=$TY>> { fn shrink(&self) -> Box<Iterator<Item=$TY>> {
@ -76,13 +87,23 @@ macro_rules! arbitrary_large {
fn arbitrary<G>(g: &mut G) -> $TY fn arbitrary<G>(g: &mut G) -> $TY
where G: Gen where G: Gen
{ {
if g.gen() { // NOTE Generate edge cases with a 10% chance
$TY($ty::from_parts(g.gen(), g.gen())) let t = if g.gen_weighted_bool(10) {
} else if g.gen() { *g.choose(&[
$TY($ty::from_parts(0, g.gen())) $ty::min_value(),
0,
$ty::max_value(),
]).unwrap()
} else { } else {
$TY($ty::from_parts(g.gen(), 0)) match g.gen_range(0, 3) {
0 => $ty::from_parts(g.gen(), g.gen()),
1 => $ty::from_parts(0, g.gen()),
2 => $ty::from_parts(g.gen(), 0),
_ => unreachable!(),
} }
};
$TY(t)
} }
fn shrink(&self) -> Box<Iterator<Item=$TY>> { fn shrink(&self) -> Box<Iterator<Item=$TY>> {