Add some property testing, and fix a bug it found.

This commit is contained in:
2018-11-06 21:53:47 -08:00
parent ccde640f99
commit de5ff87f9e
6 changed files with 75 additions and 25 deletions

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
authors = ["awick"]
[dependencies]
quickcheck = "0.7.1"
quickcheck = "^0.7.2"

View File

@@ -219,6 +219,9 @@ rsaSizes = [512,1024,2048,3072,4096,8192,15360]
baseRequirements :: [Requirement]
baseRequirements = concatMap (\ x -> [Req x RSA]) rsaSizes
++ [Req 192 Add, Req 256 Add, Req 384 Add] -- used for testing
++ [Req 192 Mul, Req 384 Mul] -- used for testing
++ [Req 448 (Convert 512)] -- used for testing
requirements :: [Requirement]
requirements = go baseRequirements

View File

@@ -1,5 +1,3 @@
#![recursion_limit="1024"]
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
@@ -8,3 +6,45 @@ pub mod signed;
pub mod unsigned;
#[cfg(test)]
pub mod testing;
#[cfg(test)]
mod arithmetic {
use super::signed::*;
use super::unsigned::*;
quickcheck! {
fn commutivity_signed_add(x: I576, y: I576) -> bool {
(&x + &y) == (&y + &x)
}
fn commutivity_unsigned_add(x: U576, y: U576) -> bool {
(&x + &y) == (&y + &x)
}
fn commutivity_unsigned_mul(x: U192, y: U192) -> bool {
(&x * &y) == (&y * &x)
}
fn associativity_unsigned_add(x: U192, y: U192, z: U192) -> bool {
(U256::from(&x) + (&y + &z)) == ((&x + &y) + U256::from(&z))
}
fn associativity_unsigned_mul(x: U192, y: U192, z: U192) -> bool {
(U384::from(&x) * (&y * &z)) == ((&x * &y) * U384::from(&z))
}
fn identity_signed_add(x: I576) -> bool {
(&x + I576::zero()) == I640::from(&x)
}
fn identity_unsigned_add(x: U576) -> bool {
(&x + U576::zero()) == U640::from(&x)
}
fn identity_unsigned_mul(x: U192) -> bool {
(&x * U192::from(1u64)) == U384::from(&x)
}
fn additive_inverse(x: I576) -> bool {
(&x + x.negate()) == I640::zero()
}
fn distribution(x: U192, y: U192, z: U192) -> bool {
(U256::from(&x) * (&y + &z)) == U512::from((&x * &y) + (&x * &z))
}
}
}

View File

@@ -60,16 +60,19 @@ macro_rules! add_impls
value: &self.value + &rhs.value
}
} else {
if self.value > rhs.value {
match self.value.cmp(&rhs.value) {
Ordering::Greater =>
$bigger {
negative: self.negative,
value: $ubigger::from(&self.value - &rhs.value)
}
} else {
},
Ordering::Less =>
$bigger {
negative: rhs.negative,
value: $ubigger::from(&rhs.value - &self.value)
}
},
Ordering::Equal =>
$bigger::zero()
}
}
}

View File

@@ -85,16 +85,18 @@ macro_rules! signed_impls {
}
}
// #[cfg(test)]
// impl Arbitrary for $sname {
// fn arbitrary<G: Gen>(g: &mut G) -> $name {
// let neg = g.gen::<bool>();
// let val = g.gen::<$name>();
// $sname{ negative: neg, value: val }
// }
// }
#[cfg(test)]
impl Arbitrary for $sname {
fn arbitrary<G>(g: &mut G) -> $sname
where G: Gen
{
let neg = bool::arbitrary(g);
let val = $name::arbitrary(g);
let neg2 = if val.is_zero() { false } else { neg };
$sname{ negative: neg2, value: val }
}
}
impl fmt::Debug for $sname {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.negative {

View File

@@ -26,6 +26,8 @@ mod shift;
#[macro_use]
mod subtraction;
#[cfg(test)]
use quickcheck::{Arbitrary,Gen};
use std::cmp::{Ord,Ordering,PartialOrd};
use std::fmt;
use std::ops::{Add,AddAssign};