From a5e3e88e1ef16e2cf5928b7c7910535316fe310f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Mar 2020 09:19:33 -0500 Subject: [PATCH] Add proper test for add function --- src/bigint.rs | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index 51e4df6..376ba0b 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -38,18 +38,18 @@ macro_rules! impl_from_smaller { #[cfg(target_pointer_width = "16")] impl_from_smaller!(u8, u16); -#[cfg(target_pointer_width = "32")] -impl_from_smaller!(u8, u16, u32); - -#[cfg(target_pointer_width = "64")] -impl_from_smaller!(u8, u16, u32, u64); - #[cfg(target_pointer_width = "16")] static BITS:usize = 16; +#[cfg(target_pointer_width = "32")] +impl_from_smaller!(u8, u16, u32); + #[cfg(target_pointer_width = "32")] static BITS:usize = 32; +#[cfg(target_pointer_width = "64")] +impl_from_smaller!(u8, u16, u32, u64); + #[cfg(target_pointer_width = "64")] static BITS:usize = 64; @@ -78,7 +78,7 @@ fn add(u: BigInt, v: BigInt) -> BigInt { } else { out.inner.push(res); } - carry = core::usize::MAX - res + 1; + carry = core::usize::MAX - res; } else { if res < core::usize::MAX { out.inner.push(res + carry); @@ -134,18 +134,7 @@ impl BigInt { todo!(); } - pub fn from_str_radix() { - todo!(); - } - - /// Split an unsigned number into BigInt parts - fn split(&mut self, num: T) -> Vec { - // Pretty easy if you don't actually need to split the value! - if num < T::max_value() { - todo!(); - // return vec![num as usize]; - } - + pub fn from_str_radix(s: T, radix: usize) { todo!(); } } @@ -160,13 +149,20 @@ mod tests { assert_eq!(int.inner[0], 45usize) } - /* #[test] + #[test] fn test_add() { - let a = BigInt::from(core::u64::MAX); - let b = BigInt::from(core::u64::MAX); + // MAX is 2^Bitsize - 1, + // so the sum should be + // [MAX -1, 1] + // Compare base 10: 9 + 9 = 18 + let a = BigInt::from(core::usize::MAX); + let b = BigInt::from(core::usize::MAX); let sum = add(a, b); - assert!(false, "{:#?} should have inner equal to {:?}", sum, core::u64::MAX as u128 + core::u64::MAX as u128); - } */ + assert_eq!(sum.inner[0], core::usize::MAX - 1, "least significant place should be MAX - 1"); + assert_eq!(sum.inner[1], 1usize, "most significant place should be 1"); + + // assert!(false, "{:#?} should have inner equal to {:?}", sum, core::u64::MAX as u128 + core::u64::MAX as u128); + } }