From 2eacea8ff9766f76b1ac4ede252e290926b1780c Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Mon, 30 Apr 2018 13:05:10 -0700 Subject: [PATCH] Factor out the testing code, so we can use it later. --- src/cryptonum/gold_tests.rs | 20 +++++++----- src/cryptonum/signed.rs | 2 +- src/testing.rs | 61 ++++++++++++++++++++++++++++--------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/cryptonum/gold_tests.rs b/src/cryptonum/gold_tests.rs index e5f3d43..3f42e27 100644 --- a/src/cryptonum/gold_tests.rs +++ b/src/cryptonum/gold_tests.rs @@ -1,5 +1,5 @@ use cryptonum::unsigned::BarrettUCN; -use testing::{make_unsigned,run_test}; +use testing::{make_signed,make_unsigned,run_test}; #[test] fn unsigned_sum_test() @@ -9,14 +9,16 @@ fn unsigned_sum_test() let x = case.get("x").unwrap(); let y = case.get("y").unwrap(); let z = case.get("z").unwrap(); - assert_eq!(x + y, *z); + let res = x + y; + assert_eq!(res, *z); }); } #[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 y = case.get("y").unwrap(); let z = case.get("z").unwrap(); @@ -39,7 +41,8 @@ fn unsigned_sub_test() #[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 y = case.get("y").unwrap(); let z = case.get("z").unwrap(); @@ -62,7 +65,8 @@ fn unsigned_mul_test() #[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 y = case.get("y").unwrap(); let z = case.get("z").unwrap(); @@ -85,7 +89,8 @@ fn unsigned_div_test() #[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 y = case.get("y").unwrap(); let z = case.get("z").unwrap(); @@ -108,7 +113,8 @@ fn unsigned_mod_test() #[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 y = case.get("y").unwrap(); let z = case.get("z").unwrap(); diff --git a/src/cryptonum/signed.rs b/src/cryptonum/signed.rs index ede567f..4079f29 100644 --- a/src/cryptonum/signed.rs +++ b/src/cryptonum/signed.rs @@ -7,7 +7,7 @@ use std::ops::*; /// In case you were wondering, it stands for "Signed Crypto Num". #[derive(Clone,Debug,PartialEq,Eq)] pub struct SCN { - negative: bool, + pub(crate) negative: bool, pub(crate) value: UCN } diff --git a/src/testing.rs b/src/testing.rs index 3caa9aa..73416ff 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -4,45 +4,78 @@ use std::fs::File; use std::io::Read; use std::str::Lines; -fn next_value_set(line: &str) -> (String, SCN) +fn next_value_set(line: &str) -> (String, bool, Vec) { assert!(line.is_ascii()); - assert_eq!(": ", &line[1..3]); - let key = String::from(&line[0..1]); - let val = SCN::from_str(&line[3..]); - (key, val) + let mut items = line.split(": "); + let key = items.next().unwrap(); + let valbits = items.next().unwrap(); + 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) -> - Option> + Option)>> { let mut res = HashMap::new(); let mut count = 0; while count < lines { let line = contents.next()?; - let (key, val) = next_value_set(line); - res.insert(key, val); + let (key, neg, val) = next_value_set(line); + res.insert(key, (neg,val)); count += 1; } Some(res) } -pub fn make_unsigned(m: HashMap) -> HashMap +pub fn make_signed(m: HashMap)>) -> HashMap +{ + let mut res: HashMap = 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)>) -> HashMap { let mut res: HashMap = HashMap::new(); - for (key, sval) in m.iter() { - assert!(!sval.is_negative()); - res.insert(key.clone(), sval.clone().into()); + for (key, (neg, bval)) in m.iter() { + assert!(!neg); + res.insert(key.clone(), UCN::from_bytes(bval)); } res } pub fn run_test(fname: &'static str, i: usize, f: F) - where F: Fn(HashMap) + where F: Fn(HashMap)>) { let mut file = File::open(fname).unwrap(); let mut contents = String::new(); @@ -53,5 +86,3 @@ pub fn run_test(fname: &'static str, i: usize, f: F) f(scase); } } - -