@@ -7,24 +7,21 @@ macro_rules! tests {
7
7
// Check that the following produce the correct values from
8
8
// `isqrt`:
9
9
//
10
- // * `<$T>::MIN` (for signed types, no nonnegative value can
11
- // negate to `<$T>::MIN)
12
10
// * the first and last 128 nonnegative values
13
11
// * powers of two, minus one
14
12
// * powers of two
15
13
//
16
- // For signed types, check that `checked_isqrt` and
17
- // `isqrt` either produce the same numeric value or
18
- // respectively produce `None` and a panic.
14
+ // For signed types, check that `checked_isqrt` and `isqrt`
15
+ // either produce the same numeric value or respectively
16
+ // produce `None` and a panic. Make sure to do a consistency
17
+ // check for `<$T>::MIN` as well, as no nonnegative values
18
+ // negate to it.
19
19
//
20
20
// For unsigned types check that `isqrt` produces the same
21
21
// numeric value for `$T` and `NonZero<$T>`.
22
22
#[ test]
23
23
fn isqrt( ) {
24
- // Check the minimum value because, for signed types,
25
- // there's no nonnegative value that can be negated into
26
- // the minimum value.
27
- isqrt_consistency_check( $T:: MIN ) ;
24
+ isqrt_consistency_check( <$T>:: MIN ) ;
28
25
29
26
for n in ( 0 ..=127 )
30
27
. chain( <$T>:: MAX - 127 ..=<$T>:: MAX )
@@ -35,11 +32,17 @@ macro_rules! tests {
35
32
36
33
let isqrt_n = n. isqrt( ) ;
37
34
assert!(
38
- isqrt_n. checked_mul( isqrt_n) . map( |isqrt_n_squared| isqrt_n_squared <= n) . unwrap_or( false ) ,
35
+ isqrt_n
36
+ . checked_mul( isqrt_n)
37
+ . map( |isqrt_n_squared| isqrt_n_squared <= n)
38
+ . unwrap_or( false ) ,
39
39
"`{n}.isqrt()` should be lower than {isqrt_n}."
40
40
) ;
41
41
assert!(
42
- ( isqrt_n + 1 ) . checked_mul( isqrt_n + 1 ) . map( |isqrt_n_plus_1_squared| n < isqrt_n_plus_1_squared) . unwrap_or( true ) ,
42
+ ( isqrt_n + 1 )
43
+ . checked_mul( isqrt_n + 1 )
44
+ . map( |isqrt_n_plus_1_squared| n < isqrt_n_plus_1_squared)
45
+ . unwrap_or( true ) ,
43
46
"`{n}.isqrt()` should be higher than {isqrt_n})."
44
47
) ;
45
48
}
@@ -173,8 +176,8 @@ macro_rules! signed_check {
173
176
( $T: ident) => {
174
177
/// This takes an input and, if it's nonnegative or
175
178
#[ doc = concat!( "`" , stringify!( $T) , "::MIN`," ) ]
176
- /// checks that `isqrt` and `checked_isqrt` produce equivalent
177
- /// results for that input and for the negative of that input.
179
+ /// checks that `isqrt` and `checked_isqrt` produce equivalent results
180
+ /// for that input and for the negative of that input.
178
181
fn isqrt_consistency_check( n: $T) {
179
182
// `<$T>::MIN` will be negative, so ignore it in this nonnegative
180
183
// section.
@@ -186,8 +189,8 @@ macro_rules! signed_check {
186
189
) ;
187
190
}
188
191
189
- // `wrapping_neg` so that `<$T>::MIN` will negate to
190
- // itself rather than panicking.
192
+ // `wrapping_neg` so that `<$T>::MIN` will negate to itself rather
193
+ // than panicking.
191
194
let negative_n = n. wrapping_neg( ) ;
192
195
193
196
// Zero negated will still be nonnegative, so ignore it in this
@@ -209,8 +212,8 @@ macro_rules! signed_check {
209
212
210
213
macro_rules! unsigned_check {
211
214
( $T: ident) => {
212
- /// This takes an input and, if it's nonzero, checks that
213
- /// `isqrt` produces the same numeric value for both
215
+ /// This takes an input and, if it's nonzero, checks that `isqrt`
216
+ /// produces the same numeric value for both
214
217
#[ doc = concat!( "`" , stringify!( $T) , "` and " ) ]
215
218
#[ doc = concat!( "`NonZero<" , stringify!( $T) , ">`." ) ]
216
219
fn isqrt_consistency_check( n: $T) {
@@ -220,7 +223,9 @@ macro_rules! unsigned_check {
220
223
assert_eq!(
221
224
n. isqrt( ) ,
222
225
:: core:: num:: NonZero :: <$T>:: new( n)
223
- . expect( "Was not able to create a new `NonZero` value from a nonzero value" )
226
+ . expect(
227
+ "Was not able to create a new `NonZero` value from a nonzero number."
228
+ )
224
229
. isqrt( )
225
230
. get( ) ,
226
231
"`{n}.isqrt` should match `NonZero`'s `{n}.isqrt().get()`." ,
0 commit comments