alamb commented on code in PR #2818:
URL: https://github.com/apache/arrow-rs/pull/2818#discussion_r988887254


##########
arrow-buffer/src/bigint.rs:
##########
@@ -303,8 +350,106 @@ mod tests {
         }
     }
 
+    /// Tests operations against the two provided [`i256`]
+    fn test_ops(il: i256, ir: i256) {
+        let bl = BigInt::from_signed_bytes_le(&il.to_le_bytes());
+        let br = BigInt::from_signed_bytes_le(&ir.to_le_bytes());
+
+        // Comparison
+        assert_eq!(il.cmp(&ir), bl.cmp(&br), "{} cmp {}", bl, br);
+
+        // To i128
+        assert_eq!(il.to_i128(), bl.to_i128(), "{}", bl);
+        assert_eq!(ir.to_i128(), br.to_i128(), "{}", br);
+
+        // Absolute value
+        let (abs, overflow) = i256::from_bigint_with_overflow(bl.abs());
+        assert_eq!(il.wrapping_abs(), abs);
+        assert_eq!(il.checked_abs().is_none(), overflow);
+
+        let (abs, overflow) = i256::from_bigint_with_overflow(br.abs());
+        assert_eq!(ir.wrapping_abs(), abs);
+        assert_eq!(ir.checked_abs().is_none(), overflow);
+
+        // Addition
+        let actual = il.wrapping_add(ir);
+        let (expected, overflow) =
+            i256::from_bigint_with_overflow(bl.clone() + br.clone());
+        assert_eq!(actual, expected);
+
+        let checked = il.checked_add(ir);
+        match overflow {
+            true => assert!(checked.is_none()),
+            false => assert_eq!(checked.unwrap(), actual),
+        }
+
+        // Subtraction
+        let actual = il.wrapping_sub(ir);
+        let (expected, overflow) =
+            i256::from_bigint_with_overflow(bl.clone() - br.clone());
+        assert_eq!(actual.to_string(), expected.to_string());
+
+        let checked = il.checked_sub(ir);
+        match overflow {
+            true => assert!(checked.is_none()),
+            false => assert_eq!(checked.unwrap(), actual),
+        }
+
+        // Multiplication
+        let actual = il.wrapping_mul(ir);
+        let (expected, overflow) =
+            i256::from_bigint_with_overflow(bl.clone() * br.clone());
+        assert_eq!(actual.to_string(), expected.to_string());
+
+        let checked = il.checked_mul(ir);
+        match overflow {
+            true => assert!(
+                checked.is_none(),
+                "{} * {} = {} vs {} * {} = {}",
+                il,
+                ir,
+                actual,
+                bl,
+                br,
+                expected
+            ),
+            false => assert_eq!(
+                checked.unwrap(),
+                actual,
+                "{} * {} = {} vs {} * {} = {}",
+                il,
+                ir,
+                actual,
+                bl,
+                br,
+                expected
+            ),
+        }
+    }
+
     #[test]
     fn test_i256() {
+        let candidates = [
+            i256::from_parts(0, 0),
+            i256::from_parts(0, 1),
+            i256::from_parts(0, -1),
+            i256::from_parts(u128::MAX, 1),

Review Comment:
   thanks -- this is much easier to ensure coverage



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to