Factor out the testing code, so we can use it later.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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<u8>)
|
||||
{
|
||||
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<HashMap<String,SCN>>
|
||||
Option<HashMap<String,(bool,Vec<u8>)>>
|
||||
{
|
||||
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<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();
|
||||
|
||||
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<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 contents = String::new();
|
||||
@@ -53,5 +86,3 @@ pub fn run_test<F>(fname: &'static str, i: usize, f: F)
|
||||
f(scase);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user