Skip to content

Commit

Permalink
Small refactor of bigint tests
Browse files Browse the repository at this point in the history
Print errors immediately rather than deferring to the end, so any debug
output shows up immediately before the relevant failed test.
  • Loading branch information
tgross35 committed Feb 10, 2025
1 parent 5151be0 commit 90872c1
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/math/support/big/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate std;
use std::string::String;
use std::vec::Vec;
use std::{eprintln, format};

use super::{HInt, MinInt, i256, u256};
Expand Down Expand Up @@ -36,28 +35,30 @@ fn widen_mul_u128() {
(0, 1234, u256::ZERO),
];

let mut errors = Vec::new();
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
let res = a.widen_mul(b);
let res_z = a.zero_widen_mul(b);
assert_eq!(res, res_z);
if res != exp {
errors.push((i, a, b, exp, res));
}
}

for (i, a, b, exp, res) in &errors {
let mut has_errors = false;
let mut add_error = |i, a, b, expected, actual| {
has_errors = true;
eprintln!(
"\
FAILURE ({i}): {a:#034x} * {b:#034x}\n\
expected: {}\n\
got: {}\
",
hexu(*exp),
hexu(*res)
hexu(expected),
hexu(actual)
);
};

for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
let res = a.widen_mul(b);
let res_z = a.zero_widen_mul(b);
assert_eq!(res, res_z);
if res != exp {
add_error(i, a, b, exp, res);
}
}
assert!(errors.is_empty());

assert!(!has_errors);
}

#[test]
Expand All @@ -68,7 +69,21 @@ fn not_u256() {
#[test]
fn shr_u256() {
let only_low = [1, u16::MAX.into(), u32::MAX.into(), u64::MAX.into(), u128::MAX];
let mut errors = Vec::new();
let mut has_errors = false;

let mut add_error = |a, b, expected, actual| {
has_errors = true;
eprintln!(
"\
FAILURE: {} >> {b}\n\
expected: {}\n\
actual: {}\
",
hexu(a),
hexu(expected),
hexu(actual),
);
};

for a in only_low {
for perturb in 0..10 {
Expand All @@ -77,7 +92,7 @@ fn shr_u256() {
let res = a.widen() >> shift;
let expected = (a >> shift).widen();
if res != expected {
errors.push((a.widen(), shift, res, expected));
add_error(a.widen(), shift, expected, res);
}
}
}
Expand Down Expand Up @@ -107,23 +122,11 @@ fn shr_u256() {
for (input, shift, expected) in check {
let res = input >> shift;
if res != expected {
errors.push((input, shift, res, expected));
add_error(input, shift, expected, res);
}
}

for (a, b, res, expected) in &errors {
eprintln!(
"\
FAILURE: {} >> {b}\n\
expected: {}\n\
got: {}\
",
hexu(*a),
hexu(*expected),
hexu(*res)
);
}
assert!(errors.is_empty());
assert!(!has_errors);
}

#[test]
Expand Down

0 comments on commit 90872c1

Please sign in to comment.