Skip to content

Commit

Permalink
Improve some test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
haxelion committed Jul 7, 2024
1 parent b7a0b1d commit 9c28d5d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/tests/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod add {
use crate::tests::*;
use crate::utils::StaticCast;

op_test_section!(Add::add, AddAssign::add_assign, { op_test_block });
op_test_section!(Add::add, AddAssign::add_assign, { op_test_block, op_test_block2 });
}

mod div {
Expand All @@ -22,7 +22,7 @@ mod sub {
use crate::tests::*;
use crate::utils::StaticCast;

op_test_section!(Sub::sub, SubAssign::sub_assign, { op_test_block });
op_test_section!(Sub::sub, SubAssign::sub_assign, { op_test_block, op_test_block2 });
}

mod mul {
Expand All @@ -31,7 +31,7 @@ mod mul {
use crate::tests::*;
use crate::utils::StaticCast;

op_test_section!(Mul::mul, MulAssign::mul_assign, { op_test_block });
op_test_section!(Mul::mul, MulAssign::mul_assign, { op_test_block, op_test_block2 });
}

mod rem {
Expand Down
43 changes: 10 additions & 33 deletions src/tests/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod not {
fn bvf_test_inner<I: Integer, const N: usize>() {
for size in 0..BVF::<I, N>::capacity() {
let (bv, bi) = random_test_bv::<BVF<I, N>>(size);
assert_eq!(!&bv, !&bi);
assert_eq!(!bv, !bi);
}
}
Expand All @@ -32,6 +33,7 @@ mod not {
fn bvd() {
for size in 0..512 {
let (bv, bi) = random_test_bv::<BVD>(size);
assert_eq!(!&bv, !&bi);
assert_eq!(!bv, !bi);
}
}
Expand All @@ -40,6 +42,7 @@ mod not {
fn bv() {
for size in 0..512 {
let (bv, bi) = random_test_bv::<BV>(size);
assert_eq!(!&bv, !&bi);
assert_eq!(!bv, !bi);
}
}
Expand All @@ -63,38 +66,12 @@ mod shift {
use crate::auto::BV;
use crate::dynamic::BVD;
use crate::fixed::BVF;
use crate::tests::{bvf_inner_unroll, random_test_bv};
use crate::tests::{bvf_inner_unroll, random_test_bv, shift_test_block};
use crate::utils::Integer;

macro_rules! op_test_block {
($lhs:ty, {$($rhs:ty),+}, $op:path, $op_assign:path, $size:ident) => {
$(
op_test_block!($lhs, $rhs, $op, $op_assign, $size);
)+
};
($lhs:ty, $rhs:ty, $op:path, $op_assign:path, $size:ident) => {
let modulo = BigInt::from(1u8) << $size;
let shift = (random::<usize>() % (2 * $size)) as $rhs;
let (bv, bi) = random_test_bv::<$lhs>($size);
let reference = $op(&bi, shift) % modulo;
// Normal op
assert_eq!($op(&bv, &shift), reference);
assert_eq!($op(bv.clone(), &shift), reference);
assert_eq!($op(&bv, shift), reference);
assert_eq!($op(bv.clone(), shift), reference);
// Assign op
let mut bv2 = bv.clone();
$op_assign(&mut bv2, &shift);
assert_eq!(bv2, reference);
bv2 = bv.clone();
$op_assign(&mut bv2, shift);
assert_eq!(bv2, reference);
};
}

fn shl_bvf_inner<I: Integer, const N: usize>() {
for size in 1..BVF::<I, N>::capacity() {
op_test_block!(BVF<I, N>, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
shift_test_block!(BVF<I, N>, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
}
}

Expand All @@ -106,20 +83,20 @@ mod shift {
#[test]
fn shl_bvd() {
for size in 1..512 {
op_test_block!(BVD, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
shift_test_block!(BVD, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
}
}

#[test]
fn shl_bv() {
for size in 1..512 {
op_test_block!(BV, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
shift_test_block!(BV, {u8, u16, u32, u64, u128, usize}, Shl::shl, ShlAssign::shl_assign, size);
}
}

fn shr_bvf_inner<I: Integer, const N: usize>() {
for size in 1..BVF::<I, N>::capacity() {
op_test_block!(BVF<I, N>, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
shift_test_block!(BVF<I, N>, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
}
}

Expand All @@ -131,14 +108,14 @@ mod shift {
#[test]
fn shr_bvd() {
for size in 1..512 {
op_test_block!(BVD, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
shift_test_block!(BVD, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
}
}

#[test]
fn shr_bv() {
for size in 1..512 {
op_test_block!(BV, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
shift_test_block!(BV, {u8, u16, u32, u64, u128, usize}, Shr::shr, ShrAssign::shr_assign, size);
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions src/tests/bitvector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::io::Cursor;
use std::iter::repeat;

use rand::seq::SliceRandom;
use rand::{thread_rng, Rng};
use rand::{thread_rng, Rng, RngCore};

use crate::auto::BV;
use crate::bit::Bit;
use crate::dynamic::BVD;
use crate::fixed::BVF;
use crate::tests::{bvf_inner_unroll_cap, random_bv};
use crate::{BitVector, Endianness};
use crate::{BitVector, ConvertionError, Endianness};

fn with_capacity_inner<B: BitVector>(max_capacity: usize) {
for c in 0..max_capacity {
Expand Down Expand Up @@ -127,6 +127,29 @@ fn from_to_bytes_inner<B: BitVector>(max_capacity: usize) {
#[test]
fn from_to_bytes_bvf() {
bvf_inner_unroll_cap!(from_to_bytes_inner, {u8, u16, u32, u64, u128}, {1, 2, 3, 4, 5});

let mut buffer: Vec<_> = repeat(0u8).take(64).collect();
thread_rng().fill_bytes(&mut buffer);
assert_eq!(
BVF::<u8, 2>::from_bytes(&buffer, Endianness::BE),
Err(ConvertionError::NotEnoughCapacity)
);
assert_eq!(
BVF::<u16, 2>::from_bytes(&buffer, Endianness::BE),
Err(ConvertionError::NotEnoughCapacity)
);
assert_eq!(
BVF::<u32, 2>::from_bytes(&buffer, Endianness::BE),
Err(ConvertionError::NotEnoughCapacity)
);
assert_eq!(
BVF::<u64, 2>::from_bytes(&buffer, Endianness::BE),
Err(ConvertionError::NotEnoughCapacity)
);
assert_eq!(
BVF::<u128, 2>::from_bytes(&buffer, Endianness::BE),
Err(ConvertionError::NotEnoughCapacity)
);
}

#[test]
Expand Down Expand Up @@ -163,6 +186,14 @@ fn read_write_inner<B: BitVector>(max_capacity: usize) {
#[test]
fn read_write_inner_bvf() {
bvf_inner_unroll_cap!(read_write_inner, {u8, u16, u32, u64, u128}, {1, 2, 3, 4, 5});

let mut buffer: Vec<_> = repeat(0u8).take(64).collect();
thread_rng().fill_bytes(&mut buffer);
assert!(BVF::<u8, 2>::read(&mut Cursor::new(&buffer), 512, Endianness::BE).is_err());
assert!(BVF::<u16, 2>::read(&mut Cursor::new(&buffer), 512, Endianness::BE).is_err());
assert!(BVF::<u32, 2>::read(&mut Cursor::new(&buffer), 512, Endianness::BE).is_err());
assert!(BVF::<u64, 2>::read(&mut Cursor::new(&buffer), 512, Endianness::BE).is_err());
assert!(BVF::<u128, 2>::read(&mut Cursor::new(&buffer), 512, Endianness::BE).is_err());
}

#[test]
Expand Down
32 changes: 30 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ macro_rules! op_test_block2 {
($lhs:ty, $rhs:ty, $op:path, $op_assign:path, $size:ident) => {
let modulo = BigInt::from(1u8) << $size;
let (bv1, bi1) = random_test_bv::<$lhs>($size);
let (mut bv2, mut bi2) = random_test_bv::<$rhs>(usize::min(1, $size));
let (mut bv2, mut bi2) = random_test_bv::<$rhs>(usize::max(1, $size / 2));
while bv2.is_zero() {
(bv2, bi2) = random_test_bv::<$rhs>(usize::min(1, $size));
(bv2, bi2) = random_test_bv::<$rhs>(usize::max(1, $size / 2));
}
let reference = $op(&bi1, &bi2) % &modulo;
// Normal op
Expand All @@ -163,6 +163,33 @@ macro_rules! op_test_block2 {
};
}

// Variant suited for shifts.
macro_rules! shift_test_block {
($lhs:ty, {$($rhs:ty),+}, $op:path, $op_assign:path, $size:ident) => {
$(
shift_test_block!($lhs, $rhs, $op, $op_assign, $size);
)+
};
($lhs:ty, $rhs:ty, $op:path, $op_assign:path, $size:ident) => {
let modulo = BigInt::from(1u8) << $size;
let shift = (random::<usize>() % (2 * $size)) as $rhs;
let (bv, bi) = random_test_bv::<$lhs>($size);
let reference = $op(&bi, shift) % modulo;
// Normal op
assert_eq!($op(&bv, &shift), reference);
assert_eq!($op(bv.clone(), &shift), reference);
assert_eq!($op(&bv, shift), reference);
assert_eq!($op(bv.clone(), shift), reference);
// Assign op
let mut bv2 = bv.clone();
$op_assign(&mut bv2, &shift);
assert_eq!(bv2, reference);
bv2 = bv.clone();
$op_assign(&mut bv2, shift);
assert_eq!(bv2, reference);
};
}

macro_rules! op_test_section {
($op:path, $op_assign:path, {$($block:path),+}) => {
fn bvf_bvf_inner<
Expand Down Expand Up @@ -253,3 +280,4 @@ pub(crate) use bvf_inner_unroll_cap;
pub(crate) use op_test_block;
pub(crate) use op_test_block2;
pub(crate) use op_test_section;
pub(crate) use shift_test_block;

0 comments on commit 9c28d5d

Please sign in to comment.