From 3ff3b66b3ea8e6b58c635fbb3cdf513fb65014f7 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Wed, 13 Nov 2024 20:12:37 -0500 Subject: [PATCH] Add tests so people messing with DecInt can catch UB with miri --- tests/path/dec_int.rs | 50 ++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/path/dec_int.rs b/tests/path/dec_int.rs index 6ce1a5123..71660a073 100644 --- a/tests/path/dec_int.rs +++ b/tests/path/dec_int.rs @@ -1,20 +1,40 @@ use rustix::path::DecInt; +macro_rules! check { + ($i:expr) => { + let i = $i; + assert_eq!(DecInt::new(i).as_ref().to_str().unwrap(), i.to_string()); + }; +} + #[test] fn test_dec_int() { - assert_eq!(DecInt::new(0).as_ref().to_str().unwrap(), "0"); - assert_eq!(DecInt::new(-1).as_ref().to_str().unwrap(), "-1"); - assert_eq!(DecInt::new(789).as_ref().to_str().unwrap(), "789"); - assert_eq!( - DecInt::new(i64::MIN).as_ref().to_str().unwrap(), - i64::MIN.to_string() - ); - assert_eq!( - DecInt::new(i64::MAX).as_ref().to_str().unwrap(), - i64::MAX.to_string() - ); - assert_eq!( - DecInt::new(u64::MAX).as_ref().to_str().unwrap(), - u64::MAX.to_string() - ); + check!(0); + check!(-1); + check!(789); + + check!(u8::MAX); + check!(i8::MIN); + check!(u16::MAX); + check!(i16::MIN); + check!(u32::MAX); + check!(i32::MIN); + check!(u64::MAX); + check!(i64::MIN); + #[cfg(any( + target_pointer_width = "16", + target_pointer_width = "32", + target_pointer_width = "64" + ))] + { + check!(usize::MAX); + check!(isize::MIN); + } +} + +#[test] +#[should_panic] +fn test_unsupported_dec_int() { + check!(u128::MAX); + check!(i128::MIN); }