Skip to content

test int_log functions #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100f12d17026fccfc5d80527b5976dd66b228b13
df20355fa9fa5e9fb89be4e4bfee8a643bb7a23e
29 changes: 29 additions & 0 deletions tests/run-pass/integer-ops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -Coverflow-checks=off
#![feature(int_log)]
#![allow(arithmetic_overflow)]

pub fn main() {
Expand Down Expand Up @@ -171,4 +172,32 @@ pub fn main() {
assert_eq!(10i8.overflowing_abs(), (10,false));
assert_eq!((-10i8).overflowing_abs(), (10,false));
assert_eq!((-128i8).overflowing_abs(), (-128,true));

// Logarithms
macro_rules! test_log {
($type:ident, $max_log2:expr, $max_log10:expr) => {
assert_eq!($type::MIN.checked_log2(), None);
assert_eq!($type::MIN.checked_log10(), None);
assert_eq!($type::MAX.checked_log2(), Some($max_log2));
assert_eq!($type::MAX.checked_log10(), Some($max_log10));
}
}

test_log!(i8, 6, 2);
test_log!(u8, 7, 2);
test_log!(i16, 14, 4);
test_log!(u16, 15, 4);
test_log!(i32, 30, 9);
test_log!(u32, 31, 9);
test_log!(i64, 62, 18);
test_log!(u64, 63, 19);
test_log!(i128, 126, 38);
test_log!(u128, 127, 38);

for i in (1..=i16::MAX).step_by(i8::MAX as usize) {
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32));
}
for i in (1..=u16::MAX).step_by(i8::MAX as usize) {
assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32));
}
}