Skip to content

Commit

Permalink
Auto merge of #2044 - RalfJung:int_log, r=RalfJung
Browse files Browse the repository at this point in the history
test int_log functions

I'll have to disable many of their tests in libcore since they take too long, so let's add some of them back on our side here.
  • Loading branch information
bors committed Mar 31, 2022
2 parents c1bbf07 + 811e6dd commit 6e1ed17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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));
}
}

0 comments on commit 6e1ed17

Please sign in to comment.