From 1f15ff93f57ab3c17f8dc287f01fa3586e2e13a1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 8 Dec 2021 14:14:50 -0500 Subject: [PATCH] Add some more tests to bigint module --- src/bigint.rs | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index ca46895..55472c2 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -887,7 +887,7 @@ mod tests { #[test] fn test_not() { - let a = BigInt::from(0u8); + let a = BigInt::from(0_u8); let b = !a; assert_eq!(b.inner[0], core::usize::MAX); @@ -895,7 +895,7 @@ mod tests { #[test] fn test_partial_eq() { - let a = 12345u16; + let a = 12345_u16; let b = BigInt::from(a); assert!(a.eq(&b)); @@ -904,9 +904,9 @@ mod tests { #[test] fn test_partial_ord() { - let a = 12345u32; + let a = 12345_u32; let b = BigInt::from(a); - let c = 3u8; + let c = 3_u8; assert_eq!(a.partial_cmp(&b), Some(Ordering::Equal)); assert_eq!(c.partial_cmp(&b), Some(Ordering::Less)); @@ -924,7 +924,7 @@ mod tests { assert_eq!(-BigInt::from(2), BigInt::from(-2)); // Larger than usize - assert_eq!(BigInt::from(45u128), BigInt::from(45usize)); + assert_eq!(BigInt::from(45_u128), BigInt::from(45_usize)); } #[test] @@ -933,8 +933,8 @@ mod tests { let res = BigInt::from(big_num); assert_eq!(res.sign, Sign::Positive, "{:#?}", res); - assert_eq!(res.inner[0], 8usize, "{:#?}", res); - assert_eq!(res.inner[1], 9usize, "{:#?}", res); + assert_eq!(res.inner[0], 8_usize, "{:#?}", res); + assert_eq!(res.inner[1], 9_usize, "{:#?}", res); } #[test] @@ -943,8 +943,8 @@ mod tests { let res = BigInt::from(-big_num); assert_eq!(res.sign, Sign::Negative, "{:#?}", res); - assert_eq!(res.inner[0], 3usize, "{:#?}", res); - assert_eq!(res.inner[1], 2usize, "{:#?}", res); + assert_eq!(res.inner[0], 3_usize, "{:#?}", res); + assert_eq!(res.inner[1], 2_usize, "{:#?}", res); } #[test] @@ -958,7 +958,7 @@ mod tests { fn test_from_str_small() { let str = "012345"; let num = BigInt::from(str); - assert_eq!(num.inner[0], 12345usize); + assert_eq!(num.inner[0], 12345_usize); } #[test] @@ -972,20 +972,36 @@ mod tests { fn test_from_string_small() { let str = String::from("012345"); let num = BigInt::from(str); - assert_eq!(num.inner[0], 12345usize); + assert_eq!(num.inner[0], 12345_usize); } #[test] fn test_from_str_radix_1() { let s = "1".repeat(32); let num = BigInt::from_str_radix(s, 1); - assert_eq!(num.inner[0], 32usize); + assert_eq!(num.inner[0], 32_usize); + } + + #[test] + fn test_from_str_radix_1_with_zeroes() { + let ones = "1".repeat(32); + let zeroes = "0".repeat(24); + let s = ones + &zeroes; + + let num = BigInt::from_str_radix(s, 1); + assert_eq!(num.inner[0], 32_usize); + } + + #[test] + #[should_panic] + fn test_from_str_radix_invalid() { + let _ = BigInt::from_str_radix("foobar0", 50); } #[test] #[should_panic] fn test_from_str_radix_large() { - BigInt::from_str_radix("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321", 36); + let _ = BigInt::from_str_radix("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321", 36); } #[test] @@ -993,12 +1009,12 @@ mod tests { let num = BigInt::from_str_radix("FEDCBA", 16); assert!(num.inner[0] > 0, "Number is not greater than 0"); assert!(num.inner[0] < usize::MAX, "Result is larger than usize"); - assert_eq!(num.inner[0], 0xFEDCBAusize); + assert_eq!(num.inner[0], 0xFEDCBA_usize); } #[test] fn test_from_str_radix_lowercase() { let num = BigInt::from_str_radix("fedcba", 16); - assert_eq!(num.inner[0], 0xFEDCBAusize); + assert_eq!(num.inner[0], 0xFEDCBA_usize); } }