@@ -184,6 +184,31 @@ macro_rules! uint_module {
184
184
assert_eq!( $T:: from_str_radix( "Z" , 10 ) . ok( ) , None :: <$T>) ;
185
185
assert_eq!( $T:: from_str_radix( "_" , 2 ) . ok( ) , None :: <$T>) ;
186
186
}
187
+
188
+ #[ test]
189
+ fn test_pow( ) {
190
+ let mut r = 2 as $T;
191
+ assert_eq!( r. pow( 2 ) , 4 as $T) ;
192
+ assert_eq!( r. pow( 0 ) , 1 as $T) ;
193
+ assert_eq!( r. wrapping_pow( 2 ) , 4 as $T) ;
194
+ assert_eq!( r. wrapping_pow( 0 ) , 1 as $T) ;
195
+ assert_eq!( r. checked_pow( 2 ) , Some ( 4 as $T) ) ;
196
+ assert_eq!( r. checked_pow( 0 ) , Some ( 1 as $T) ) ;
197
+ assert_eq!( r. overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
198
+ assert_eq!( r. overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
199
+ assert_eq!( r. saturating_pow( 2 ) , 4 as $T) ;
200
+ assert_eq!( r. saturating_pow( 0 ) , 1 as $T) ;
201
+
202
+ r = MAX ;
203
+ // use `^` to represent .pow() with no overflow.
204
+ // if itest::MAX == 2^j-1, then itest is a `j` bit int,
205
+ // so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
206
+ // thussaturating_pow the overflowing result is exactly 1.
207
+ assert_eq!( r. wrapping_pow( 2 ) , 1 as $T) ;
208
+ assert_eq!( r. checked_pow( 2 ) , None ) ;
209
+ assert_eq!( r. overflowing_pow( 2 ) , ( 1 as $T, true ) ) ;
210
+ assert_eq!( r. saturating_pow( 2 ) , MAX ) ;
211
+ }
187
212
}
188
213
} ;
189
214
}
0 commit comments