Haibo Huang | a4c5ece | 2020-12-29 20:32:47 -0800 | [diff] [blame] | 1 | #![cfg_attr(feature = "cargo-clippy", allow(cast_lossless, string_lit_as_bytes))] |
Yi Kong | 8884bbe | 2020-08-31 01:13:13 +0800 | [diff] [blame] | 2 | #![allow(non_snake_case)] |
| 3 | |
| 4 | extern crate itoa; |
| 5 | |
| 6 | macro_rules! test { |
| 7 | ( |
| 8 | $( |
| 9 | $(#[$attr:meta])* |
| 10 | $name:ident($value:expr, $expected:expr) |
| 11 | ),* |
| 12 | ) => { |
| 13 | $( |
| 14 | $(#[$attr])* |
| 15 | #[test] |
| 16 | fn $name() { |
| 17 | #[cfg(feature = "std")] |
| 18 | { |
| 19 | let mut buf = [b'\0'; 40]; |
| 20 | let len = itoa::write(&mut buf[..], $value).unwrap(); |
| 21 | assert_eq!(&buf[0..len], $expected.as_bytes()); |
| 22 | } |
| 23 | |
| 24 | let mut s = String::new(); |
| 25 | itoa::fmt(&mut s, $value).unwrap(); |
| 26 | assert_eq!(s, $expected); |
| 27 | } |
| 28 | )* |
| 29 | } |
| 30 | } |
| 31 | |
Haibo Huang | a4c5ece | 2020-12-29 20:32:47 -0800 | [diff] [blame] | 32 | test! { |
Yi Kong | 8884bbe | 2020-08-31 01:13:13 +0800 | [diff] [blame] | 33 | test_u64_0(0u64, "0"), |
| 34 | test_u64_half(<u32>::max_value() as u64, "4294967295"), |
| 35 | test_u64_max(<u64>::max_value(), "18446744073709551615"), |
| 36 | test_i64_min(<i64>::min_value(), "-9223372036854775808"), |
| 37 | |
| 38 | test_i16_0(0i16, "0"), |
| 39 | test_i16_min(<i16>::min_value(), "-32768"), |
| 40 | |
| 41 | #[cfg(feature = "i128")] |
| 42 | test_u128_0(0u128, "0"), |
| 43 | #[cfg(feature = "i128")] |
| 44 | test_u128_max(<u128>::max_value(), "340282366920938463463374607431768211455"), |
| 45 | #[cfg(feature = "i128")] |
| 46 | test_i128_min(<i128>::min_value(), "-170141183460469231731687303715884105728") |
| 47 | } |