Skip to content

Commit 8859812

Browse files
committed
Improved tests of isqrt and checked_isqrt implementations
* Inputs chosen more thoroughly and systematically. * Checks that `isqrt` and `checked_isqrt` have equivalent results for signed types, either equivalent numerically or equivalent as a panic and a `None`. * Checks that `isqrt` has numerically-equivalent results for unsigned types and their `NonZero` counterparts.
1 parent 8bfcae7 commit 8859812

File tree

3 files changed

+366
-32
lines changed

3 files changed

+366
-32
lines changed

library/core/tests/num/int_macros.rs

-32
Original file line numberDiff line numberDiff line change
@@ -290,38 +290,6 @@ macro_rules! int_module {
290290
assert_eq!(r.saturating_pow(0), 1 as $T);
291291
}
292292

293-
#[test]
294-
fn test_isqrt() {
295-
assert_eq!($T::MIN.checked_isqrt(), None);
296-
assert_eq!((-1 as $T).checked_isqrt(), None);
297-
assert_eq!((0 as $T).isqrt(), 0 as $T);
298-
assert_eq!((1 as $T).isqrt(), 1 as $T);
299-
assert_eq!((2 as $T).isqrt(), 1 as $T);
300-
assert_eq!((99 as $T).isqrt(), 9 as $T);
301-
assert_eq!((100 as $T).isqrt(), 10 as $T);
302-
}
303-
304-
#[cfg(not(miri))] // Miri is too slow
305-
#[test]
306-
fn test_lots_of_isqrt() {
307-
let n_max: $T = (1024 * 1024).min($T::MAX as u128) as $T;
308-
for n in 0..=n_max {
309-
let isqrt: $T = n.isqrt();
310-
311-
assert!(isqrt.pow(2) <= n);
312-
let (square, overflow) = (isqrt + 1).overflowing_pow(2);
313-
assert!(overflow || square > n);
314-
}
315-
316-
for n in ($T::MAX - 127)..=$T::MAX {
317-
let isqrt: $T = n.isqrt();
318-
319-
assert!(isqrt.pow(2) <= n);
320-
let (square, overflow) = (isqrt + 1).overflowing_pow(2);
321-
assert!(overflow || square > n);
322-
}
323-
}
324-
325293
#[test]
326294
fn test_div_floor() {
327295
let a: $T = 8;

0 commit comments

Comments
 (0)