generate arbitrary instances for all uint types
This commit is contained in:
parent
a243481934
commit
b1e5ea93ad
22
src/uint.rs
22
src/uint.rs
|
@ -2533,17 +2533,25 @@ mod tests {
|
|||
|
||||
mod laws {
|
||||
use quickcheck::{Arbitrary, Gen, TestResult};
|
||||
use uint::U128;
|
||||
use uint::{U128, U256, U512};
|
||||
|
||||
impl Arbitrary for U128 {
|
||||
fn arbitrary<G: Gen>(g: &mut G) -> U128 {
|
||||
let mut res = [0u8; 16];
|
||||
let size = g.gen_range(0, 16);
|
||||
g.fill_bytes(&mut res[..size]);
|
||||
U128::from(res)
|
||||
macro_rules! uint_arbitrary {
|
||||
($uint:ty, $n_bytes:tt) => {
|
||||
impl Arbitrary for $uint {
|
||||
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
||||
let mut res = [0u8; $n_bytes];
|
||||
let size = g.gen_range(0, $n_bytes);
|
||||
g.fill_bytes(&mut res[..size]);
|
||||
Self::from(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint_arbitrary!(U128, 16);
|
||||
uint_arbitrary!(U256, 32);
|
||||
uint_arbitrary!(U512, 64);
|
||||
|
||||
quickcheck! {
|
||||
fn associative_add(x: U128, y: U128, z: U128) -> TestResult {
|
||||
if x.overflowing_add(y).1 || y.overflowing_add(z).1 || (x + y).overflowing_add(z).1 {
|
||||
|
|
Loading…
Reference in New Issue