Skip to content

Commit bacd57a

Browse files
Add integer to string formatting tests
1 parent 5f3b84a commit bacd57a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

library/alloctests/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ mod fmt;
6363
mod heap;
6464
mod linked_list;
6565
mod misc_tests;
66+
mod num;
6667
mod rc;
6768
mod slice;
6869
mod sort;

library/alloctests/tests/num.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::fmt::{Debug, Display};
2+
use std::str::FromStr;
3+
4+
fn assert_nb<Int: ToString + FromStr + Debug + Display + Eq>(value: Int) {
5+
let s = value.to_string();
6+
let s2 = format!("{}", value);
7+
8+
assert_eq!(s, s2);
9+
let Ok(ret) = Int::from_str(&s) else {
10+
panic!("failed to convert into to string");
11+
};
12+
assert_eq!(ret, value);
13+
}
14+
15+
macro_rules! uint_to_s {
16+
($($fn_name:ident, $int:ident,)+) => {
17+
$(
18+
#[test]
19+
fn $fn_name() {
20+
assert_nb::<$int>($int::MIN);
21+
assert_nb::<$int>($int::MAX);
22+
assert_nb::<$int>(1);
23+
assert_nb::<$int>($int::MIN / 2);
24+
assert_nb::<$int>($int::MAX / 2);
25+
}
26+
)+
27+
}
28+
}
29+
macro_rules! int_to_s {
30+
($($fn_name:ident, $int:ident,)+) => {
31+
$(
32+
#[test]
33+
fn $fn_name() {
34+
assert_nb::<$int>($int::MIN);
35+
assert_nb::<$int>($int::MAX);
36+
assert_nb::<$int>(1);
37+
assert_nb::<$int>(0);
38+
assert_nb::<$int>(-1);
39+
assert_nb::<$int>($int::MIN / 2);
40+
assert_nb::<$int>($int::MAX / 2);
41+
}
42+
)+
43+
}
44+
}
45+
46+
int_to_s!(
47+
test_i8_to_string,
48+
i8,
49+
test_i16_to_string,
50+
i16,
51+
test_i32_to_string,
52+
i32,
53+
test_i64_to_string,
54+
i64,
55+
test_i128_to_string,
56+
i128,
57+
);
58+
uint_to_s!(
59+
test_u8_to_string,
60+
u8,
61+
test_u16_to_string,
62+
u16,
63+
test_u32_to_string,
64+
u32,
65+
test_u64_to_string,
66+
u64,
67+
test_u128_to_string,
68+
u128,
69+
);

0 commit comments

Comments
 (0)