Skip to content

Commit 8795845

Browse files
authored
add extra tests.
finished adding the extra tests to prevent further typo
1 parent f3d476b commit 8795845

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libcore/tests/num/uint_macros.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,31 @@ macro_rules! uint_module {
184184
assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
185185
assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
186186
}
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+
}
187212
}
188213
};
189214
}

0 commit comments

Comments
 (0)