@@ -664,11 +664,32 @@ mod tests {
664
664
assert_eq ! ( third. checked_mul( 4 ) , None ) ;
665
665
}
666
666
667
+ macro_rules! test_is_power_of_two {
668
+ ( $test_name: ident, $T: ident) => (
669
+ fn $test_name( ) {
670
+ #![ test]
671
+ assert_eq!( ( 0 as $T) . is_power_of_two( ) , false ) ;
672
+ assert_eq!( ( 1 as $T) . is_power_of_two( ) , true ) ;
673
+ assert_eq!( ( 2 as $T) . is_power_of_two( ) , true ) ;
674
+ assert_eq!( ( 3 as $T) . is_power_of_two( ) , false ) ;
675
+ assert_eq!( ( 4 as $T) . is_power_of_two( ) , true ) ;
676
+ assert_eq!( ( 5 as $T) . is_power_of_two( ) , false ) ;
677
+ assert!( ( $T:: MAX / 2 + 1 ) . is_power_of_two( ) , true ) ;
678
+ }
679
+ )
680
+ }
681
+
682
+ test_is_power_of_two ! { test_is_power_of_two_u8, u8 }
683
+ test_is_power_of_two ! { test_is_power_of_two_u16, u16 }
684
+ test_is_power_of_two ! { test_is_power_of_two_u32, u32 }
685
+ test_is_power_of_two ! { test_is_power_of_two_u64, u64 }
686
+ test_is_power_of_two ! { test_is_power_of_two_uint, uint }
687
+
667
688
macro_rules! test_next_power_of_two {
668
689
( $test_name: ident, $T: ident) => (
669
690
fn $test_name( ) {
670
691
#![ test]
671
- assert_eq!( ( 0 as $T) . next_power_of_two( ) , 0 ) ;
692
+ assert_eq!( ( 0 as $T) . next_power_of_two( ) , 1 ) ;
672
693
let mut next_power = 1 ;
673
694
for i in range:: <$T>( 1 , 40 ) {
674
695
assert_eq!( i. next_power_of_two( ) , next_power) ;
@@ -688,15 +709,15 @@ mod tests {
688
709
( $test_name: ident, $T: ident) => (
689
710
fn $test_name( ) {
690
711
#![ test]
691
- assert_eq!( ( 0 as $T) . checked_next_power_of_two( ) , None ) ;
712
+ assert_eq!( ( 0 as $T) . checked_next_power_of_two( ) , Some ( 1 ) ) ;
713
+ assert!( ( $T:: MAX / 2 ) . checked_next_power_of_two( ) . is_some( ) ) ;
714
+ assert_eq!( ( $T:: MAX - 1 ) . checked_next_power_of_two( ) , None ) ;
715
+ assert_eq!( $T:: MAX . checked_next_power_of_two( ) , None ) ;
692
716
let mut next_power = 1 ;
693
717
for i in range:: <$T>( 1 , 40 ) {
694
718
assert_eq!( i. checked_next_power_of_two( ) , Some ( next_power) ) ;
695
719
if i == next_power { next_power *= 2 }
696
720
}
697
- assert!( ( $T:: MAX / 2 ) . checked_next_power_of_two( ) . is_some( ) ) ;
698
- assert_eq!( ( $T:: MAX - 1 ) . checked_next_power_of_two( ) , None ) ;
699
- assert_eq!( $T:: MAX . checked_next_power_of_two( ) , None ) ;
700
721
}
701
722
)
702
723
}
0 commit comments