From 9f60706a9b7a24f2cf9762e00460356c290bb321 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 11 Mar 2020 11:16:40 -0400 Subject: [PATCH] More tests --- src/bigint.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index 3e8b78b..551ecc6 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -142,6 +142,7 @@ impl Sub for BigInt { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { + // @TODO: handle signs let mut out = BigInt::new_empty(); let u_digits = self.inner.len(); @@ -158,10 +159,6 @@ impl Sub for BigInt { let a = *self.inner.get(i).unwrap_or(&0usize); let b = *rhs.inner.get(i).unwrap_or(&0usize); - if a == 0 && b == 0 { - continue; - } - if a > 0 && (a - borrow) >= b { let res = a - b - borrow; @@ -362,6 +359,18 @@ mod tests { assert_eq!(int.inner[0], 45usize) } + #[test] + fn test_trim_zeros() { + let mut lotsoftrailing = BigInt { + inner: vec![1, 0, 0, 0, 0, 0, 0, 0, 0], + sign: Positive + }; + + lotsoftrailing.trim_zeros(); + + assert_eq!(BigInt::from(1), lotsoftrailing); + } + #[test] fn test_add() { // MAX is 2^Bitsize - 1, @@ -414,6 +423,17 @@ mod tests { diff.inner[0], core::usize::MAX - 1 ); + + let a = BigInt { + inner: vec![1,0,1], + sign: Positive + }; + let b = BigInt::from(2); + let diff = a - b; + assert_eq!( + diff.inner, + vec![core::usize::MAX, core::usize::MAX] + ); } #[test]