1
1
extern crate std;
2
2
use std:: string:: String ;
3
- use std:: vec:: Vec ;
4
3
use std:: { eprintln, format} ;
5
4
6
5
use super :: { HInt , MinInt , i256, u256} ;
@@ -36,28 +35,30 @@ fn widen_mul_u128() {
36
35
( 0 , 1234 , u256:: ZERO ) ,
37
36
] ;
38
37
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 ;
50
41
eprintln ! (
51
42
"\
52
43
FAILURE ({i}): {a:#034x} * {b:#034x}\n \
53
44
expected: {}\n \
54
45
got: {}\
55
46
",
56
- hexu( * exp ) ,
57
- hexu( * res )
47
+ hexu( expected ) ,
48
+ hexu( actual )
58
49
) ;
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
+ }
59
59
}
60
- assert ! ( errors. is_empty( ) ) ;
60
+
61
+ assert ! ( !has_errors) ;
61
62
}
62
63
63
64
#[ test]
@@ -68,7 +69,21 @@ fn not_u256() {
68
69
#[ test]
69
70
fn shr_u256 ( ) {
70
71
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
+ } ;
72
87
73
88
for a in only_low {
74
89
for perturb in 0 ..10 {
@@ -77,7 +92,7 @@ fn shr_u256() {
77
92
let res = a. widen ( ) >> shift;
78
93
let expected = ( a >> shift) . widen ( ) ;
79
94
if res != expected {
80
- errors . push ( ( a. widen ( ) , shift, res , expected ) ) ;
95
+ add_error ( a. widen ( ) , shift, expected , res ) ;
81
96
}
82
97
}
83
98
}
@@ -107,23 +122,11 @@ fn shr_u256() {
107
122
for ( input, shift, expected) in check {
108
123
let res = input >> shift;
109
124
if res != expected {
110
- errors . push ( ( input, shift, res , expected ) ) ;
125
+ add_error ( input, shift, expected , res ) ;
111
126
}
112
127
}
113
128
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) ;
127
130
}
128
131
129
132
#[ test]
0 commit comments