about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2025-02-10 05:26:50 +0000
committerTrevor Gross <t.gross35@gmail.com>2025-02-10 14:20:37 -0600
commita6dd7980f1553511ed6c03213924f202ee77ae4d (patch)
treed7b7a90d910dd4f36435cdfc0aef4522e07c44e2
parent8e7c83ea2d7ef900d3f1ad2d629dcd88f52b316b (diff)
downloadrust-a6dd7980f1553511ed6c03213924f202ee77ae4d.tar.gz
rust-a6dd7980f1553511ed6c03213924f202ee77ae4d.zip
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.
-rw-r--r--library/compiler-builtins/libm/src/math/support/big/tests.rs65
1 files changed, 34 insertions, 31 deletions
diff --git a/library/compiler-builtins/libm/src/math/support/big/tests.rs b/library/compiler-builtins/libm/src/math/support/big/tests.rs
index 6d06c700a5e..2c71191ba53 100644
--- a/library/compiler-builtins/libm/src/math/support/big/tests.rs
+++ b/library/compiler-builtins/libm/src/math/support/big/tests.rs
@@ -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};
@@ -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]
@@ -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 {
@@ -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);
                 }
             }
         }
@@ -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]