Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2b1ed2

Browse files
committedMar 15, 2025·
Add integer to string formatting tests
1 parent 4d30011 commit b2b1ed2

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 

‎library/alloc/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ pub mod sync;
229229
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
230230
pub mod task;
231231
pub mod vec;
232+
#[cfg(test)]
233+
mod tests;
232234

233235
#[doc(hidden)]
234236
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]

‎library/alloc/src/tests.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)
Please sign in to comment.