|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +fn assert_nb<Int: ToString + FromStr>(value: Int) { |
| 4 | + let s = value.to_string(); |
| 5 | + let s2 = format!("{}", value); |
| 6 | + |
| 7 | + // assert_eq!(s, s2); |
| 8 | + assert_eq!(s, "0".to_string()); |
| 9 | + let ret = Int::from_str(&s).unwrap(); |
| 10 | + assert_eq!(ret, int); |
| 11 | +} |
| 12 | + |
| 13 | +macro_rules! uint_to_s { |
| 14 | + ($($fn_name:ident, $int:ident,)+) => { |
| 15 | + $( |
| 16 | + #[test] |
| 17 | + fn $fn_name() { |
| 18 | + assert_nb::<$int>($int::MIN); |
| 19 | + assert_nb::<$int>($int::MAX); |
| 20 | + assert_nb::<$int>(1); |
| 21 | + assert_nb::<$int>($int::MIN / 2); |
| 22 | + assert_nb::<$int>($int::MAX / 2); |
| 23 | + } |
| 24 | + )+ |
| 25 | + } |
| 26 | +} |
| 27 | +macro_rules! int_to_s { |
| 28 | + ($($fn_name:ident, $int:ident,)+) => { |
| 29 | + $( |
| 30 | + #[test] |
| 31 | + fn $fn_name() { |
| 32 | + assert_nb::<$int>($int::MIN); |
| 33 | + assert_nb::<$int>($int::MAX); |
| 34 | + assert_nb::<$int>(1); |
| 35 | + assert_nb::<$int>(0); |
| 36 | + assert_nb::<$int>(-1); |
| 37 | + assert_nb::<$int>($int::MIN / 2); |
| 38 | + assert_nb::<$int>($int::MAX / 2); |
| 39 | + } |
| 40 | + )+ |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +int_to_s!( |
| 45 | + test_i8_to_string, i8, |
| 46 | + test_i16_to_string, i16, |
| 47 | + test_i32_to_string, i32, |
| 48 | + test_i64_to_string, i64, |
| 49 | + test_i128_to_string, i128, |
| 50 | +); |
| 51 | +uint_to_s!( |
| 52 | + test_u8_to_string, u8, |
| 53 | + test_u16_to_string, u16, |
| 54 | + test_u32_to_string, u32, |
| 55 | + test_u64_to_string, u64, |
| 56 | + test_u128_to_string, u128, |
| 57 | +); |
0 commit comments