1 macro_rules
! int_module
{
2 ($T
:ident
, $T_i
:ident
) => {
6 use core
::ops
::{BitAnd, BitOr, BitXor, Not, Shl, Shr}
;
15 assert_eq
!(MIN
+ MAX
+ 1, 0);
20 num
::test_num(10 as $T
, 2 as $T
);
24 fn test_rem_euclid() {
25 assert_eq
!((-1 as $T
).rem_euclid(MIN
), MAX
);
30 assert_eq
!((1 as $T
).abs(), 1 as $T
);
31 assert_eq
!((0 as $T
).abs(), 0 as $T
);
32 assert_eq
!((-1 as $T
).abs(), 1 as $T
);
37 assert_eq
!((1 as $T
).signum(), 1 as $T
);
38 assert_eq
!((0 as $T
).signum(), 0 as $T
);
39 assert_eq
!((-0 as $T
).signum(), 0 as $T
);
40 assert_eq
!((-1 as $T
).signum(), -1 as $T
);
44 fn test_is_positive() {
45 assert
!((1 as $T
).is_positive());
46 assert
!(!(0 as $T
).is_positive());
47 assert
!(!(-0 as $T
).is_positive());
48 assert
!(!(-1 as $T
).is_positive());
52 fn test_is_negative() {
53 assert
!(!(1 as $T
).is_negative());
54 assert
!(!(0 as $T
).is_negative());
55 assert
!(!(-0 as $T
).is_negative());
56 assert
!((-1 as $T
).is_negative());
60 fn test_bitwise_operators() {
61 assert_eq
!(0b1110 as $T
, (0b1100 as $T
).bitor(0b1010 as $T
));
62 assert_eq
!(0b1000 as $T
, (0b1100 as $T
).bitand(0b1010 as $T
));
63 assert_eq
!(0b0110 as $T
, (0b1100 as $T
).bitxor(0b1010 as $T
));
64 assert_eq
!(0b1110 as $T
, (0b0111 as $T
).shl(1));
65 assert_eq
!(0b0111 as $T
, (0b1110 as $T
).shr(1));
66 assert_eq
!(-(0b11 as $T
) - (1 as $T
), (0b11 as $T
).not());
69 const A
: $T
= 0b0101100;
70 const B
: $T
= 0b0100001;
71 const C
: $T
= 0b1111001;
77 fn test_count_ones() {
78 assert_eq
!(A
.count_ones(), 3);
79 assert_eq
!(B
.count_ones(), 2);
80 assert_eq
!(C
.count_ones(), 5);
84 fn test_count_zeros() {
85 let bits
= mem
::size_of
::<$T
>() * 8;
86 assert_eq
!(A
.count_zeros(), bits
as u32 - 3);
87 assert_eq
!(B
.count_zeros(), bits
as u32 - 2);
88 assert_eq
!(C
.count_zeros(), bits
as u32 - 5);
92 fn test_leading_trailing_ones() {
93 let bits
= (mem
::size_of
::<$T
>() * 8) as u32;
95 let a
: $T
= 0b0101_1111;
96 assert_eq
!(a
.trailing_ones(), 5);
97 assert_eq
!((!a
).leading_ones(), bits
- 7);
99 assert_eq
!(a
.reverse_bits().leading_ones(), 5);
101 assert_eq
!(_1
.leading_ones(), bits
);
102 assert_eq
!(_1
.trailing_ones(), bits
);
104 assert_eq
!((_1
<< 1).trailing_ones(), 0);
105 assert_eq
!(MAX
.leading_ones(), 0);
107 assert_eq
!((_1
<< 1).leading_ones(), bits
- 1);
108 assert_eq
!(MAX
.trailing_ones(), bits
- 1);
110 assert_eq
!(_0
.leading_ones(), 0);
111 assert_eq
!(_0
.trailing_ones(), 0);
113 let x
: $T
= 0b0010_1100;
114 assert_eq
!(x
.leading_ones(), 0);
115 assert_eq
!(x
.trailing_ones(), 0);
120 assert_eq
!(A
.rotate_left(6).rotate_right(2).rotate_right(4), A
);
121 assert_eq
!(B
.rotate_left(3).rotate_left(2).rotate_right(5), B
);
122 assert_eq
!(C
.rotate_left(6).rotate_right(2).rotate_right(4), C
);
124 // Rotating these should make no difference
126 // We test using 124 bits because to ensure that overlong bit shifts do
127 // not cause undefined behaviour. See #10183.
128 assert_eq
!(_0
.rotate_left(124), _0
);
129 assert_eq
!(_1
.rotate_left(124), _1
);
130 assert_eq
!(_0
.rotate_right(124), _0
);
131 assert_eq
!(_1
.rotate_right(124), _1
);
133 // Rotating by 0 should have no effect
134 assert_eq
!(A
.rotate_left(0), A
);
135 assert_eq
!(B
.rotate_left(0), B
);
136 assert_eq
!(C
.rotate_left(0), C
);
137 // Rotating by a multiple of word size should also have no effect
138 assert_eq
!(A
.rotate_left(64), A
);
139 assert_eq
!(B
.rotate_left(64), B
);
140 assert_eq
!(C
.rotate_left(64), C
);
144 fn test_swap_bytes() {
145 assert_eq
!(A
.swap_bytes().swap_bytes(), A
);
146 assert_eq
!(B
.swap_bytes().swap_bytes(), B
);
147 assert_eq
!(C
.swap_bytes().swap_bytes(), C
);
149 // Swapping these should make no difference
150 assert_eq
!(_0
.swap_bytes(), _0
);
151 assert_eq
!(_1
.swap_bytes(), _1
);
156 assert_eq
!($T
::from_le(A
.to_le()), A
);
157 assert_eq
!($T
::from_le(B
.to_le()), B
);
158 assert_eq
!($T
::from_le(C
.to_le()), C
);
159 assert_eq
!($T
::from_le(_0
), _0
);
160 assert_eq
!($T
::from_le(_1
), _1
);
161 assert_eq
!(_0
.to_le(), _0
);
162 assert_eq
!(_1
.to_le(), _1
);
167 assert_eq
!($T
::from_be(A
.to_be()), A
);
168 assert_eq
!($T
::from_be(B
.to_be()), B
);
169 assert_eq
!($T
::from_be(C
.to_be()), C
);
170 assert_eq
!($T
::from_be(_0
), _0
);
171 assert_eq
!($T
::from_be(_1
), _1
);
172 assert_eq
!(_0
.to_be(), _0
);
173 assert_eq
!(_1
.to_be(), _1
);
177 fn test_signed_checked_div() {
178 assert_eq
!((10 as $T
).checked_div(2), Some(5));
179 assert_eq
!((5 as $T
).checked_div(0), None
);
180 assert_eq
!(isize::MIN
.checked_div(-1), None
);
184 fn test_saturating_abs() {
185 assert_eq
!((0 as $T
).saturating_abs(), 0);
186 assert_eq
!((123 as $T
).saturating_abs(), 123);
187 assert_eq
!((-123 as $T
).saturating_abs(), 123);
188 assert_eq
!((MAX
- 2).saturating_abs(), MAX
- 2);
189 assert_eq
!((MAX
- 1).saturating_abs(), MAX
- 1);
190 assert_eq
!(MAX
.saturating_abs(), MAX
);
191 assert_eq
!((MIN
+ 2).saturating_abs(), MAX
- 1);
192 assert_eq
!((MIN
+ 1).saturating_abs(), MAX
);
193 assert_eq
!(MIN
.saturating_abs(), MAX
);
197 fn test_saturating_neg() {
198 assert_eq
!((0 as $T
).saturating_neg(), 0);
199 assert_eq
!((123 as $T
).saturating_neg(), -123);
200 assert_eq
!((-123 as $T
).saturating_neg(), 123);
201 assert_eq
!((MAX
- 2).saturating_neg(), MIN
+ 3);
202 assert_eq
!((MAX
- 1).saturating_neg(), MIN
+ 2);
203 assert_eq
!(MAX
.saturating_neg(), MIN
+ 1);
204 assert_eq
!((MIN
+ 2).saturating_neg(), MAX
- 1);
205 assert_eq
!((MIN
+ 1).saturating_neg(), MAX
);
206 assert_eq
!(MIN
.saturating_neg(), MAX
);
211 fn from_str
<T
: ::std
::str::FromStr
>(t
: &str) -> Option
<T
> {
212 ::std
::str::FromStr
::from_str(t
).ok()
214 assert_eq
!(from_str
::<$T
>("0"), Some(0 as $T
));
215 assert_eq
!(from_str
::<$T
>("3"), Some(3 as $T
));
216 assert_eq
!(from_str
::<$T
>("10"), Some(10 as $T
));
217 assert_eq
!(from_str
::<i32>("123456789"), Some(123456789 as i32));
218 assert_eq
!(from_str
::<$T
>("00100"), Some(100 as $T
));
220 assert_eq
!(from_str
::<$T
>("-1"), Some(-1 as $T
));
221 assert_eq
!(from_str
::<$T
>("-3"), Some(-3 as $T
));
222 assert_eq
!(from_str
::<$T
>("-10"), Some(-10 as $T
));
223 assert_eq
!(from_str
::<i32>("-123456789"), Some(-123456789 as i32));
224 assert_eq
!(from_str
::<$T
>("-00100"), Some(-100 as $T
));
226 assert_eq
!(from_str
::<$T
>(""), None
);
227 assert_eq
!(from_str
::<$T
>(" "), None
);
228 assert_eq
!(from_str
::<$T
>("x"), None
);
232 fn test_from_str_radix() {
233 assert_eq
!($T
::from_str_radix("123", 10), Ok(123 as $T
));
234 assert_eq
!($T
::from_str_radix("1001", 2), Ok(9 as $T
));
235 assert_eq
!($T
::from_str_radix("123", 8), Ok(83 as $T
));
236 assert_eq
!(i32::from_str_radix("123", 16), Ok(291 as i32));
237 assert_eq
!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
238 assert_eq
!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
239 assert_eq
!($T
::from_str_radix("z", 36), Ok(35 as $T
));
240 assert_eq
!($T
::from_str_radix("Z", 36), Ok(35 as $T
));
242 assert_eq
!($T
::from_str_radix("-123", 10), Ok(-123 as $T
));
243 assert_eq
!($T
::from_str_radix("-1001", 2), Ok(-9 as $T
));
244 assert_eq
!($T
::from_str_radix("-123", 8), Ok(-83 as $T
));
245 assert_eq
!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
246 assert_eq
!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
247 assert_eq
!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
248 assert_eq
!($T
::from_str_radix("-z", 36), Ok(-35 as $T
));
249 assert_eq
!($T
::from_str_radix("-Z", 36), Ok(-35 as $T
));
251 assert_eq
!($T
::from_str_radix("Z", 35).ok(), None
::<$T
>);
252 assert_eq
!($T
::from_str_radix("-9", 2).ok(), None
::<$T
>);
259 assert_eq
!(r
.pow(2), 4 as $T
);
260 assert_eq
!(r
.pow(0), 1 as $T
);
262 assert_eq
!(r
.pow(2), 4 as $T
);
263 assert_eq
!(r
.pow(3), -8 as $T
);