diff --git a/Cargo.toml b/Cargo.toml index dcdc6c5..1f82c45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ exclude = ["src/float/*", "src/tests", "TODO.txt", "lit-parser/*"] # TODO: make [features] default = [] nightly = [] -float = [] serde = ["dep:serde", "serde-big-array"] numtraits = ["num-integer", "num-traits"] @@ -49,4 +48,4 @@ opt-level = 3 # maximum optimisation level for faster runtime, but slower compil all-features = true [lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(test_int_bits, values("8", "16", "32", "64", "128"))'] } \ No newline at end of file +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(test_int_bits, values("16", "32", "64", "128"))'] } \ No newline at end of file diff --git a/changes/v0.12.1 b/changes/v0.12.1 new file mode 100644 index 0000000..140be3c --- /dev/null +++ b/changes/v0.12.1 @@ -0,0 +1,3 @@ +- fix midpoint method +- fix posoverflow being returned instead of invaliddigit for parsing ints +- change debug implementation of parseinterror \ No newline at end of file diff --git a/scripts/run_all_tests.sh b/scripts/run_all_tests.sh index 7e8b1b2..943d56a 100644 --- a/scripts/run_all_tests.sh +++ b/scripts/run_all_tests.sh @@ -8,7 +8,8 @@ test_integer_info () { export QUICKCHECK_TESTS=10000 run_test () { - echo $QUICKCHECK_TESTS + echo "using ${QUICKCHECK_TESTS} quickcheck tests per test" + test_integer_info "$1" RUSTFLAGS="--cfg test_int_bits=\"$1\"" cargo test int --lib --quiet $2 if [ $? -ne 0 ] @@ -20,7 +21,7 @@ run_test () { for flags in "" "--all-features" do echo "\n${CYAN_COLOR}info${RESET_FORMAT}: running tests with flags '$flags'..." - for bits in 8 16 32 64 128 + for bits in 16 32 64 128 do run_test $bits $flags done diff --git a/src/bint/bigint_helpers.rs b/src/bint/bigint_helpers.rs index c535ce7..aad91d4 100644 --- a/src/bint/bigint_helpers.rs +++ b/src/bint/bigint_helpers.rs @@ -4,17 +4,16 @@ macro_rules! bigint_helpers { impl $BInt { crate::int::bigint_helpers::impls!(I); } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::bigint_helpers::tests!(itest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::itest; + + crate::int::bigint_helpers::tests!(itest); +} + use crate::doc; crate::macro_impl!(bigint_helpers); diff --git a/src/bint/cast.rs b/src/bint/cast.rs index 0a7ca25..7bbc129 100644 --- a/src/bint/cast.rs +++ b/src/bint/cast.rs @@ -115,17 +115,16 @@ macro_rules! cast { impl CastFrom for $BInt { crate::bint::cast::bint_cast_from_float!(f64, $BUint); } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::cast::tests!(itest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::itest; + + crate::int::cast::tests!(itest); +} + crate::macro_impl!(cast); macro_rules! bint_as_different_digit_bigint { diff --git a/src/bint/checked.rs b/src/bint/checked.rs index f30a49d..050e3e1 100644 --- a/src/bint/checked.rs +++ b/src/bint/checked.rs @@ -187,99 +187,98 @@ macro_rules! checked { checked_ilog!(checked_ilog2); checked_ilog!(checked_ilog10); } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::*}; - test_bignum! { - function: ::checked_add(a: itest, b: itest), - cases: [ - (itest::MAX, -1i8) - ] - } - test_bignum! { - function: ::checked_add_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::checked_sub(a: itest, b: itest), - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::checked_sub_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::checked_mul(a: itest, b: itest), - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::checked_div(a: itest, b: itest), - cases: [ - (0, 0), - (23098403i32 as itest, 0i8), - (itest::MIN, -1i8), - (8388600i32 as itest, 68201i32 as itest) // tests the unlikely condition in the division algorithm at step D5 - ] - } - test_bignum! { - function: ::checked_div_euclid(a: itest, b: itest), - cases: [ - (itest::MIN, -1i8), - (0, 0) - ] - } - test_bignum! { - function: ::checked_rem(a: itest, b: itest), - cases: [ - (itest::MIN, -1i8), - (0, 0) - ] - } - test_bignum! { - function: ::checked_rem_euclid(a: itest, b: itest), - skip: b <= u8::MAX as itest, - cases: [ - (itest::MIN, -1i8), - (0, 0) - ] - } - test_bignum! { - function: ::checked_neg(a: itest), - cases: [ - (itest::MIN) - ] - } - test_bignum! { - function: ::checked_shl(a: itest, b: u16) - } - test_bignum! { - function: ::checked_shr(a: itest, b: u16) - } - test_bignum! { - function: ::checked_pow(a: itest, b: u16), - cases: [ - (2, itest::BITS as u16 - 1), - (-2, itest::BITS as u16 - 1) - ] - } - test_bignum! { - function: ::checked_ilog2(a: itest) - } - test_bignum! { - function: ::checked_ilog10(a: itest) - } - test_bignum! { - function: ::checked_ilog(a: itest, b: itest) - } - } - } - }; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::test_bignum; + use crate::test::types::{itest, utest}; + + test_bignum! { + function: ::checked_add(a: itest, b: itest), + cases: [ + (itest::MAX, -1i8) + ] + } + test_bignum! { + function: ::checked_add_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::checked_sub(a: itest, b: itest), + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::checked_sub_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::checked_mul(a: itest, b: itest), + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::checked_div(a: itest, b: itest), + cases: [ + (0i8, 0i8), + (23098403i32 as itest, 0i8), + (itest::MIN, -1i8), + (8388600i32 as itest, 68201i32 as itest) // tests the unlikely condition in the division algorithm at step D5 + ] + } + test_bignum! { + function: ::checked_div_euclid(a: itest, b: itest), + cases: [ + (itest::MIN, -1i8), + (0i8, 0i8) + ] + } + test_bignum! { + function: ::checked_rem(a: itest, b: itest), + cases: [ + (itest::MIN, -1i8), + (0i8, 0i8) + ] + } + test_bignum! { + function: ::checked_rem_euclid(a: itest, b: itest), + skip: b <= u8::MAX as itest, + cases: [ + (itest::MIN, -1i8), + (0i8, 0i8) + ] + } + test_bignum! { + function: ::checked_neg(a: itest), + cases: [ + (itest::MIN) + ] + } + test_bignum! { + function: ::checked_shl(a: itest, b: u16) + } + test_bignum! { + function: ::checked_shr(a: itest, b: u16) + } + test_bignum! { + function: ::checked_pow(a: itest, b: u16), + cases: [ + (2i8, itest::BITS as u16 - 1), + (-2i8, itest::BITS as u16 - 1) + ] + } + test_bignum! { + function: ::checked_ilog2(a: itest) + } + test_bignum! { + function: ::checked_ilog10(a: itest) + } + test_bignum! { + function: ::checked_ilog(a: itest, b: itest) + } } crate::macro_impl!(checked); diff --git a/src/bint/cmp.rs b/src/bint/cmp.rs index 467923e..4b304e6 100644 --- a/src/bint/cmp.rs +++ b/src/bint/cmp.rs @@ -30,15 +30,14 @@ macro_rules! cmp { Self::clamp(self, min, max) } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::cmp::tests!(itest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::*; + + crate::int::cmp::tests!(itest); +} + crate::macro_impl!(cmp); diff --git a/src/bint/convert.rs b/src/bint/convert.rs index c9d93e8..55530da 100644 --- a/src/bint/convert.rs +++ b/src/bint/convert.rs @@ -161,36 +161,34 @@ macro_rules! convert { // } // } // } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{self, types::itest}; - use crate::test::cast_types::*; - use crate::BTryFrom; - - test::test_btryfrom!(itest; UTESTD8, UTESTD16, UTESTD32, UTESTD64, TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, ITESTD8, ITESTD16, ITESTD32, ITESTD64, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10); - - #[cfg(test_int_bits = "128")] - test::test_from! { - function: ::try_from, - from_types: (i8, i16, i32, i64, i128, u8, u16, u32, u64, bool, usize, isize) - } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test; + use crate::test::types::itest; + use crate::test::cast_types::*; + use crate::BTryFrom; + + test::test_btryfrom!(itest; TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10); + + #[cfg(test_int_bits = "128")] + test::test_from! { + function: ::try_from, + from_types: (i8, i16, i32, i64, i128, u8, u16, u32, u64, bool, usize, isize) + } - #[cfg(test_int_bits = "64")] - test::test_from! { - function: ::try_from, - from_types: (i8, i16, i32, i64, u8, u16, u32, bool, isize) - } + #[cfg(test_int_bits = "64")] + test::test_from! { + function: ::try_from, + from_types: (i8, i16, i32, i64, u8, u16, u32, bool, isize) + } - test::test_into! { - function: ::try_into, - into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize) - } - } - } - }; + test::test_into! { + function: ::try_into, + into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize) + } } crate::macro_impl!(convert); diff --git a/src/bint/endian.rs b/src/bint/endian.rs index f8f084f..7d37548 100644 --- a/src/bint/endian.rs +++ b/src/bint/endian.rs @@ -218,18 +218,15 @@ macro_rules! endian { Self::from_bits($BUint::from_ne_bytes(bytes)) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::test_bignum; - use crate::test::types::itest; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::test_bignum; + use crate::test::types::itest; - crate::int::endian::tests!($Digit; itest); - } - } - }; + crate::int::endian::tests!(itest); } crate::macro_impl!(endian); diff --git a/src/bint/fmt.rs b/src/bint/fmt.rs index b91a008..0138b2f 100644 --- a/src/bint/fmt.rs +++ b/src/bint/fmt.rs @@ -48,15 +48,12 @@ macro_rules! fmt { } fmt_trait!($BInt, UpperHex); - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::fmt::tests!(itest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::fmt::tests!(itest); +} + crate::macro_impl!(fmt); diff --git a/src/bint/mod.rs b/src/bint/mod.rs index 74e6e0f..5f019c5 100644 --- a/src/bint/mod.rs +++ b/src/bint/mod.rs @@ -413,100 +413,98 @@ macro_rules! mod_impl { Self::from_bits(<$BUint:: as quickcheck::Arbitrary>::arbitrary(g)) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::{ - debug_skip, test_bignum, - types::itest, - }; - use crate::test::types::big_types::$Digit::*; - - crate::int::tests!(itest); - - test_bignum! { - function: ::unsigned_abs(a: itest), - cases: [ - (itest::MIN), - (0 as itest) - ] - } - test_bignum! { - function: ::abs(a: itest), - skip: debug_skip!(a == itest::MIN) - } - test_bignum! { - function: ::signum(a: itest) - } - test_bignum! { - function: ::is_positive(a: itest) - } - test_bignum! { - function: ::is_negative(a: itest) - } - test_bignum! { - function: ::cast_unsigned(a: itest) - } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{ + debug_skip, test_bignum, + types::itest, + }; + // use crate::test::types::big_types::$Digit::*; - #[test] - fn bit() { - let i = ITEST::from(0b1001010100101010101i64); - assert!(i.bit(2)); - assert!(!i.bit(3)); - assert!(i.bit(8)); - assert!(!i.bit(9)); - assert!(i.bit(i.bits() - 1)); - } + crate::int::tests!(itest); - #[test] - fn is_zero() { - assert!(ITEST::ZERO.is_zero()); - assert!(!ITEST::MAX.is_zero()); - assert!(!ITEST::ONE.is_zero()); - } + test_bignum! { + function: ::unsigned_abs(a: itest), + cases: [ + (itest::MIN), + (0 as itest) + ] + } + test_bignum! { + function: ::abs(a: itest), + skip: debug_skip!(a == itest::MIN) + } + test_bignum! { + function: ::signum(a: itest) + } + test_bignum! { + function: ::is_positive(a: itest) + } + test_bignum! { + function: ::is_negative(a: itest) + } + test_bignum! { + function: ::cast_unsigned(a: itest) + } - #[test] - fn is_one() { - assert!(ITEST::ONE.is_one()); - assert!(!ITEST::MAX.is_one()); - assert!(!ITEST::ZERO.is_one()); - } + #[test] + fn bit() { + let i = ITEST::from(0b10100101010101i16); + assert!(i.bit(2)); + assert!(!i.bit(3)); + assert!(i.bit(8)); + assert!(!i.bit(9)); + assert!(i.bit(i.bits() - 1)); + } - #[test] - fn bits() { - let u = ITEST::from(0b11101001010100101010101i32); - assert_eq!(u.bits(), 23); - } + #[test] + fn is_zero() { + assert!(ITEST::ZERO.is_zero()); + assert!(!ITEST::MAX.is_zero()); + assert!(!ITEST::ONE.is_zero()); + } - #[test] - fn default() { - assert_eq!(ITEST::default(), itest::default().into()); - } + #[test] + fn is_one() { + assert!(ITEST::ONE.is_one()); + assert!(!ITEST::MAX.is_one()); + assert!(!ITEST::ZERO.is_one()); + } - #[test] - fn is_power_of_two() { - assert!(!ITEST::from(-94956729465i64).is_power_of_two()); - assert!(!ITEST::from(79458945i32).is_power_of_two()); - assert!(ITEST::from(1i32 << 17).is_power_of_two()); - } + #[test] + fn bits() { + let u = ITEST::from(0b10100101010101i16); + assert_eq!(u.bits(), 14); + } - #[test] - fn sum() { - let v = vec![&ITEST::ZERO, &ITEST::ONE, &ITEST::TWO, &ITEST::THREE, &ITEST::FOUR]; - assert_eq!(ITEST::TEN, v.iter().copied().sum()); - assert_eq!(ITEST::TEN, v.into_iter().sum()); - } + #[test] + fn default() { + assert_eq!(ITEST::default(), itest::default().into()); + } - #[test] - fn product() { - let v = vec![&ITEST::ONE, &ITEST::TWO, &ITEST::THREE]; - assert_eq!(ITEST::SIX, v.iter().copied().sum()); - assert_eq!(ITEST::SIX, v.into_iter().sum()); - } - } - } - }; + #[test] + fn is_power_of_two() { + assert!(!ITEST::from(-1273i16).is_power_of_two()); + assert!(!ITEST::from(8945i16).is_power_of_two()); + assert!(ITEST::from(1i16 << 14).is_power_of_two()); + } + + #[test] + fn sum() { + let v = vec![&ITEST::ZERO, &ITEST::ONE, &ITEST::TWO, &ITEST::THREE, &ITEST::FOUR]; + assert_eq!(ITEST::TEN, v.iter().copied().sum()); + assert_eq!(ITEST::TEN, v.into_iter().sum()); + } + + #[test] + fn product() { + let v = vec![&ITEST::ONE, &ITEST::TWO, &ITEST::THREE]; + assert_eq!(ITEST::SIX, v.iter().copied().sum()); + assert_eq!(ITEST::SIX, v.into_iter().sum()); + } } crate::macro_impl!(mod_impl); diff --git a/src/bint/numtraits.rs b/src/bint/numtraits.rs index 5c50d65..c71a8f5 100644 --- a/src/bint/numtraits.rs +++ b/src/bint/numtraits.rs @@ -369,17 +369,14 @@ macro_rules! numtraits { self.signed_digit().is_negative() } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::types::itest; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::itest; - crate::int::numtraits::tests!(itest); - } - } - }; + crate::int::numtraits::tests!(itest); } crate::macro_impl!(numtraits); \ No newline at end of file diff --git a/src/bint/ops.rs b/src/bint/ops.rs index d792bf4..3db2bc4 100644 --- a/src/bint/ops.rs +++ b/src/bint/ops.rs @@ -78,23 +78,18 @@ macro_rules! ops { } crate::int::ops::impls!($BInt, $BUint, $BInt); + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use super::*; - use crate::test::{debug_skip, test_bignum, types::itest}; - use crate::test::types::big_types::$Digit::*; - - crate::int::ops::tests!(itest); +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{debug_skip, test_bignum, types::itest}; + use core::ops::Neg; - test_bignum! { - function: ::neg(a: itest), - skip: debug_skip!(a == itest::MIN) - } - } - } - }; + test_bignum! { + function: ::neg(a: itest), + skip: debug_skip!(a == itest::MIN) + } } crate::macro_impl!(ops); diff --git a/src/bint/overflowing.rs b/src/bint/overflowing.rs index 5f252b9..ba6c21a 100644 --- a/src/bint/overflowing.rs +++ b/src/bint/overflowing.rs @@ -274,77 +274,74 @@ macro_rules! overflowing { (out, overflow) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::{test_bignum, types::itest}; - use crate::test::types::big_types::$Digit::*; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::itest}; - test_bignum! { - function: ::overflowing_add(a: itest, b: itest) - } - test_bignum! { - function: ::overflowing_sub(a: itest, b: itest) - } - test_bignum! { - function: ::overflowing_mul(a: itest, b: itest) - } - test_bignum! { - function: ::overflowing_div(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8), - (itest::MIN, 1i8) - ] - } - test_bignum! { - function: ::overflowing_div_euclid(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::overflowing_rem(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::overflowing_rem_euclid(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::overflowing_neg(a: itest), - cases: [ - (0i8), - (itest::MIN) - ] - } - test_bignum! { - function: ::overflowing_shl(a: itest, b: u16) - } - test_bignum! { - function: ::overflowing_shr(a: itest, b: u16) - } - test_bignum! { - function: ::overflowing_abs(a: itest), - cases: [ - (0i8), - (itest::MIN) - ] - } - test_bignum! { - function: ::overflowing_pow(a: itest, b: u16) - } - } - } - }; + test_bignum! { + function: ::overflowing_add(a: itest, b: itest) + } + test_bignum! { + function: ::overflowing_sub(a: itest, b: itest) + } + test_bignum! { + function: ::overflowing_mul(a: itest, b: itest) + } + test_bignum! { + function: ::overflowing_div(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8), + (itest::MIN, 1i8) + ] + } + test_bignum! { + function: ::overflowing_div_euclid(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::overflowing_rem(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::overflowing_rem_euclid(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::overflowing_neg(a: itest), + cases: [ + (0i8), + (itest::MIN) + ] + } + test_bignum! { + function: ::overflowing_shl(a: itest, b: u16) + } + test_bignum! { + function: ::overflowing_shr(a: itest, b: u16) + } + test_bignum! { + function: ::overflowing_abs(a: itest), + cases: [ + (0i8), + (itest::MIN) + ] + } + test_bignum! { + function: ::overflowing_pow(a: itest, b: u16) + } } crate::macro_impl!(overflowing); diff --git a/src/bint/radix.rs b/src/bint/radix.rs index 820a5f7..3d5c491 100644 --- a/src/bint/radix.rs +++ b/src/bint/radix.rs @@ -162,138 +162,135 @@ macro_rules! radix { self.bits.to_radix_le(radix) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{quickcheck_from_to_radix, test_bignum, self}; - use crate::$BInt; - use crate::test::types::itest; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{quickcheck_from_to_radix, test_bignum, self}; + use crate::test::types::itest; + use crate::BInt; - test_bignum! { - function: ::from_str_radix, - cases: [ - ("-14359abcasdhfkdgdfgsde", 34u32), - ("+23797984569ahgkhhjdskjdfiu", 32u32), - ("-253613132341435345", 7u32), - ("+23467abcad47790809ef37", 16u32), - ("-712930769245766867875986646", 10u32), - ("-😱234292", 36u32), - ("-+345934758", 13u32), - ("12💯12", 15u32), - ("gap gap", 36u32), - ("-9223372036854775809", 10u32), - ("-1000000000000000000001", 8u32), - ("+1000000000000000000001", 8u32), - ("-8000000000000001", 16u32), - ("+-23459374", 15u32), - ("8000000000000000", 16u32), - ("", 10u32) - ] - } + test_bignum! { + function: ::from_str_radix, + cases: [ + ("-14359abcasdhfkdgdfgsde", 34u32), + ("+23797984569ahgkhhjdskjdfiu", 32u32), + ("-253613132341435345", 7u32), + ("+23467abcad47790809ef37", 16u32), + ("-712930769245766867875986646", 10u32), + ("-😱234292", 36u32), + ("-+345934758", 13u32), + ("12💯12", 15u32), + ("gap gap", 36u32), + ("-9223372036854775809", 10u32), + ("-1000000000000000000001", 8u32), + ("+1000000000000000000001", 8u32), + ("-8000000000000001", 16u32), + ("+-23459374", 15u32), + ("8000000000000000", 16u32), + ("", 10u32) + ] + } - quickcheck_from_to_radix!(itest, radix_be, 256); - quickcheck_from_to_radix!(itest, radix_le, 256); - quickcheck_from_to_radix!(itest, str_radix, 36); + quickcheck_from_to_radix!(itest, radix_be, 256); + quickcheck_from_to_radix!(itest, radix_le, 256); + quickcheck_from_to_radix!(itest, str_radix, 36); - test::quickcheck_from_str_radix!(itest, "+" | "-"); - test::quickcheck_from_str!(itest); + test::quickcheck_from_str_radix!(itest, "+" | "-"); + test::quickcheck_from_str!(itest); - #[test] - fn from_to_radix_le() { - let buf = &[ - 61, 45, 48, 20, 37, 59, 53, 28, 28, 52, 54, 13, 44, 3, 46, 42, 20, 46, 37, 32, - 13, 27, 47, 30, 33, 25, 3, 32, 4, 54, 53, 6, 44, 25, 10, 22, 33, 48, 7, 17, - ]; - let u = $BInt::<100>::from_radix_le(buf, 64).unwrap(); - let v = u.to_radix_le(64); - assert_eq!(v, buf); + #[test] + fn from_to_radix_le() { + let buf = &[ + 61, 45, 48, 20, 37, 59, 53, 28, 28, 52, 54, 13, 44, 3, 46, 42, 20, 46, 37, 32, + 13, 27, 47, 30, 33, 25, 3, 32, 4, 54, 53, 6, 44, 25, 10, 22, 33, 48, 7, 17, + ]; + let u = BInt::<100>::from_radix_le(buf, 64).unwrap(); + let v = u.to_radix_le(64); + assert_eq!(v, buf); - let buf = &[ - 33, 34, 61, 53, 74, 67, 54, 62, 22, 29, 4, 2, 43, 73, 74, 24, 8, 74, 65, 3, 78, - ]; - let option = $BInt::<100>::from_radix_le(buf, 78); - assert!(option.is_none()); + let buf = &[ + 33, 34, 61, 53, 74, 67, 54, 62, 22, 29, 4, 2, 43, 73, 74, 24, 8, 74, 65, 3, 78, + ]; + let option = BInt::<100>::from_radix_le(buf, 78); + assert!(option.is_none()); - let buf = &[ - 1, 3, 3, 0, 2, 1, 2, 3, 0, 4, 1, 2, 0, 0, 0, 0, 3, 2, 0, 1, 0, 4, 1, 3, 1, 4, - 3, 3, 3, 4, 1, 2, 2, 1, 3, 0, 2, 1, 2, 3, 1, 1, 0, 2, 2, 1, 1, 2, 1, 0, 0, 0, - 3, 3, 3, 0, 0, 4, 4, 2, - ]; - let u = $BInt::<100>::from_radix_le(buf, 5).unwrap(); - let v = u.to_radix_le(5); - assert_eq!(v, buf); - } - #[test] - fn from_to_radix_be() { - let buf = &[ - 29, 89, 92, 118, 69, 140, 141, 70, 71, 76, 66, 13, 30, 28, 38, 145, 40, 7, 57, - 18, 25, 65, 150, 119, 155, 18, 64, 76, 106, 87, - ]; - let u = $BInt::<100>::from_radix_be(buf, 157).unwrap(); - let v = u.to_radix_be(157); - assert_eq!(v, buf); + let buf = &[ + 1, 3, 3, 0, 2, 1, 2, 3, 0, 4, 1, 2, 0, 0, 0, 0, 3, 2, 0, 1, 0, 4, 1, 3, 1, 4, + 3, 3, 3, 4, 1, 2, 2, 1, 3, 0, 2, 1, 2, 3, 1, 1, 0, 2, 2, 1, 1, 2, 1, 0, 0, 0, + 3, 3, 3, 0, 0, 4, 4, 2, + ]; + let u = BInt::<100>::from_radix_le(buf, 5).unwrap(); + let v = u.to_radix_le(5); + assert_eq!(v, buf); + } + #[test] + fn from_to_radix_be() { + let buf = &[ + 29, 89, 92, 118, 69, 140, 141, 70, 71, 76, 66, 13, 30, 28, 38, 145, 40, 7, 57, + 18, 25, 65, 150, 119, 155, 18, 64, 76, 106, 87, + ]; + let u = BInt::<100>::from_radix_be(buf, 157).unwrap(); + let v = u.to_radix_be(157); + assert_eq!(v, buf); - let buf = &[ - 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, - 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, - 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, - 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, - ]; - let u = $BInt::<100>::from_radix_be(buf, 2).unwrap(); - let v = u.to_radix_be(2); - assert_eq!(v, buf); + let buf = &[ + 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, + 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, + 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, + ]; + let u = BInt::<100>::from_radix_be(buf, 2).unwrap(); + let v = u.to_radix_be(2); + assert_eq!(v, buf); - let buf = &[ - 91, 167, 5, 99, 61, 38, 158, 149, 115, 79, 13, 118, 53, 16, 144, 123, 70, 81, - 78, 61, 39, 6, 34, 95, 98, 23, 175, 182, - ]; - let option = $BInt::<100>::from_radix_le(buf, 180); - assert!(option.is_none()); + let buf = &[ + 91, 167, 5, 99, 61, 38, 158, 149, 115, 79, 13, 118, 53, 16, 144, 123, 70, 81, + 78, 61, 39, 6, 34, 95, 98, 23, 175, 182, + ]; + let option = BInt::<100>::from_radix_le(buf, 180); + assert!(option.is_none()); - let buf = &[ - 39, 90, 119, 93, 95, 7, 70, 81, 3, 100, 39, 107, 98, 31, 61, 5, 36, 19, 18, - 124, 4, 77, 119, 17, 121, 116, 24, 35, - ]; - let u = $BInt::<100>::from_radix_be(buf, 128).unwrap(); - let v = u.to_radix_be(128); - assert_eq!(v, buf); - } - #[test] - fn from_to_str_radix() { - let src = "-293487598aashkhkhakb8345cbvjkus"; - let u = $BInt::<100>::from_str_radix(src, 35).unwrap(); - let v = u.to_str_radix(35); - assert_eq!(v, src); + let buf = &[ + 39, 90, 119, 93, 95, 7, 70, 81, 3, 100, 39, 107, 98, 31, 61, 5, 36, 19, 18, + 124, 4, 77, 119, 17, 121, 116, 24, 35, + ]; + let u = BInt::<100>::from_radix_be(buf, 128).unwrap(); + let v = u.to_radix_be(128); + assert_eq!(v, buf); + } + #[test] + fn from_to_str_radix() { + let src = "-293487598aashkhkhakb8345cbvjkus"; + let u = BInt::<100>::from_str_radix(src, 35).unwrap(); + let v = u.to_str_radix(35); + assert_eq!(v, src); - let src = "zzzzzzzzzzzzzzzzzzzzzzzzz"; - let result = $BInt::<1>::from_str_radix(src, 36); - assert!(result.is_err()); + let src = "zzzzzzzzzzzzzzzzzzzzzzzzz"; + let result = BInt::<1>::from_str_radix(src, 36); + assert!(result.is_err()); - let invalid = "inval_id string"; - let result = $BInt::<1>::from_str_radix(invalid, 36); - assert!(result.is_err()); + let invalid = "inval_id string"; + let result = BInt::<1>::from_str_radix(invalid, 36); + assert!(result.is_err()); - let src = "72954hslfhbui79845y6audfgiu984h5ihhhdfg"; - let u = $BInt::<100>::from_str_radix(src, 36).unwrap(); - assert_eq!(u.to_str_radix(36), src); - } - #[test] - fn parse_bytes() { - let src = "1797972456987acbdead7889"; - let u = $BInt::<100>::parse_bytes(src.as_bytes(), 16).unwrap(); - let v = $BInt::<100>::from_str_radix(src, 16).unwrap(); - assert_eq!(u, v); - assert_eq!(v.to_str_radix(16), src); + let src = "72954hslfhbui79845y6audfgiu984h5ihhhdfg"; + let u = BInt::<100>::from_str_radix(src, 36).unwrap(); + assert_eq!(u.to_str_radix(36), src); + } + #[test] + fn parse_bytes() { + let src = "1797972456987acbdead7889"; + let u = BInt::<100>::parse_bytes(src.as_bytes(), 16).unwrap(); + let v = BInt::<100>::from_str_radix(src, 16).unwrap(); + assert_eq!(u, v); + assert_eq!(v.to_str_radix(16), src); - let bytes = b"279874657dgfhjh"; - let option = $BInt::<100>::parse_bytes(bytes, 11); - assert!(option.is_none()); - } - } - } - }; + let bytes = b"279874657dgfhjh"; + let option = BInt::<100>::parse_bytes(bytes, 11); + assert!(option.is_none()); + } } crate::macro_impl!(radix); diff --git a/src/bint/saturating.rs b/src/bint/saturating.rs index e961427..541cff3 100644 --- a/src/bint/saturating.rs +++ b/src/bint/saturating.rs @@ -120,53 +120,50 @@ macro_rules! saturating { } } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::*}; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::*}; - test_bignum! { - function: ::saturating_add(a: itest, b: itest) - } - test_bignum! { - function: ::saturating_add_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::saturating_sub(a: itest, b: itest) - } - test_bignum! { - function: ::saturating_sub_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::saturating_div(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8) - ] - } - test_bignum! { - function: ::saturating_neg(a: itest), - cases: [ - (itest::MIN) - ] - } - test_bignum! { - function: ::saturating_abs(a: itest), - cases: [ - (itest::MIN) - ] - } - test_bignum! { - function: ::saturating_mul(a: itest, b: itest) - } - test_bignum! { - function: ::saturating_pow(a: itest, b: u16) - } - } - } - }; + test_bignum! { + function: ::saturating_add(a: itest, b: itest) + } + test_bignum! { + function: ::saturating_add_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::saturating_sub(a: itest, b: itest) + } + test_bignum! { + function: ::saturating_sub_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::saturating_div(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8) + ] + } + test_bignum! { + function: ::saturating_neg(a: itest), + cases: [ + (itest::MIN) + ] + } + test_bignum! { + function: ::saturating_abs(a: itest), + cases: [ + (itest::MIN) + ] + } + test_bignum! { + function: ::saturating_mul(a: itest, b: itest) + } + test_bignum! { + function: ::saturating_pow(a: itest, b: u16) + } } crate::macro_impl!(saturating); diff --git a/src/bint/strict.rs b/src/bint/strict.rs index 02420ea..067e973 100644 --- a/src/bint/strict.rs +++ b/src/bint/strict.rs @@ -34,28 +34,25 @@ macro_rules! strict { ) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::strict::tests!(itest); +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::strict::tests!(itest); - test_bignum! { - function: ::strict_abs(a: itest), - skip: a.checked_abs().is_none() - } - test_bignum! { - function: ::strict_add_unsigned(a: itest, b: utest), - skip: a.checked_add_unsigned(b).is_none() - } - test_bignum! { - function: ::strict_sub_unsigned(a: itest, b: utest), - skip: a.checked_sub_unsigned(b).is_none() - } - } - } - }; + test_bignum! { + function: ::strict_abs(a: itest), + skip: a.checked_abs().is_none() + } + test_bignum! { + function: ::strict_add_unsigned(a: itest, b: utest), + skip: a.checked_add_unsigned(b).is_none() + } + test_bignum! { + function: ::strict_sub_unsigned(a: itest, b: utest), + skip: a.checked_sub_unsigned(b).is_none() + } } use crate::doc; diff --git a/src/bint/unchecked.rs b/src/bint/unchecked.rs index b82318f..c5f5fea 100644 --- a/src/bint/unchecked.rs +++ b/src/bint/unchecked.rs @@ -1,17 +1,14 @@ macro_rules! unchecked { ($BUint: ident, $BInt: ident, $Digit: ident) => { crate::int::unchecked::impls!($BInt, I); - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::unchecked::tests!(itest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::unchecked::tests!(itest); +} + use crate::doc; crate::macro_impl!(unchecked); diff --git a/src/bint/wrapping.rs b/src/bint/wrapping.rs index b0940e7..85e8ead 100644 --- a/src/bint/wrapping.rs +++ b/src/bint/wrapping.rs @@ -103,69 +103,66 @@ macro_rules! wrapping { Self::from_bits(self.bits.wrapping_pow(pow)) } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::{itest, utest}}; - - test_bignum! { - function: ::wrapping_add(a: itest, b: itest) - } - test_bignum! { - function: ::wrapping_add_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::wrapping_sub(a: itest, b: itest) - } - test_bignum! { - function: ::wrapping_sub_unsigned(a: itest, b: utest) - } - test_bignum! { - function: ::wrapping_mul(a: itest, b: itest) - } - test_bignum! { - function: ::wrapping_div(a: itest, b: itest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_div_euclid(a: itest, b: itest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_rem(a: itest, b: itest), - skip: b == 0, - cases: [ - (itest::MIN, -1i8), - (185892231884832768i64 as itest, 92946115942416385i64 as itest) - ] - } - test_bignum! { - function: ::wrapping_rem_euclid(a: itest, b: itest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_neg(a: itest), - cases: [ - (itest::MIN) - ] - } - test_bignum! { - function: ::wrapping_shl(a: itest, b: u16) - } - test_bignum! { - function: ::wrapping_shr(a: itest, b: u16) - } - test_bignum! { - function: ::wrapping_abs(a: itest) - } - test_bignum! { - function: ::wrapping_pow(a: itest, b: u16) - } - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::{itest, utest}}; + + test_bignum! { + function: ::wrapping_add(a: itest, b: itest) + } + test_bignum! { + function: ::wrapping_add_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::wrapping_sub(a: itest, b: itest) + } + test_bignum! { + function: ::wrapping_sub_unsigned(a: itest, b: utest) + } + test_bignum! { + function: ::wrapping_mul(a: itest, b: itest) + } + test_bignum! { + function: ::wrapping_div(a: itest, b: itest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_div_euclid(a: itest, b: itest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_rem(a: itest, b: itest), + skip: b == 0, + cases: [ + (itest::MIN, -1i8), + (185892231884832768i64 as itest, 92946115942416385i64 as itest) + ] + } + test_bignum! { + function: ::wrapping_rem_euclid(a: itest, b: itest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_neg(a: itest), + cases: [ + (itest::MIN) + ] + } + test_bignum! { + function: ::wrapping_shl(a: itest, b: u16) + } + test_bignum! { + function: ::wrapping_shr(a: itest, b: u16) + } + test_bignum! { + function: ::wrapping_abs(a: itest) + } + test_bignum! { + function: ::wrapping_pow(a: itest, b: u16) + } +} + crate::macro_impl!(wrapping); diff --git a/src/buint/bigint_helpers.rs b/src/buint/bigint_helpers.rs index 54bf1bc..507a873 100644 --- a/src/buint/bigint_helpers.rs +++ b/src/buint/bigint_helpers.rs @@ -57,34 +57,29 @@ macro_rules! bigint_helpers { } } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - // use crate::test::{test_bignum, types::*}; - use crate::test::types::big_types::$Digit::*; - - type U64 = crate::$BUint::<{64 / $Digit::BITS as usize}>; - - crate::int::bigint_helpers::tests!(utest); +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::bigint_helpers::tests!(utest); - test_bignum! { - function: ::widening_mul(a: u64, b: u64), - cases: [ - (u64::MAX, u64::MAX) - ] - } + #[cfg(test_int_bits = "64")] + test_bignum! { + function: ::widening_mul(a: utest, b: utest), + cases: [ + (utest::MAX, utest::MAX) + ] + } - test_bignum! { - function: ::carrying_mul(a: u64, b: u64, c: u64), - cases: [ - (u64::MAX, u64::MAX, u64::MAX), - (u64::MAX, u64::MAX, 1u64) - ] - } - } - } - }; + #[cfg(test_int_bits = "64")] + test_bignum! { + function: ::carrying_mul(a: utest, b: utest, c: utest), + cases: [ + (utest::MAX, utest::MAX, utest::MAX), + (utest::MAX, utest::MAX, 1 as utest) + ] + } } crate::macro_impl!(bigint_helpers); diff --git a/src/buint/cast.rs b/src/buint/cast.rs index 8c1b9e3..57b4367 100644 --- a/src/buint/cast.rs +++ b/src/buint/cast.rs @@ -90,28 +90,28 @@ use crate::cast::float::{FloatMantissa, CastUintFromFloatHelper, CastFloatFromUi macro_rules! cast { ($BUint: ident, $BInt: ident, $Digit: ident) => { - #[cfg(feature = "float")] - impl FloatMantissa for $BUint { - const ZERO: Self = Self::ZERO; - const ONE: Self = Self::ONE; - const TWO: Self = Self::TWO; - const MAX: Self = Self::MAX; + // #[cfg(feature = "float")] + // impl FloatMantissa for $BUint { + // const ZERO: Self = Self::ZERO; + // const ONE: Self = Self::ONE; + // const TWO: Self = Self::TWO; + // const MAX: Self = Self::MAX; - #[inline] - fn leading_zeros(self) -> ExpType { - Self::leading_zeros(self) - } + // #[inline] + // fn leading_zeros(self) -> ExpType { + // Self::leading_zeros(self) + // } - #[inline] - fn checked_shr(self, n: ExpType) -> Option { - Self::checked_shr(self, n) - } + // #[inline] + // fn checked_shr(self, n: ExpType) -> Option { + // Self::checked_shr(self, n) + // } - #[inline] - fn is_power_of_two(self) -> bool { - Self::is_power_of_two(self) - } - } + // #[inline] + // fn is_power_of_two(self) -> bool { + // Self::is_power_of_two(self) + // } + // } impl CastUintFromFloatHelper for $BUint { const MAX: Self = Self::MAX; @@ -220,17 +220,14 @@ macro_rules! cast { crate::cast::float::cast_uint_from_float(value) } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::cast::tests!(utest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::cast::tests!(utest); +} + crate::macro_impl!(cast); macro_rules! buint_as_different_digit_bigint { diff --git a/src/buint/checked.rs b/src/buint/checked.rs index 06daca0..3ba7a7a 100644 --- a/src/buint/checked.rs +++ b/src/buint/checked.rs @@ -267,78 +267,75 @@ macro_rules! checked { Some(Self::power_of_two(bits)) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::*}; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::*}; - test_bignum! { - function: ::checked_add(a: utest, b: utest), - cases: [ - (utest::MAX, 1u8) - ] - } - test_bignum! { - function: ::checked_add_signed(a: utest, b: itest) - } - test_bignum! { - function: ::checked_sub(a: utest, b: utest) - } - test_bignum! { - function: ::checked_mul(a: utest, b: utest) - } - test_bignum! { - function: ::checked_div(a: utest, b: utest), - cases: [ - (328622u32 as utest, 10000u32 as utest), // tests the unlikely condition in the division algorithm at step D5 - (2074086u32 as utest, 76819u32 as utest) // tests the unlikely condition in the division algorithm at step D5 - ] - } - test_bignum! { - function: ::checked_div_euclid(a: utest, b: utest) - } - test_bignum! { - function: ::checked_rem(a: utest, b: utest) - } - test_bignum! { - function: ::checked_rem_euclid(a: utest, b: utest) - } - test_bignum! { - function: ::checked_neg(a: utest) - } - test_bignum! { - function: ::checked_shl(a: utest, b: u16) - } - test_bignum! { - function: ::checked_shr(a: utest, b: u16) - } - test_bignum! { - function: ::checked_pow(a: utest, b: u16) - } - test_bignum! { - function: ::checked_ilog(a: utest, b: utest), - cases: [ - (2u8, 60u8), - (utest::MAX, 2u8) - ] - } - test_bignum! { - function: ::checked_ilog2(a: utest) - } - test_bignum! { - function: ::checked_ilog10(a: utest) - } - test_bignum! { - function: ::checked_next_power_of_two(a: utest), - cases: [ - (utest::MAX) - ] - } - } - } - }; + test_bignum! { + function: ::checked_add(a: utest, b: utest), + cases: [ + (utest::MAX, 1u8) + ] + } + test_bignum! { + function: ::checked_add_signed(a: utest, b: itest) + } + test_bignum! { + function: ::checked_sub(a: utest, b: utest) + } + test_bignum! { + function: ::checked_mul(a: utest, b: utest) + } + test_bignum! { + function: ::checked_div(a: utest, b: utest), + cases: [ + (328622u32 as utest, 10000u32 as utest), // tests the unlikely condition in the division algorithm at step D5 + (2074086u32 as utest, 76819u32 as utest) // tests the unlikely condition in the division algorithm at step D5 + ] + } + test_bignum! { + function: ::checked_div_euclid(a: utest, b: utest) + } + test_bignum! { + function: ::checked_rem(a: utest, b: utest) + } + test_bignum! { + function: ::checked_rem_euclid(a: utest, b: utest) + } + test_bignum! { + function: ::checked_neg(a: utest) + } + test_bignum! { + function: ::checked_shl(a: utest, b: u16) + } + test_bignum! { + function: ::checked_shr(a: utest, b: u16) + } + test_bignum! { + function: ::checked_pow(a: utest, b: u16) + } + test_bignum! { + function: ::checked_ilog(a: utest, b: utest), + cases: [ + (2u8, 60u8), + (utest::MAX, 2u8) + ] + } + test_bignum! { + function: ::checked_ilog2(a: utest) + } + test_bignum! { + function: ::checked_ilog10(a: utest) + } + test_bignum! { + function: ::checked_next_power_of_two(a: utest), + cases: [ + (utest::MAX) + ] + } } crate::macro_impl!(checked); diff --git a/src/buint/cmp.rs b/src/buint/cmp.rs index da21cf4..1221bde 100644 --- a/src/buint/cmp.rs +++ b/src/buint/cmp.rs @@ -30,15 +30,12 @@ macro_rules! cmp { Self::clamp(self, min, max) } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::cmp::tests!(utest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::cmp::tests!(utest); +} + crate::macro_impl!(cmp); diff --git a/src/buint/convert.rs b/src/buint/convert.rs index 7f5f4c7..1f4ad60 100644 --- a/src/buint/convert.rs +++ b/src/buint/convert.rs @@ -226,29 +226,27 @@ macro_rules! convert { uint.digits } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{self, types::utest}; - use crate::test::cast_types::*; - use super::BTryFrom; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{self, types::utest}; + use crate::test::cast_types::*; + use super::BTryFrom; - test::test_btryfrom!(utest; UTESTD8, UTESTD16, UTESTD32, UTESTD64, TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, ITESTD8, ITESTD16, ITESTD32, ITESTD64, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10/*, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize*/); + test::test_btryfrom!(utest; TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10/*, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize*/); - test::test_from! { - function: ::try_from, - from_types: (u8, u16, u32, u64, bool, char, i8, i16, i32, i64, isize, usize) // TODO: when we can use TryFrom for conversions between bnum ints, we can just add the list of test types here, same as in the casting tests - } + #[cfg(not(any(test_int_bits = "16", test_int_bits = "32")))] // TODO: due to incorrectly implementing From instead of TryFrom for small bnum ints + test::test_from! { + function: ::try_from, + from_types: (u8, u16, u32, u64, bool, char, i8, i16, i32, i64, isize, usize) // TODO: when we can use TryFrom for conversions between bnum ints, we can just add the list of test types here, same as in the casting tests + } - test::test_into! { - function: ::try_into, - into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize) - } - } - } - }; + test::test_into! { + function: ::try_into, + into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize) + } } crate::macro_impl!(convert); diff --git a/src/buint/endian.rs b/src/buint/endian.rs index 71936d9..f3d96ad 100644 --- a/src/buint/endian.rs +++ b/src/buint/endian.rs @@ -287,17 +287,14 @@ macro_rules! endian { pub(crate) const BYTES_USIZE: usize = N * digit::$Digit::BYTES as usize; } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::utest}; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::utest}; - crate::int::endian::tests!($Digit; utest); - } - } - }; + crate::int::endian::tests!(utest); } crate::macro_impl!(endian); diff --git a/src/buint/fmt.rs b/src/buint/fmt.rs index 8ba8d56..3202c69 100644 --- a/src/buint/fmt.rs +++ b/src/buint/fmt.rs @@ -104,15 +104,12 @@ macro_rules! fmt { impl UpperHex for $BUint { fmt_method!("{:X}", "{:01$X}", digit::$Digit::HEX_PADDING, "0x"); } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::fmt::tests!(utest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::fmt::tests!(utest); +} + crate::macro_impl!(fmt); \ No newline at end of file diff --git a/src/buint/mod.rs b/src/buint/mod.rs index 8bcd0f0..f35c042 100644 --- a/src/buint/mod.rs +++ b/src/buint/mod.rs @@ -642,93 +642,90 @@ macro_rules! mod_impl { out } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::{debug_skip, test_bignum, types::utest}; - use crate::test::types::big_types::$Digit::*; - - crate::int::tests!(utest); - - test_bignum! { - function: ::next_power_of_two(a: utest), - skip: debug_skip!(a.checked_next_power_of_two().is_none()) - } - test_bignum! { - function: ::is_power_of_two(a: utest) - } - test_bignum! { - function: ::cast_signed(a: utest) - } - - #[test] - fn digits() { - let a = UTEST::MAX; - let digits = *a.digits(); - assert_eq!(a, UTEST::from_digits(digits)); - } - - #[test] - fn bit() { - let u = UTEST::from(0b001010100101010101u64); - assert!(u.bit(0)); - assert!(!u.bit(1)); - assert!(!u.bit(17)); - assert!(!u.bit(16)); - assert!(u.bit(15)); - } - - #[test] - fn is_zero() { - assert!(UTEST::MIN.is_zero()); - assert!(!UTEST::MAX.is_zero()); - assert!(!UTEST::ONE.is_zero()); - } - - #[test] - fn is_one() { - assert!(UTEST::ONE.is_one()); - assert!(!UTEST::MAX.is_one()); - assert!(!UTEST::ZERO.is_one()); - let mut digits = *super::$BUint::<2>::MAX.digits(); - digits[0] = 1; - let b = super::$BUint::<2>::from_digits(digits); - assert!(!b.is_one()); - } - - #[test] - fn bits() { - let u = UTEST::from(0b1001010100101010101u128); - assert_eq!(u.bits(), 19); - - let u = UTEST::power_of_two(34); - assert_eq!(u.bits(), 35); - } - - #[test] - fn default() { - assert_eq!(UTEST::default(), utest::default().into()); - } - - #[test] - fn sum() { - let v = vec![&UTEST::ZERO, &UTEST::ONE, &UTEST::TWO, &UTEST::THREE, &UTEST::FOUR]; - assert_eq!(UTEST::TEN, v.iter().copied().sum()); - assert_eq!(UTEST::TEN, v.into_iter().sum()); - } - - #[test] - fn product() { - let v = vec![&UTEST::ONE, &UTEST::TWO, &UTEST::THREE]; - assert_eq!(UTEST::SIX, v.iter().copied().sum()); - assert_eq!(UTEST::SIX, v.into_iter().sum()); - } - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{debug_skip, test_bignum, types::utest}; + + crate::int::tests!(utest); + + test_bignum! { + function: ::next_power_of_two(a: utest), + skip: debug_skip!(a.checked_next_power_of_two().is_none()) + } + test_bignum! { + function: ::is_power_of_two(a: utest) + } + test_bignum! { + function: ::cast_signed(a: utest) + } + + #[test] + fn digits() { + let a = UTEST::MAX; + let digits = *a.digits(); + assert_eq!(a, UTEST::from_digits(digits)); + } + + #[test] + fn bit() { + let u = UTEST::from(0b001010100101010101u64); + assert!(u.bit(0)); + assert!(!u.bit(1)); + // assert!(!u.bit(17)); + // assert!(!u.bit(16)); + assert!(u.bit(15)); + } + + #[test] + fn is_zero() { + assert!(UTEST::MIN.is_zero()); + assert!(!UTEST::MAX.is_zero()); + assert!(!UTEST::ONE.is_zero()); + } + + #[test] + fn is_one() { + assert!(UTEST::ONE.is_one()); + assert!(!UTEST::MAX.is_one()); + assert!(!UTEST::ZERO.is_one()); + let mut digits = *super::BUint::<2>::MAX.digits(); + digits[0] = 1; + let b = super::BUint::<2>::from_digits(digits); + assert!(!b.is_one()); + } + + #[test] + fn bits() { + let u = UTEST::from(0b1010100101010101u128); + assert_eq!(u.bits(), 16); + + let u = UTEST::power_of_two(7); + assert_eq!(u.bits(), 8); + } + + #[test] + fn default() { + assert_eq!(UTEST::default(), utest::default().into()); + } + + #[test] + fn sum() { + let v = vec![&UTEST::ZERO, &UTEST::ONE, &UTEST::TWO, &UTEST::THREE, &UTEST::FOUR]; + assert_eq!(UTEST::TEN, v.iter().copied().sum()); + assert_eq!(UTEST::TEN, v.into_iter().sum()); + } + + #[test] + fn product() { + let v = vec![&UTEST::ONE, &UTEST::TWO, &UTEST::THREE]; + assert_eq!(UTEST::SIX, v.iter().copied().sum()); + assert_eq!(UTEST::SIX, v.into_iter().sum()); + } +} + crate::macro_impl!(mod_impl); mod bigint_helpers; diff --git a/src/buint/numtraits.rs b/src/buint/numtraits.rs index 5cd9b55..9cfb051 100644 --- a/src/buint/numtraits.rs +++ b/src/buint/numtraits.rs @@ -419,17 +419,14 @@ macro_rules! numtraits { } impl Unsigned for $BUint {} - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::types::utest; - - crate::int::numtraits::tests!(utest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::utest; + + crate::int::numtraits::tests!(utest); +} + crate::macro_impl!(numtraits); diff --git a/src/buint/ops.rs b/src/buint/ops.rs index 8b5942b..649a31a 100644 --- a/src/buint/ops.rs +++ b/src/buint/ops.rs @@ -101,15 +101,13 @@ macro_rules! ops { crate::int::ops::impls!($BUint, $BUint, $BInt); - #[cfg(test)] + #[cfg(all(test, test_int_bits = "64"))] paste::paste! { - mod [<$Digit _digit_tests>] { + mod [<$Digit _add_digit_test>] { use super::*; use crate::test::{test_bignum, types::utest}; use crate::test::types::big_types::$Digit::*; - crate::int::ops::tests!(utest); - quickcheck::quickcheck! { fn add_digit(a: utest, b: $Digit) -> quickcheck::TestResult { use crate::cast::As; @@ -130,4 +128,11 @@ macro_rules! ops { }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::utest}; + + crate::int::ops::tests!(utest); +} + crate::macro_impl!(ops); diff --git a/src/buint/overflowing.rs b/src/buint/overflowing.rs index 8845590..b346821 100644 --- a/src/buint/overflowing.rs +++ b/src/buint/overflowing.rs @@ -148,53 +148,50 @@ macro_rules! overflowing { (prod, o || overflow) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::{test_bignum, types::utest}; - use crate::test::types::big_types::$Digit::*; +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::utest}; - test_bignum! { - function: ::overflowing_add(a: utest, b: utest) - } - test_bignum! { - function: ::overflowing_sub(a: utest, b: utest) - } - test_bignum! { - function: ::overflowing_mul(a: utest, b: utest) - } - test_bignum! { - function: ::overflowing_div(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::overflowing_div_euclid(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::overflowing_rem(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::overflowing_rem_euclid(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::overflowing_neg(a: utest) - } - test_bignum! { - function: ::overflowing_shl(a: utest, b: u16) - } - test_bignum! { - function: ::overflowing_shr(a: utest, b: u16) - } - test_bignum! { - function: ::overflowing_pow(a: utest, b: u16) - } - } - } - }; + test_bignum! { + function: ::overflowing_add(a: utest, b: utest) + } + test_bignum! { + function: ::overflowing_sub(a: utest, b: utest) + } + test_bignum! { + function: ::overflowing_mul(a: utest, b: utest) + } + test_bignum! { + function: ::overflowing_div(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::overflowing_div_euclid(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::overflowing_rem(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::overflowing_rem_euclid(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::overflowing_neg(a: utest) + } + test_bignum! { + function: ::overflowing_shl(a: utest, b: u16) + } + test_bignum! { + function: ::overflowing_shr(a: utest, b: u16) + } + test_bignum! { + function: ::overflowing_pow(a: utest, b: u16) + } } crate::macro_impl!(overflowing); diff --git a/src/buint/radix.rs b/src/buint/radix.rs index f8a8b88..797d1b4 100644 --- a/src/buint/radix.rs +++ b/src/buint/radix.rs @@ -223,13 +223,23 @@ macro_rules! radix { let base_digits_per_digit = (digit::$Digit::BITS_U8 / ilog2(radix)) as usize; let full_digits = input_digits_len / base_digits_per_digit as usize; let remaining_digits = input_digits_len % base_digits_per_digit as usize; + let radix_u8 = radix as u8; + if full_digits > N || full_digits == N && remaining_digits != 0 { + let mut i = if leading_sign { 1 } else { 0 }; + while i < N * base_digits_per_digit + if leading_sign { 1 } else { 0 } { + if Self::byte_to_digit::(buf[i]) >= radix_u8 { + return Err(ParseIntError { + kind: IntErrorKind::InvalidDigit, + }); + } + i += 1; + } return Err(ParseIntError { kind: IntErrorKind::PosOverflow, }); } - let radix_u8 = radix as u8; let log2r = ilog2(radix); let mut i = 0; @@ -270,7 +280,7 @@ macro_rules! radix { } Ok(out) }, - 8 | 32 | 64 | 128 => { + /*8 | 32 | 64 | 128*/ 0 => { // TODO: for now, we don't use this, as hard to get the errors right let mut out = Self::ZERO; let radix_u8 = radix as u8; let log2r = ilog2(radix); @@ -289,6 +299,7 @@ macro_rules! radix { }; let d = Self::byte_to_digit::(buf[idx]); if d >= radix_u8 { + // panic!("noooooo"); return Err(ParseIntError { kind: IntErrorKind::InvalidDigit, }); @@ -368,6 +379,20 @@ macro_rules! radix { j += 1; } if carry != 0 { + while start < buf.len() && start < end { // TODO: this isn't quite correct behaviour + let idx = if BE { + start + } else { + buf.len() - 1 - start + }; + let d = Self::byte_to_digit::(buf[idx]); + if d >= radix_u8 { + return Err(ParseIntError { + kind: IntErrorKind::InvalidDigit, + }); + } + start += 1; + } return Err(ParseIntError { kind: IntErrorKind::PosOverflow, }); @@ -573,97 +598,96 @@ macro_rules! radix { Self::from_str_radix(src, 10) } } + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::{quickcheck_from_to_radix, test_bignum, self}; - use crate::$BUint; - use core::str::FromStr; - use crate::test::types::big_types::$Digit::*; - use crate::test::types::utest; - - test_bignum! { - function: ::from_str, - cases: [ - ("398475394875230495745"), - ("3984753948752304957423490785029749572977970985"), - ("12345💩👍"), - ("1234567890a"), - ("") - ] - } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{quickcheck_from_to_radix, test_bignum, self}; + use crate::BUint; + use core::str::FromStr; + use crate::test::types::utest; + + + test_bignum! { + function: ::from_str, + cases: [ + ("398475394875230495745"), + ("3984753948752304957423490785029749572977970985"), + ("12345💩👍"), + ("1234567890a"), + ("") + ] + } - test_bignum! { - function: ::from_str_radix, - cases: [ - ("+af7345asdofiuweor", 35u32), - ("+945hhdgi73945hjdfj", 32u32), - ("+3436847561345343455", 9u32), - ("+affe758457bc345540ac399", 16u32), - ("+affe758457bc345540ac39929334534ee34579234795", 17u32), - ("+3777777777777777777777777777777777777777777", 8u32), - ("+37777777777777777777777777777777777777777761", 8u32), - ("+1777777777777777777777", 8u32), - ("+17777777777777777777773", 8u32), - ("+2000000000000000000000", 8u32), - ("-234598734", 10u32), - ("g234ab", 16u32), - ("234£$2234", 15u32), - ("123456💯", 30u32), - ("3434💯34593487", 12u32), - ("💯34593487", 11u32), - ("12345678", 8u32), - ("abcdefw", 32u32), - ("1234ab", 11u32), - ("1234", 4u32), - ("010120101", 2u32), - ("10000000000000000", 16u32), - ("p8hrbe0mo0084i6vckj1tk7uvacnn4cm", 32u32), - ("", 10u32) - ] - } + #[cfg(not(test_int_bits = "16"))] + test_bignum! { + function: ::from_str_radix, + cases: [ + ("+af7345asdofiuweor", 35u32), + ("+945hhdgi73945hjdfj", 32u32), + ("+3436847561345343455", 9u32), + ("+affe758457bc345540ac399", 16u32), + ("+affe758457bc345540ac39929334534ee34579234795", 17u32), + ("+3777777777777777777777777777777777777777777", 8u32), + ("+37777777777777777777777777777777777777777761", 8u32), + ("+1777777777777777777777", 8u32), + ("+17777777777777777777773", 8u32), + ("+2000000000000000000000", 8u32), + ("-234598734", 10u32), + ("g234ab", 16u32), + ("234£$2234", 15u32), + ("123456💯", 30u32), + ("3434💯34593487", 12u32), + ("💯34593487", 11u32), + ("12345678", 8u32), + ("abcdefw", 32u32), + ("1234ab", 11u32), + ("1234", 4u32), + ("010120101", 2u32), + ("10000000000000000", 16u32), + ("p8hrbe0mo0084i6vckj1tk7uvacnn4cm", 32u32), + ("", 10u32) + ] + } - quickcheck_from_to_radix!(utest, radix_be, 256); - quickcheck_from_to_radix!(utest, radix_le, 256); - quickcheck_from_to_radix!(utest, str_radix, 36); + quickcheck_from_to_radix!(utest, radix_be, 256); + quickcheck_from_to_radix!(utest, radix_le, 256); + quickcheck_from_to_radix!(utest, str_radix, 36); - #[test] - #[should_panic(expected = "attempt to parse integer from empty string")] - fn parse_str_radix_empty() { - let _ = UTEST::parse_str_radix("", 10); - } + #[test] + #[should_panic(expected = "attempt to parse integer from empty string")] + fn parse_str_radix_empty() { + let _ = UTEST::parse_str_radix("", 10); + } - #[test] - #[should_panic(expected = "attempt to parse integer from string containing invalid digit")] - fn parse_str_radix_invalid_char() { - let _ = UTEST::parse_str_radix("a", 10); - } + #[test] + #[should_panic(expected = "attempt to parse integer from string containing invalid digit")] + fn parse_str_radix_invalid_char() { + let _ = UTEST::parse_str_radix("a", 10); + } - #[test] - fn parse_empty() { - assert_eq!(UTEST::from_radix_be(&[], 10), Some(UTEST::ZERO)); - assert_eq!(UTEST::from_radix_le(&[], 10), Some(UTEST::ZERO)); - } + #[test] + fn parse_empty() { + assert_eq!(UTEST::from_radix_be(&[], 10), Some(UTEST::ZERO)); + assert_eq!(UTEST::from_radix_le(&[], 10), Some(UTEST::ZERO)); + } - test::quickcheck_from_str_radix!(utest, "+" | ""); - test::quickcheck_from_str!(utest); + test::quickcheck_from_str_radix!(utest, "+" | ""); + test::quickcheck_from_str!(utest); - #[test] - fn parse_bytes() { - let src = "134957dkbhadoinegrhi983475hdgkhgdhiu3894hfd"; - let u = $BUint::<100>::parse_bytes(src.as_bytes(), 35).unwrap(); - let v = $BUint::<100>::from_str_radix(src, 35).unwrap(); - assert_eq!(u, v); - assert_eq!(v.to_str_radix(35), src); + #[test] + fn parse_bytes() { + let src = "134957dkbhadoinegrhi983475hdgkhgdhiu3894hfd"; + let u = BUint::<100>::parse_bytes(src.as_bytes(), 35).unwrap(); + let v = BUint::<100>::from_str_radix(src, 35).unwrap(); + assert_eq!(u, v); + assert_eq!(v.to_str_radix(35), src); - let bytes = b"345977fsuudf0350845"; - let option = $BUint::<100>::parse_bytes(bytes, 20); - assert!(option.is_none()); - } - } - } - }; + let bytes = b"345977fsuudf0350845"; + let option = BUint::<100>::parse_bytes(bytes, 20); + assert!(option.is_none()); + } } crate::macro_impl!(radix); diff --git a/src/buint/saturating.rs b/src/buint/saturating.rs index afb86fd..fc5adba 100644 --- a/src/buint/saturating.rs +++ b/src/buint/saturating.rs @@ -74,6 +74,9 @@ macro_rules! saturating { #[cfg(test)] crate::test::all_digit_tests! { + use crate::test::test_bignum; + use crate::test::types::*; + test_bignum! { function: ::saturating_add(a: utest, b: utest) } diff --git a/src/buint/strict.rs b/src/buint/strict.rs index 395d920..972935d 100644 --- a/src/buint/strict.rs +++ b/src/buint/strict.rs @@ -14,21 +14,17 @@ macro_rules! strict { ) } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; + }; +} - crate::int::strict::tests!(utest); +#[cfg(test)] +crate::test::all_digit_tests! { + crate::int::strict::tests!(utest); - test_bignum! { - function: ::strict_add_signed(a: utest, b: itest), - skip: a.checked_add_signed(b).is_none() - } - } - } - }; + test_bignum! { + function: ::strict_add_signed(a: utest, b: itest), + skip: a.checked_add_signed(b).is_none() + } } use crate::doc; diff --git a/src/buint/unchecked.rs b/src/buint/unchecked.rs index 0601a54..1f2bfe7 100644 --- a/src/buint/unchecked.rs +++ b/src/buint/unchecked.rs @@ -1,17 +1,16 @@ macro_rules! unchecked { ($BUint: ident, $BInt: ident, $Digit: ident) => { crate::int::unchecked::impls!($BUint, U); - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - crate::int::unchecked::tests!(utest); - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::*; + + crate::int::unchecked::tests!(utest); +} + use crate::doc; crate::macro_impl!(unchecked); diff --git a/src/buint/wrapping.rs b/src/buint/wrapping.rs index 8030acc..5077c3f 100644 --- a/src/buint/wrapping.rs +++ b/src/buint/wrapping.rs @@ -112,78 +112,75 @@ macro_rules! wrapping { } } } - - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::{test_bignum, types::{utest, itest}}; - - #[test] - #[should_panic(expected = "attempt to divide by zero")] - fn div_by_zero_panic() { - let a = UTEST::MAX; - let b = UTEST::ZERO; - let _ = a.wrapping_div(b); - } - - #[test] - #[should_panic(expected = "attempt to calculate the remainder with a divisor of zero")] - fn rem_by_zero_panic() { - let a = UTEST::MAX; - let b = UTEST::ZERO; - let _ = a.wrapping_rem(b); - } - - test_bignum! { - function: ::wrapping_add(a: utest, b: utest) - } - test_bignum! { - function: ::wrapping_add_signed(a: utest, b: itest) - } - test_bignum! { - function: ::wrapping_sub(a: utest, b: utest) - } - test_bignum! { - function: ::wrapping_mul(a: utest, b: utest) - } - test_bignum! { - function: ::wrapping_div(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_div_euclid(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_rem(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_rem_euclid(a: utest, b: utest), - skip: b == 0 - } - test_bignum! { - function: ::wrapping_neg(a: utest) - } - test_bignum! { - function: ::wrapping_shl(a: utest, b: u16) - } - test_bignum! { - function: ::wrapping_shr(a: utest, b: u16) - } - test_bignum! { - function: ::wrapping_pow(a: utest, b: u16) - } - test_bignum! { - function: ::wrapping_next_power_of_two(a: utest), - cases: [ - (utest::MAX) - ] - } - } - } }; } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::{test_bignum, types::{utest, itest}}; + + #[test] + #[should_panic(expected = "attempt to divide by zero")] + fn div_by_zero_panic() { + let a = UTEST::MAX; + let b = UTEST::ZERO; + let _ = a.wrapping_div(b); + } + + #[test] + #[should_panic(expected = "attempt to calculate the remainder with a divisor of zero")] + fn rem_by_zero_panic() { + let a = UTEST::MAX; + let b = UTEST::ZERO; + let _ = a.wrapping_rem(b); + } + + test_bignum! { + function: ::wrapping_add(a: utest, b: utest) + } + test_bignum! { + function: ::wrapping_add_signed(a: utest, b: itest) + } + test_bignum! { + function: ::wrapping_sub(a: utest, b: utest) + } + test_bignum! { + function: ::wrapping_mul(a: utest, b: utest) + } + test_bignum! { + function: ::wrapping_div(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_div_euclid(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_rem(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_rem_euclid(a: utest, b: utest), + skip: b == 0 + } + test_bignum! { + function: ::wrapping_neg(a: utest) + } + test_bignum! { + function: ::wrapping_shl(a: utest, b: u16) + } + test_bignum! { + function: ::wrapping_shr(a: utest, b: u16) + } + test_bignum! { + function: ::wrapping_pow(a: utest, b: u16) + } + test_bignum! { + function: ::wrapping_next_power_of_two(a: utest), + cases: [ + (utest::MAX) + ] + } +} + crate::macro_impl!(wrapping); diff --git a/src/errors/parseint.rs b/src/errors/parseint.rs index 6b26817..a2aa8f7 100644 --- a/src/errors/parseint.rs +++ b/src/errors/parseint.rs @@ -4,7 +4,7 @@ use core::num::IntErrorKind; /// The error type that is returned when parsing an integer from an invalid source. /// /// This error can occur when the `from_str_radix` or [`FromStr::from_str`](https://doc.rust-lang.org/core/str/trait.FromStr.html#tymethod.from_str) methods of e.g. [`BUint`](crate::BUint::from_str_radix) are called with an invalid input string. -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq, Clone, Debug)] pub struct ParseIntError { pub(crate) kind: IntErrorKind, } @@ -40,9 +40,3 @@ impl Display for ParseIntError { write!(f, "{} {}", super::err_prefix!(), self.description()) } } - -impl Debug for ParseIntError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Display::fmt(&self, f) - } -} diff --git a/src/int/cast.rs b/src/int/cast.rs index 87eb7b5..ad5bdc2 100644 --- a/src/int/cast.rs +++ b/src/int/cast.rs @@ -12,7 +12,7 @@ macro_rules! tests { $( test::test_from! { function: <$int as CastFrom>::cast_from, - from_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, bool, char, UTESTD8, UTESTD16, UTESTD32, UTESTD64, TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, ITESTD8, ITESTD16, ITESTD32, ITESTD64, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10) + from_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, bool, char, TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestUint9, TestUint10, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8, TestInt9, TestInt10) } test::test_into! { @@ -20,7 +20,7 @@ macro_rules! tests { into_types: (u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64) } - crate::int::cast::test_cast_to_bigint!($int; UTESTD8, UTESTD16, UTESTD32, UTESTD64, TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, ITESTD8, ITESTD16, ITESTD32, ITESTD64, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8); + crate::int::cast::test_cast_to_bigint!($int; TestUint1, TestUint2, TestUint3, TestUint4, TestUint5, TestUint6, TestUint7, TestUint8, TestInt1, TestInt2, TestInt3, TestInt4, TestInt5, TestInt6, TestInt7, TestInt8); )* }; } diff --git a/src/int/endian.rs b/src/int/endian.rs index 89e6643..5b1df53 100644 --- a/src/int/endian.rs +++ b/src/int/endian.rs @@ -110,7 +110,7 @@ pub fn le_pad(pad_length: usize, bits: u32) -> (usize, Range, Range { + ($int: ty) => { #[cfg(feature = "nightly")] use crate::test::U8ArrayWrapper; diff --git a/src/int/fmt.rs b/src/int/fmt.rs index dc1de18..f50795c 100644 --- a/src/int/fmt.rs +++ b/src/int/fmt.rs @@ -92,4 +92,4 @@ macro_rules! tests { pub(crate) use tests; #[cfg(test)] -crate::int::fmt::impl_format!(u128, i128, u64, i64); +crate::int::fmt::impl_format!(u128, i128, u64, i64, u32, i32, u16, i16); diff --git a/src/int/ops.rs b/src/int/ops.rs index 9521d00..1c927f0 100644 --- a/src/int/ops.rs +++ b/src/int/ops.rs @@ -384,6 +384,9 @@ pub(crate) use impls; #[cfg(test)] macro_rules! tests { ($int: ty) => { + #[allow(unused_imports)] + use super::*; + test_bignum! { function: <$int as Add>::add(a: $int, b: $int), skip: a.checked_add(b).is_none() diff --git a/src/random.rs b/src/random.rs index 4786ca9..24b1a4e 100644 --- a/src/random.rs +++ b/src/random.rs @@ -324,25 +324,22 @@ macro_rules! random { uniform_int_impl!($BUint, $BUint); uniform_int_impl!($BInt, $BUint, to_bits, from_bits); + }; +} - #[cfg(test)] - paste::paste! { - mod [<$Digit _digit_tests>] { - use crate::test::types::big_types::$Digit::*; - use crate::test::types::*; - use rand::SeedableRng; - - fn seeded_rngs(seed: u64) -> (R, R) { - let rng = R::seed_from_u64(seed); - let rng2 = rng.clone(); // need to clone `rng` to produce the same results before it is used as `gen` mutates it - (rng, rng2) - } +#[cfg(test)] +crate::test::all_digit_tests! { + use crate::test::types::*; + use rand::SeedableRng; + + fn seeded_rngs(seed: u64) -> (R, R) { + let rng = R::seed_from_u64(seed); + let rng2 = rng.clone(); // need to clone `rng` to produce the same results before it is used as `gen` mutates it + (rng, rng2) + } - test_random!(utest; StdRng, SmallRng); - test_random!(itest; StdRng, SmallRng); - } - } - }; + test_random!(utest; StdRng, SmallRng); + test_random!(itest; StdRng, SmallRng); } crate::macro_impl!(random); diff --git a/src/test/convert.rs b/src/test/convert.rs index fec8c8a..5a33a0d 100644 --- a/src/test/convert.rs +++ b/src/test/convert.rs @@ -49,6 +49,8 @@ test_convert_bigints!(128, 64); test_convert_big!(BUintD32<{32 / 32}>, BUintD16<{32 / 16}>, BUintD8<{32 / 8}>; u32); test_convert_big!(BIntD32<{32 / 32}>, BIntD16<{32 / 16}>, BIntD8<{32 / 8}>; i32); +test_convert_big!(BUintD16<{16 / 16}>, BUintD8<{16 / 8}>; u16); +test_convert_big!(BIntD16<{16 / 16}>, BIntD8<{16 / 8}>; i16); impl TestConvert for Option { type Output = Option<::Output>; diff --git a/src/test/macros.rs b/src/test/macros.rs index 507f287..b33468a 100644 --- a/src/test/macros.rs +++ b/src/test/macros.rs @@ -31,7 +31,7 @@ macro_rules! test_bignum { fn []() { $( let (big, primitive) = crate::test::results!(<$primitive> :: $function ($($($re2)? Into::into($arg)), *)); - assert_eq!(big, primitive); + assert_eq!(big, primitive, "cases assertion with inputs {:?}", ($($arg), *)); )* } } @@ -248,9 +248,8 @@ macro_rules! digit_tests { { use $Digit: ident digit; $($test_content: tt)* } => { paste::paste! { mod [<$Digit _digit_tests>] { - use crate::test::{test_bignum, types::*}; use crate::test::types::big_types::$Digit::*; - + $($test_content)* } } @@ -263,13 +262,12 @@ macro_rules! all_digit_tests { { $($test_content: tt)* } => { crate::test::digit_tests! { use u8 digit; $($test_content)* } - #[cfg(not(test_int_bits = "8"))] crate::test::digit_tests! { use u16 digit; $($test_content)* } - #[cfg(not(any(test_int_bits = "8", test_int_bits = "16")))] + #[cfg(not(test_int_bits = "16"))] crate::test::digit_tests! { use u32 digit; $($test_content)* } - #[cfg(not(any(test_int_bits = "8", test_int_bits = "16", test_int_bits = "32")))] + #[cfg(not(any(test_int_bits = "16", test_int_bits = "32")))] crate::test::digit_tests! { use u64 digit; $($test_content)* } } } diff --git a/src/test/types.rs b/src/test/types.rs index 53e68e7..e3a4323 100644 --- a/src/test/types.rs +++ b/src/test/types.rs @@ -36,10 +36,10 @@ pub mod big_types { #[cfg(test_int_bits = "16")] mod small_types { #[allow(non_camel_case_types)] - pub type utest = u64; + pub type utest = u16; #[allow(non_camel_case_types)] - pub type itest = i64; + pub type itest = i16; // #[cfg(feature = "float")] // #[allow(non_camel_case_types)] @@ -49,10 +49,10 @@ mod small_types { #[cfg(test_int_bits = "32")] mod small_types { #[allow(non_camel_case_types)] - pub type utest = u64; + pub type utest = u32; #[allow(non_camel_case_types)] - pub type itest = i64; + pub type itest = i32; // #[cfg(feature = "float")] // #[allow(non_camel_case_types)]