@@ -252,15 +252,42 @@ macro_rules! int_module {
252
252
assert_eq!( $T:: from_str_radix( "-9" , 2 ) . ok( ) , None :: <$T>) ;
253
253
}
254
254
255
+
255
256
#[ test]
256
257
fn test_pow( ) {
257
258
let mut r = 2 as $T;
258
-
259
259
assert_eq!( r. pow( 2 ) , 4 as $T) ;
260
260
assert_eq!( r. pow( 0 ) , 1 as $T) ;
261
+ assert_eq!( r. wrapping_pow( 2 ) , 4 as $T) ;
262
+ assert_eq!( r. wrapping_pow( 0 ) , 1 as $T) ;
263
+ assert_eq!( r. checked_pow( 2 ) , Some ( 4 as $T) ) ;
264
+ assert_eq!( r. checked_pow( 0 ) , Some ( 1 as $T) ) ;
265
+ assert_eq!( r. overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
266
+ assert_eq!( r. overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
267
+ assert_eq!( r. saturating_pow( 2 ) , 4 as $T) ;
268
+ assert_eq!( r. saturating_pow( 0 ) , 1 as $T) ;
269
+
270
+ r = MAX ;
271
+ // use `^` to represent .pow() with no overflow.
272
+ // if itest::MAX == 2^j-1, then itest is a `j` bit int,
273
+ // so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
274
+ // thussaturating_pow the overflowing result is exactly 1.
275
+ assert_eq!( r. wrapping_pow( 2 ) , 1 as $T) ;
276
+ assert_eq!( r. checked_pow( 2 ) , None ) ;
277
+ assert_eq!( r. overflowing_pow( 2 ) , ( 1 as $T, true ) ) ;
278
+ assert_eq!( r. saturating_pow( 2 ) , MAX ) ;
279
+ //test for negative exponent.
261
280
r = -2 as $T;
262
- assert_eq!( r. pow( 2 ) , 4 as $T) ;
263
281
assert_eq!( r. pow( 3 ) , -8 as $T) ;
282
+ assert_eq!( r. pow( 0 ) , 1 as $T) ;
283
+ assert_eq!( r. wrapping_pow( 3 ) , -8 as $T) ;
284
+ assert_eq!( r. wrapping_pow( 0 ) , 1 as $T) ;
285
+ assert_eq!( r. checked_pow( 3 ) , Some ( -8 ) as $T) ;
286
+ assert_eq!( r. checked_pow( 0 ) , Some ( 1 ) as $T) ;
287
+ assert_eq!( r. overflowing_pow( 3 ) , ( -8 as $T, false ) ) ;
288
+ assert_eq!( r. overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
289
+ assert_eq!( r. saturating_pow( 3 ) , -8 as $T) ;
290
+ assert_eq!( r. saturating_pow( 0 ) , 1 as $T) ;
264
291
}
265
292
}
266
293
} ;
0 commit comments