Skip to content

Commit 401e43e

Browse files
committed
Small refactor of bigint tests
Print errors immediately rather than deferring to the end, so any debug output shows up immediately before the relevant failed test.
1 parent 010201f commit 401e43e

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

libm/src/math/support/big/tests.rs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
extern crate std;
22
use std::string::String;
3-
use std::vec::Vec;
43
use std::{eprintln, format};
54

65
use super::{HInt, MinInt, i256, u256};
@@ -36,28 +35,30 @@ fn widen_mul_u128() {
3635
(0, 1234, u256::ZERO),
3736
];
3837

39-
let mut errors = Vec::new();
40-
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
41-
let res = a.widen_mul(b);
42-
let res_z = a.zero_widen_mul(b);
43-
assert_eq!(res, res_z);
44-
if res != exp {
45-
errors.push((i, a, b, exp, res));
46-
}
47-
}
48-
49-
for (i, a, b, exp, res) in &errors {
38+
let mut has_errors = false;
39+
let mut add_error = |i, a, b, expected, actual| {
40+
has_errors = true;
5041
eprintln!(
5142
"\
5243
FAILURE ({i}): {a:#034x} * {b:#034x}\n\
5344
expected: {}\n\
5445
got: {}\
5546
",
56-
hexu(*exp),
57-
hexu(*res)
47+
hexu(expected),
48+
hexu(actual)
5849
);
50+
};
51+
52+
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
53+
let res = a.widen_mul(b);
54+
let res_z = a.zero_widen_mul(b);
55+
assert_eq!(res, res_z);
56+
if res != exp {
57+
add_error(i, a, b, exp, res);
58+
}
5959
}
60-
assert!(errors.is_empty());
60+
61+
assert!(!has_errors);
6162
}
6263

6364
#[test]
@@ -68,7 +69,21 @@ fn not_u256() {
6869
#[test]
6970
fn shr_u256() {
7071
let only_low = [1, u16::MAX.into(), u32::MAX.into(), u64::MAX.into(), u128::MAX];
71-
let mut errors = Vec::new();
72+
let mut has_errors = false;
73+
74+
let mut add_error = |a, b, expected, actual| {
75+
has_errors = true;
76+
eprintln!(
77+
"\
78+
FAILURE: {} >> {b}\n\
79+
expected: {}\n\
80+
actual: {}\
81+
",
82+
hexu(a),
83+
hexu(expected),
84+
hexu(actual),
85+
);
86+
};
7287

7388
for a in only_low {
7489
for perturb in 0..10 {
@@ -77,7 +92,7 @@ fn shr_u256() {
7792
let res = a.widen() >> shift;
7893
let expected = (a >> shift).widen();
7994
if res != expected {
80-
errors.push((a.widen(), shift, res, expected));
95+
add_error(a.widen(), shift, expected, res);
8196
}
8297
}
8398
}
@@ -107,23 +122,11 @@ fn shr_u256() {
107122
for (input, shift, expected) in check {
108123
let res = input >> shift;
109124
if res != expected {
110-
errors.push((input, shift, res, expected));
125+
add_error(input, shift, expected, res);
111126
}
112127
}
113128

114-
for (a, b, res, expected) in &errors {
115-
eprintln!(
116-
"\
117-
FAILURE: {} >> {b}\n\
118-
expected: {}\n\
119-
got: {}\
120-
",
121-
hexu(*a),
122-
hexu(*expected),
123-
hexu(*res)
124-
);
125-
}
126-
assert!(errors.is_empty());
129+
assert!(!has_errors);
127130
}
128131

129132
#[test]

0 commit comments

Comments
 (0)