refactor tests

This commit is contained in:
Kitsu 2020-04-02 14:19:51 +03:00
parent 74abd24423
commit ae4ff59b1c
4 changed files with 32 additions and 48 deletions

View File

@ -126,6 +126,7 @@ impl Product for GF256 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{EXP_TABLE, GF256, LOG_TABLE}; use super::{EXP_TABLE, GF256, LOG_TABLE};
use alloc::vec;
#[test] #[test]
fn add_works() { fn add_works() {
@ -206,13 +207,13 @@ mod tests {
#[test] #[test]
fn sum_works() { fn sum_works() {
let values = alloc::vec![GF256(0x53), GF256(0xCA), GF256(0)]; let values = vec![GF256(0x53), GF256(0xCA), GF256(0)];
assert_eq!(values.into_iter().sum::<GF256>().0, 0x99); assert_eq!(values.into_iter().sum::<GF256>().0, 0x99);
} }
#[test] #[test]
fn product_works() { fn product_works() {
let values = alloc::vec![GF256(1), GF256(1), GF256(4)]; let values = vec![GF256(1), GF256(1), GF256(4)];
assert_eq!(values.into_iter().product::<GF256>().0, 4); assert_eq!(values.into_iter().product::<GF256>().0, 4);
} }
} }

View File

@ -167,23 +167,27 @@ impl Sharks {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Share, Sharks}; use super::{Share, Sharks};
use alloc::vec::Vec; use alloc::{vec, vec::Vec};
#[cfg(not(feature = "std"))]
use rand_chacha::rand_core::SeedableRng; impl Sharks {
#[cfg(not(feature = "std"))]
fn make_shares(&self, secret: &[u8]) -> impl Iterator<Item = Share> {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
let mut rng = ChaCha8Rng::from_seed([0x90; 32]);
self.dealer_rng(secret, &mut rng)
}
#[cfg(feature = "std")]
fn make_shares(&self, secret: &[u8]) -> impl Iterator<Item = Share> {
self.dealer(secret)
}
}
#[test] #[test]
fn test_insufficient_shares_err() { fn test_insufficient_shares_err() {
let sharks = Sharks(255); let sharks = Sharks(255);
let shares: Vec<Share> = sharks.make_shares(&[1]).take(254).collect();
#[cfg(not(feature = "std"))]
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
#[cfg(feature = "std")]
let dealer = sharks.dealer(&[1]);
#[cfg(not(feature = "std"))]
let dealer = sharks.dealer_rng(&[1], &mut rng);
let shares: Vec<Share> = dealer.take(254).collect();
let secret = sharks.recover(&shares); let secret = sharks.recover(&shares);
assert!(secret.is_err()); assert!(secret.is_err());
} }
@ -191,16 +195,7 @@ mod tests {
#[test] #[test]
fn test_duplicate_shares_err() { fn test_duplicate_shares_err() {
let sharks = Sharks(255); let sharks = Sharks(255);
let mut shares: Vec<Share> = sharks.make_shares(&[1]).take(255).collect();
#[cfg(not(feature = "std"))]
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
#[cfg(feature = "std")]
let dealer = sharks.dealer(&[1]);
#[cfg(not(feature = "std"))]
let dealer = sharks.dealer_rng(&[1], &mut rng);
let mut shares: Vec<Share> = dealer.take(255).collect();
shares[1] = Share { shares[1] = Share {
x: shares[0].x, x: shares[0].x,
y: shares[0].y.clone(), y: shares[0].y.clone(),
@ -212,17 +207,8 @@ mod tests {
#[test] #[test]
fn test_integration_works() { fn test_integration_works() {
let sharks = Sharks(255); let sharks = Sharks(255);
let shares: Vec<Share> = sharks.make_shares(&[1, 2, 3, 4]).take(255).collect();
#[cfg(not(feature = "std"))]
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
#[cfg(feature = "std")]
let dealer = sharks.dealer(&[1, 2, 3, 4]);
#[cfg(not(feature = "std"))]
let dealer = sharks.dealer_rng(&[1, 2, 3, 4], &mut rng);
let shares: Vec<Share> = dealer.take(255).collect();
let secret = sharks.recover(&shares).unwrap(); let secret = sharks.recover(&shares).unwrap();
assert_eq!(secret, alloc::vec![1, 2, 3, 4]); assert_eq!(secret, vec![1, 2, 3, 4]);
} }
} }

View File

@ -62,7 +62,7 @@ pub fn get_evaluator(polys: Vec<Vec<GF256>>) -> impl Iterator<Item = Share> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{get_evaluator, interpolate, random_polynomial, Share, GF256}; use super::{get_evaluator, interpolate, random_polynomial, Share, GF256};
use alloc::vec::Vec; use alloc::{vec, vec::Vec};
use rand_chacha::rand_core::SeedableRng; use rand_chacha::rand_core::SeedableRng;
#[test] #[test]
@ -75,14 +75,11 @@ mod tests {
#[test] #[test]
fn evaluator_works() { fn evaluator_works() {
let iter = get_evaluator(alloc::vec![alloc::vec![GF256(3), GF256(2), GF256(5)]]); let iter = get_evaluator(vec![vec![GF256(3), GF256(2), GF256(5)]]);
let values: Vec<_> = iter.take(2).map(|s| (s.x, s.y)).collect(); let values: Vec<_> = iter.take(2).map(|s| (s.x, s.y)).collect();
assert_eq!( assert_eq!(
values, values,
alloc::vec![ vec![(GF256(1), vec![GF256(4)]), (GF256(2), vec![GF256(13)])]
(GF256(1), alloc::vec![GF256(4)]),
(GF256(2), alloc::vec![GF256(13)])
]
); );
} }
@ -90,9 +87,9 @@ mod tests {
fn interpolate_works() { fn interpolate_works() {
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
let poly = random_polynomial(GF256(185), 10, &mut rng); let poly = random_polynomial(GF256(185), 10, &mut rng);
let iter = get_evaluator(alloc::vec![poly]); let iter = get_evaluator(vec![poly]);
let shares: Vec<Share> = iter.take(10).collect(); let shares: Vec<Share> = iter.take(10).collect();
let root = interpolate(&shares); let root = interpolate(&shares);
assert_eq!(root, alloc::vec![185]); assert_eq!(root, vec![185]);
} }
} }

View File

@ -53,16 +53,16 @@ impl From<&[u8]> for Share {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{Share, GF256}; use super::{Share, GF256};
use alloc::vec::Vec; use alloc::{vec, vec::Vec};
#[test] #[test]
fn vec_from_share_works() { fn vec_from_share_works() {
let share = Share { let share = Share {
x: GF256(1), x: GF256(1),
y: alloc::vec![GF256(2), GF256(3)], y: vec![GF256(2), GF256(3)],
}; };
let bytes = Vec::from(&share); let bytes = Vec::from(&share);
assert_eq!(bytes, alloc::vec![1, 2, 3]); assert_eq!(bytes, vec![1, 2, 3]);
} }
#[test] #[test]
@ -70,6 +70,6 @@ mod tests {
let bytes = [1, 2, 3]; let bytes = [1, 2, 3];
let share = Share::from(&bytes[..]); let share = Share::from(&bytes[..]);
assert_eq!(share.x, GF256(1)); assert_eq!(share.x, GF256(1));
assert_eq!(share.y, alloc::vec![GF256(2), GF256(3)]); assert_eq!(share.y, vec![GF256(2), GF256(3)]);
} }
} }