|
1 | 1 | // compile-flags: -Coverflow-checks=off
|
| 2 | +#![feature(int_log)] |
2 | 3 | #![allow(arithmetic_overflow)]
|
3 | 4 |
|
4 | 5 | pub fn main() {
|
@@ -171,4 +172,32 @@ pub fn main() {
|
171 | 172 | assert_eq!(10i8.overflowing_abs(), (10,false));
|
172 | 173 | assert_eq!((-10i8).overflowing_abs(), (10,false));
|
173 | 174 | assert_eq!((-128i8).overflowing_abs(), (-128,true));
|
| 175 | + |
| 176 | + // Logarithms |
| 177 | + macro_rules! test_log { |
| 178 | + ($type:ident, $max_log2:expr, $max_log10:expr) => { |
| 179 | + assert_eq!($type::MIN.checked_log2(), None); |
| 180 | + assert_eq!($type::MIN.checked_log10(), None); |
| 181 | + assert_eq!($type::MAX.checked_log2(), Some($max_log2)); |
| 182 | + assert_eq!($type::MAX.checked_log10(), Some($max_log10)); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + test_log!(i8, 6, 2); |
| 187 | + test_log!(u8, 7, 2); |
| 188 | + test_log!(i16, 14, 4); |
| 189 | + test_log!(u16, 15, 4); |
| 190 | + test_log!(i32, 30, 9); |
| 191 | + test_log!(u32, 31, 9); |
| 192 | + test_log!(i64, 62, 18); |
| 193 | + test_log!(u64, 63, 19); |
| 194 | + test_log!(i128, 126, 38); |
| 195 | + test_log!(u128, 127, 38); |
| 196 | + |
| 197 | + for i in (1..=i16::MAX).step_by(i8::MAX as usize) { |
| 198 | + assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32)); |
| 199 | + } |
| 200 | + for i in (1..=u16::MAX).step_by(i8::MAX as usize) { |
| 201 | + assert_eq!(i.checked_log(13), Some((i as f32).log(13.0) as u32)); |
| 202 | + } |
174 | 203 | }
|
0 commit comments