Files
rust/library/alloctests/tests/num.rs

80 lines
1.8 KiB
Rust
Raw Normal View History

2025-06-05 23:29:12 +02:00
use core::fmt::NumBuffer;
2025-03-16 14:45:08 +01:00
use std::str::FromStr;
2025-06-05 23:29:12 +02:00
macro_rules! assert_nb {
($int:ident, $value:expr) => {
let value: $int = $value;
let s = value.to_string();
let s2 = format!("s: {}.", value);
2025-03-16 14:45:08 +01:00
2025-06-05 23:29:12 +02:00
assert_eq!(format!("s: {s}."), s2);
let Ok(ret) = $int::from_str(&s) else {
panic!("failed to convert into to string");
};
assert_eq!(ret, value);
let mut buffer = NumBuffer::<$int>::new();
assert_eq!(value.format_into(&mut buffer), s.as_str());
2025-03-16 14:45:08 +01:00
};
}
macro_rules! uint_to_s {
($($fn_name:ident, $int:ident,)+) => {
$(
#[test]
fn $fn_name() {
2025-06-05 23:29:12 +02:00
assert_nb!($int, $int::MIN);
assert_nb!($int, $int::MAX);
assert_nb!($int, 1);
assert_nb!($int, $int::MIN / 2);
assert_nb!($int, $int::MAX / 2);
2025-03-16 14:45:08 +01:00
}
)+
}
}
macro_rules! int_to_s {
($($fn_name:ident, $int:ident,)+) => {
$(
#[test]
fn $fn_name() {
2025-06-05 23:29:12 +02:00
assert_nb!($int, $int::MIN);
assert_nb!($int, $int::MAX);
assert_nb!($int, 1);
assert_nb!($int, 0);
assert_nb!($int, -1);
assert_nb!($int, $int::MIN / 2);
assert_nb!($int, $int::MAX / 2);
2025-03-16 14:45:08 +01:00
}
)+
}
}
int_to_s!(
test_i8_to_string,
i8,
test_i16_to_string,
i16,
test_i32_to_string,
i32,
test_i64_to_string,
i64,
test_isize_to_string,
isize,
2025-03-16 14:45:08 +01:00
test_i128_to_string,
i128,
);
uint_to_s!(
test_u8_to_string,
u8,
test_u16_to_string,
u16,
test_u32_to_string,
u32,
test_u64_to_string,
u64,
test_usize_to_string,
usize,
2025-03-16 14:45:08 +01:00
test_u128_to_string,
u128,
);