Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod fmt;
mod heap;
mod linked_list;
mod misc_tests;
mod num;
mod rc;
mod slice;
mod sort;
Expand Down
69 changes: 69 additions & 0 deletions library/alloctests/tests/num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::fmt::{Debug, Display};
use std::str::FromStr;

fn assert_nb<Int: ToString + FromStr + Debug + Display + Eq>(value: Int) {
let s = value.to_string();
let s2 = format!("{}", value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth doing something like format!("s: {}.") to avoid this losing coverage if we ever impl #76531, #52804, ... -- will make the assertions a bit more complicated, but seems plausibly worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done!


assert_eq!(s, s2);
let Ok(ret) = Int::from_str(&s) else {
panic!("failed to convert into to string");
};
assert_eq!(ret, value);
}

macro_rules! uint_to_s {
($($fn_name:ident, $int:ident,)+) => {
$(
#[test]
fn $fn_name() {
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);
}
)+
}
}
macro_rules! int_to_s {
($($fn_name:ident, $int:ident,)+) => {
$(
#[test]
fn $fn_name() {
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);
}
)+
}
}

int_to_s!(
test_i8_to_string,
i8,
test_i16_to_string,
i16,
test_i32_to_string,
i32,
test_i64_to_string,
i64,
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_u128_to_string,
u128,
);
Loading