Factor out the testing code, so we can use it later.

This commit is contained in:
2018-04-30 13:05:10 -07:00
parent 153d88237f
commit 2eacea8ff9
3 changed files with 60 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
use cryptonum::unsigned::BarrettUCN; use cryptonum::unsigned::BarrettUCN;
use testing::{make_unsigned,run_test}; use testing::{make_signed,make_unsigned,run_test};
#[test] #[test]
fn unsigned_sum_test() fn unsigned_sum_test()
@@ -9,14 +9,16 @@ fn unsigned_sum_test()
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();
assert_eq!(x + y, *z); let res = x + y;
assert_eq!(res, *z);
}); });
} }
#[test] #[test]
fn signed_sum_test() fn signed_sum_test()
{ {
run_test("tests/math/signed_add.tests", 3, |case| { run_test("tests/math/signed_add.tests", 3, |bcase| {
let case = make_signed(bcase);
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();
@@ -39,7 +41,8 @@ fn unsigned_sub_test()
#[test] #[test]
fn signed_sub_test() fn signed_sub_test()
{ {
run_test("tests/math/signed_sub.tests", 3, |case| { run_test("tests/math/signed_sub.tests", 3, |bcase| {
let case = make_signed(bcase);
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();
@@ -62,7 +65,8 @@ fn unsigned_mul_test()
#[test] #[test]
fn signed_mul_test() fn signed_mul_test()
{ {
run_test("tests/math/signed_mul.tests", 3, |case| { run_test("tests/math/signed_mul.tests", 3, |bcase| {
let case = make_signed(bcase);
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();
@@ -85,7 +89,8 @@ fn unsigned_div_test()
#[test] #[test]
fn signed_div_test() fn signed_div_test()
{ {
run_test("tests/math/signed_div.tests", 3, |case| { run_test("tests/math/signed_div.tests", 3, |bcase| {
let case = make_signed(bcase);
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();
@@ -108,7 +113,8 @@ fn unsigned_mod_test()
#[test] #[test]
fn signed_mod_test() fn signed_mod_test()
{ {
run_test("tests/math/signed_mod.tests", 3, |case| { run_test("tests/math/signed_mod.tests", 3, |bcase| {
let case = make_signed(bcase);
let x = case.get("x").unwrap(); let x = case.get("x").unwrap();
let y = case.get("y").unwrap(); let y = case.get("y").unwrap();
let z = case.get("z").unwrap(); let z = case.get("z").unwrap();

View File

@@ -7,7 +7,7 @@ use std::ops::*;
/// In case you were wondering, it stands for "Signed Crypto Num". /// In case you were wondering, it stands for "Signed Crypto Num".
#[derive(Clone,Debug,PartialEq,Eq)] #[derive(Clone,Debug,PartialEq,Eq)]
pub struct SCN { pub struct SCN {
negative: bool, pub(crate) negative: bool,
pub(crate) value: UCN pub(crate) value: UCN
} }

View File

@@ -4,45 +4,78 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::str::Lines; use std::str::Lines;
fn next_value_set(line: &str) -> (String, SCN) fn next_value_set(line: &str) -> (String, bool, Vec<u8>)
{ {
assert!(line.is_ascii()); assert!(line.is_ascii());
assert_eq!(": ", &line[1..3]); let mut items = line.split(": ");
let key = String::from(&line[0..1]); let key = items.next().unwrap();
let val = SCN::from_str(&line[3..]); let valbits = items.next().unwrap();
(key, val) let neg = valbits.contains('-');
let valbitsnoneg = valbits.trim_left_matches("-");
let mut nibble_iter = valbitsnoneg.chars().rev();
let mut val = Vec::new();
while let Some(c1) = nibble_iter.next() {
match nibble_iter.next() {
None => {
val.push( c1.to_digit(16).unwrap() as u8 );
}
Some(c2) => {
let b1 = c1.to_digit(16).unwrap() as u8;
let b2 = c2.to_digit(16).unwrap() as u8;
val.push( (b2 << 4) | b1 );
}
}
}
val.reverse();
(key.to_string(), neg, val)
} }
fn next_test_case(contents: &mut Lines, lines: usize) -> fn next_test_case(contents: &mut Lines, lines: usize) ->
Option<HashMap<String,SCN>> Option<HashMap<String,(bool,Vec<u8>)>>
{ {
let mut res = HashMap::new(); let mut res = HashMap::new();
let mut count = 0; let mut count = 0;
while count < lines { while count < lines {
let line = contents.next()?; let line = contents.next()?;
let (key, val) = next_value_set(line); let (key, neg, val) = next_value_set(line);
res.insert(key, val); res.insert(key, (neg,val));
count += 1; count += 1;
} }
Some(res) Some(res)
} }
pub fn make_unsigned(m: HashMap<String,SCN>) -> HashMap<String,UCN> pub fn make_signed(m: HashMap<String,(bool,Vec<u8>)>) -> HashMap<String,SCN>
{
let mut res: HashMap<String,SCN> = HashMap::new();
for (key, (neg, bval)) in m.iter() {
let base = UCN::from_bytes(bval);
let val = SCN{ negative: *neg, value: base };
res.insert(key.clone(), val);
}
res
}
pub fn make_unsigned(m: HashMap<String,(bool,Vec<u8>)>) -> HashMap<String,UCN>
{ {
let mut res: HashMap<String,UCN> = HashMap::new(); let mut res: HashMap<String,UCN> = HashMap::new();
for (key, sval) in m.iter() { for (key, (neg, bval)) in m.iter() {
assert!(!sval.is_negative()); assert!(!neg);
res.insert(key.clone(), sval.clone().into()); res.insert(key.clone(), UCN::from_bytes(bval));
} }
res res
} }
pub fn run_test<F>(fname: &'static str, i: usize, f: F) pub fn run_test<F>(fname: &'static str, i: usize, f: F)
where F: Fn(HashMap<String,SCN>) where F: Fn(HashMap<String,(bool,Vec<u8>)>)
{ {
let mut file = File::open(fname).unwrap(); let mut file = File::open(fname).unwrap();
let mut contents = String::new(); let mut contents = String::new();
@@ -53,5 +86,3 @@ pub fn run_test<F>(fname: &'static str, i: usize, f: F)
f(scase); f(scase);
} }
} }