|
| 1 | +// When testing functions, QuickCheck (QC) uses small values for integer (`u*`/`i*`) arguments |
| 2 | +// (~ `[-100, 100]`), but these values don't stress all the code paths in our intrinsics. Here we |
| 3 | +// create newtypes over the primitive integer types with the goal of having full control over the |
| 4 | +// random values that will be used to test our intrinsics. |
| 5 | + |
| 6 | +use std::boxed::Box; |
| 7 | +use std::fmt; |
| 8 | + |
| 9 | +use quickcheck::{Arbitrary, Gen}; |
| 10 | + |
| 11 | +use LargeInt; |
| 12 | + |
| 13 | +// Generates values in the full range of the integer type |
| 14 | +macro_rules! arbitrary { |
| 15 | + ($TY:ident : $ty:ident) => { |
| 16 | + #[derive(Clone, Copy)] |
| 17 | + pub struct $TY(pub $ty); |
| 18 | + |
| 19 | + impl Arbitrary for $TY { |
| 20 | + fn arbitrary<G>(g: &mut G) -> $TY |
| 21 | + where G: Gen |
| 22 | + { |
| 23 | + $TY(g.gen()) |
| 24 | + } |
| 25 | + |
| 26 | + fn shrink(&self) -> Box<Iterator<Item=$TY>> { |
| 27 | + struct Shrinker { |
| 28 | + x: $ty, |
| 29 | + } |
| 30 | + |
| 31 | + impl Iterator for Shrinker { |
| 32 | + type Item = $TY; |
| 33 | + |
| 34 | + fn next(&mut self) -> Option<$TY> { |
| 35 | + self.x /= 2; |
| 36 | + if self.x == 0 { |
| 37 | + None |
| 38 | + } else { |
| 39 | + Some($TY(self.x)) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if self.0 == 0 { |
| 45 | + ::quickcheck::empty_shrinker() |
| 46 | + } else { |
| 47 | + Box::new(Shrinker { x: self.0 }) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + impl fmt::Debug for $TY { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 54 | + fmt::Debug::fmt(&self.0, f) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +arbitrary!(I32: i32); |
| 61 | +arbitrary!(U32: u32); |
| 62 | + |
| 63 | +// These integers are "too large". If we generate e.g. `u64` values in the full range then there's |
| 64 | +// only `1 / 2^32` chance of seeing a value smaller than `2^32` (i.e. whose higher "word" (32-bits) |
| 65 | +// is `0`)! But this is an important group of values to tests because we have special code paths for |
| 66 | +// them. Instead we'll generate e.g. `u64` integers this way: uniformly pick between (a) setting the |
| 67 | +// low word to 0 and generating a random high word, (b) vice versa: high word to 0 and random low |
| 68 | +// word or (c) generate both words randomly. This let's cover better the code paths in our |
| 69 | +// intrinsics. |
| 70 | +macro_rules! arbitrary_large { |
| 71 | + ($TY:ident : $ty:ident) => { |
| 72 | + #[derive(Clone, Copy)] |
| 73 | + pub struct $TY(pub $ty); |
| 74 | + |
| 75 | + impl Arbitrary for $TY { |
| 76 | + fn arbitrary<G>(g: &mut G) -> $TY |
| 77 | + where G: Gen |
| 78 | + { |
| 79 | + if g.gen() { |
| 80 | + $TY($ty::from_parts(g.gen(), g.gen())) |
| 81 | + } else if g.gen() { |
| 82 | + $TY($ty::from_parts(0, g.gen())) |
| 83 | + } else { |
| 84 | + $TY($ty::from_parts(g.gen(), 0)) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn shrink(&self) -> Box<Iterator<Item=$TY>> { |
| 89 | + struct Shrinker { |
| 90 | + x: $ty, |
| 91 | + } |
| 92 | + |
| 93 | + impl Iterator for Shrinker { |
| 94 | + type Item = $TY; |
| 95 | + |
| 96 | + fn next(&mut self) -> Option<$TY> { |
| 97 | + self.x /= 2; |
| 98 | + if self.x == 0 { |
| 99 | + None |
| 100 | + } else { |
| 101 | + Some($TY(self.x)) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if self.0 == 0 { |
| 107 | + ::quickcheck::empty_shrinker() |
| 108 | + } else { |
| 109 | + Box::new(Shrinker { x: self.0 }) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + impl fmt::Debug for $TY { |
| 115 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 116 | + fmt::Debug::fmt(&self.0, f) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +arbitrary_large!(I64: i64); |
| 123 | +arbitrary_large!(U64: u64); |
0 commit comments