@@ -321,6 +321,106 @@ fn nonzero_trailing_zeros() {
321
321
assert_eq ! ( TRAILING_ZEROS , 2 ) ;
322
322
}
323
323
324
+ #[ test]
325
+ fn test_nonzero_isolate_most_significant_one ( ) {
326
+ // Signed most significant one
327
+ macro_rules! nonzero_int_impl {
328
+ ( $( $T: ty) ,+) => {
329
+ $(
330
+ {
331
+ const BITS : $T = -1 ;
332
+ const MOST_SIG_ONE : $T = 1 << ( <$T>:: BITS - 1 ) ;
333
+
334
+ // Right shift the most significant one through each
335
+ // bit position, starting with all bits set
336
+ let mut i = 0 ;
337
+ while i < <$T>:: BITS {
338
+ assert_eq!(
339
+ NonZero :: <$T>:: new( BITS >> i) . unwrap( ) . isolate_most_significant_one( ) ,
340
+ NonZero :: <$T>:: new( MOST_SIG_ONE >> i) . unwrap( ) . isolate_most_significant_one( )
341
+ ) ;
342
+ i += 1 ;
343
+ }
344
+ }
345
+ ) +
346
+ } ;
347
+ }
348
+
349
+ // Unsigned most significant one
350
+ macro_rules! nonzero_uint_impl {
351
+ ( $( $T: ty) ,+) => {
352
+ $(
353
+ {
354
+ const BITS : $T = <$T>:: MAX ;
355
+ const MOST_SIG_ONE : $T = 1 << ( <$T>:: BITS - 1 ) ;
356
+
357
+ let mut i = 0 ;
358
+ while i < <$T>:: BITS {
359
+ assert_eq!(
360
+ NonZero :: <$T>:: new( BITS >> i) . unwrap( ) . isolate_most_significant_one( ) ,
361
+ NonZero :: <$T>:: new( MOST_SIG_ONE >> i) . unwrap( ) . isolate_most_significant_one( ) ,
362
+ ) ;
363
+ i += 1 ;
364
+ }
365
+ }
366
+ ) +
367
+ } ;
368
+ }
369
+
370
+ nonzero_int_impl ! ( i8 , i16 , i32 , i64 , i128 , isize ) ;
371
+ nonzero_uint_impl ! ( u8 , u16 , u32 , u64 , u128 , usize ) ;
372
+ }
373
+
374
+ #[ test]
375
+ fn test_nonzero_isolate_least_significant_one ( ) {
376
+ // Signed least significant one
377
+ macro_rules! nonzero_int_impl {
378
+ ( $( $T: ty) ,+) => {
379
+ $(
380
+ {
381
+ const BITS : $T = -1 ;
382
+ const LEAST_SIG_ONE : $T = 1 ;
383
+
384
+ // Left shift the least significant one through each
385
+ // bit position, starting with all bits set
386
+ let mut i = 0 ;
387
+ while i < <$T>:: BITS {
388
+ assert_eq!(
389
+ NonZero :: <$T>:: new( BITS << i) . unwrap( ) . isolate_least_significant_one( ) ,
390
+ NonZero :: <$T>:: new( LEAST_SIG_ONE << i) . unwrap( ) . isolate_least_significant_one( )
391
+ ) ;
392
+ i += 1 ;
393
+ }
394
+ }
395
+ ) +
396
+ } ;
397
+ }
398
+
399
+ // Unsigned least significant one
400
+ macro_rules! nonzero_uint_impl {
401
+ ( $( $T: ty) ,+) => {
402
+ $(
403
+ {
404
+ const BITS : $T = <$T>:: MAX ;
405
+ const LEAST_SIG_ONE : $T = 1 ;
406
+
407
+ let mut i = 0 ;
408
+ while i < <$T>:: BITS {
409
+ assert_eq!(
410
+ NonZero :: <$T>:: new( BITS << i) . unwrap( ) . isolate_least_significant_one( ) ,
411
+ NonZero :: <$T>:: new( LEAST_SIG_ONE << i) . unwrap( ) . isolate_least_significant_one( ) ,
412
+ ) ;
413
+ i += 1 ;
414
+ }
415
+ }
416
+ ) +
417
+ } ;
418
+ }
419
+
420
+ nonzero_int_impl ! ( i8 , i16 , i32 , i64 , i128 , isize ) ;
421
+ nonzero_uint_impl ! ( u8 , u16 , u32 , u64 , u128 , usize ) ;
422
+ }
423
+
324
424
#[ test]
325
425
fn test_nonzero_uint_div ( ) {
326
426
let nz = NonZero :: new ( 1 ) . unwrap ( ) ;
0 commit comments