Skip to content

Commit

Permalink
Add tests so people messing with DecInt can catch UB with miri
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Saveau committed Nov 20, 2024
1 parent b0f5234 commit 3ff3b66
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions tests/path/dec_int.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 3ff3b66

Please sign in to comment.