From bacd57a583338b5667046abe474e728a5e881cd0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 16 Mar 2025 14:45:08 +0100 Subject: [PATCH 1/2] Add integer to string formatting tests --- library/alloctests/tests/lib.rs | 1 + library/alloctests/tests/num.rs | 69 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 library/alloctests/tests/num.rs diff --git a/library/alloctests/tests/lib.rs b/library/alloctests/tests/lib.rs index 46c11ea150bf8..f1f4cc6f93bbd 100644 --- a/library/alloctests/tests/lib.rs +++ b/library/alloctests/tests/lib.rs @@ -63,6 +63,7 @@ mod fmt; mod heap; mod linked_list; mod misc_tests; +mod num; mod rc; mod slice; mod sort; diff --git a/library/alloctests/tests/num.rs b/library/alloctests/tests/num.rs new file mode 100644 index 0000000000000..c9c0cd09ff6ab --- /dev/null +++ b/library/alloctests/tests/num.rs @@ -0,0 +1,69 @@ +use std::fmt::{Debug, Display}; +use std::str::FromStr; + +fn assert_nb(value: Int) { + let s = value.to_string(); + let s2 = format!("{}", value); + + 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, +); From 25900c2768b0b3e3d55a531c9aeb27edaccb92d1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 23 Mar 2025 22:21:41 +0100 Subject: [PATCH 2/2] Tweak integer to string conversion test a bit to be future-proof --- library/alloctests/tests/num.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/alloctests/tests/num.rs b/library/alloctests/tests/num.rs index c9c0cd09ff6ab..3c76e68c60640 100644 --- a/library/alloctests/tests/num.rs +++ b/library/alloctests/tests/num.rs @@ -3,9 +3,9 @@ use std::str::FromStr; fn assert_nb(value: Int) { let s = value.to_string(); - let s2 = format!("{}", value); + let s2 = format!("s: {}.", value); - assert_eq!(s, s2); + assert_eq!(format!("s: {s}."), s2); let Ok(ret) = Int::from_str(&s) else { panic!("failed to convert into to string"); };