| 1 |
use paste::*; |
| 2 |
|
| 3 |
macro_rules! impl_case { |
| 4 |
($n:literal >> $s:literal => $expected:literal) => { |
| 5 |
paste! { |
| 6 |
#[test] |
| 7 |
fn [< case_ $n _ $s >]() { |
| 8 |
assert_eq!(TEST_FUNC($n, $s), $expected); |
| 9 |
} |
| 10 |
} |
| 11 |
}; |
| 12 |
($n:literal => $expected:literal) => { |
| 13 |
paste! { |
| 14 |
#[test] |
| 15 |
fn [< case_ $n >]() { |
| 16 |
assert_eq!(TEST_FUNC($n), $expected); |
| 17 |
} |
| 18 |
} |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
mod count_digits_u32 { |
| 24 |
use super::*; |
| 25 |
const TEST_FUNC: fn(u32) -> usize = count_digits_u32; |
| 26 |
|
| 27 |
impl_case!(0 => 1); |
| 28 |
impl_case!(1 => 1); |
| 29 |
impl_case!(10 => 2); |
| 30 |
impl_case!(999999 => 6); |
| 31 |
impl_case!(4294967295 => 10); |
| 32 |
} |
| 33 |
|
| 34 |
mod count_digits_u64 { |
| 35 |
use super::*; |
| 36 |
const TEST_FUNC: fn(u64) -> usize = count_digits_u64; |
| 37 |
|
| 38 |
impl_case!(0 => 1); |
| 39 |
impl_case!(1 => 1); |
| 40 |
impl_case!(10 => 2); |
| 41 |
impl_case!(999999 => 6); |
| 42 |
impl_case!(4294967295 => 10); |
| 43 |
impl_case!(18446744073709551615 => 20); |
| 44 |
} |
| 45 |
|
| 46 |
mod count_digits_uint { |
| 47 |
use super::*; |
| 48 |
|
| 49 |
#[allow(non_snake_case)] |
| 50 |
fn TEST_FUNC(src: &str) -> u64 { |
| 51 |
let n = src.parse().unwrap(); |
| 52 |
count_digits_biguint(&n) |
| 53 |
} |
| 54 |
|
| 55 |
impl_case!("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" => 99); |
| 56 |
impl_case!("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" => 100); |
| 57 |
impl_case!("10000000000000000000000000000000000000000000000000000000000000000" => 65); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
mod dec_shift_right_u32 { |
| 62 |
use super::*; |
| 63 |
const TEST_FUNC: fn(u32, usize) -> u32 = dec_shift_right_u32; |
| 64 |
|
| 65 |
impl_case!(0 >> 0 => 0); |
| 66 |
|
| 67 |
impl_case!(12345 >> 0 => 12345); |
| 68 |
impl_case!(12345 >> 1 => 1234); |
| 69 |
impl_case!(12345 >> 2 => 123); |
| 70 |
impl_case!(12345 >> 6 => 0); |
| 71 |
impl_case!(12345 >> 7 => 0); |
| 72 |
|
| 73 |
impl_case!(999999999 >> 1 => 99999999); |
| 74 |
impl_case!(999999999 >> 5 => 9999); |
| 75 |
impl_case!(999999999 >> 7 => 99); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
mod dec_shift_right_u64 { |
| 80 |
use super::*; |
| 81 |
const TEST_FUNC: fn(u64, usize) -> u64 = dec_shift_right_u64; |
| 82 |
|
| 83 |
impl_case!(1234567890123 >> 0 => 1234567890123); |
| 84 |
impl_case!(1234567890123 >> 2 => 12345678901); |
| 85 |
impl_case!(1234567890123 >> 6 => 1234567); |
| 86 |
impl_case!(1234567890123 >> 7 => 123456); |
| 87 |
|
| 88 |
impl_case!(18446744073709551615 >> 1 => 1844674407370955161); |
| 89 |
impl_case!(18446744073709551615 >> 5 => 184467440737095); |
| 90 |
impl_case!(18446744073709551615 >> 6 => 18446744073709); |
| 91 |
impl_case!(18446744073709551615 >> 7 => 1844674407370); |
| 92 |
impl_case!(18446744073709551615 >> 9 => 18446744073); |
| 93 |
impl_case!(18446744073709551615 >> 10 => 1844674407); |
| 94 |
impl_case!(18446744073709551615 >> 19 => 1); |
| 95 |
impl_case!(18446744073709551615 >> 20 => 0); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
#[cfg(property_tests)] |
| 100 |
mod prop { |
| 101 |
use super::*; |
| 102 |
use proptest::*; |
| 103 |
use proptest::num::f64::*; |
| 104 |
|
| 105 |
proptest! { |
| 106 |
#[test] |
| 107 |
fn check_dec_shift_right_u32(n: u32) { |
| 108 |
let mut x = 1; |
| 109 |
for s in 0..12 { |
| 110 |
let expected = if x > 0 { n / x } else { 0 }; |
| 111 |
x = x.checked_mul(10).unwrap_or(0); |
| 112 |
|
| 113 |
let value = dec_shift_right_u32(n, s); |
| 114 |
prop_assert_eq!(expected, value); |
| 115 |
} |
| 116 |
} |
| 117 |
#[test] |
| 118 |
fn check_dec_shift_right_u64(n: u64) { |
| 119 |
let mut x = 1; |
| 120 |
for s in 0..22 { |
| 121 |
let expected = if x > 0 { n / x } else { 0 }; |
| 122 |
x = x.checked_mul(10).unwrap_or(0); |
| 123 |
|
| 124 |
let value = dec_shift_right_u64(n, s); |
| 125 |
prop_assert_eq!(expected, value); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |