1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 macro_rules
! uint_module
{ ($T
:ident
, $T_i
:ident
) => (
16 use core
::ops
::{BitOr, BitAnd, BitXor, Shl, Shr, Not}
;
17 use std
::str::FromStr
;
24 assert
!((MIN
+ MAX
).wrapping_add(1) == 0);
29 num
::test_num(10 as $T
, 2 as $T
);
33 fn test_bitwise_operators() {
34 assert
!(0b1110 as $T
== (0b1100 as $T
).bitor(0b1010 as $T
));
35 assert
!(0b1000 as $T
== (0b1100 as $T
).bitand(0b1010 as $T
));
36 assert
!(0b0110 as $T
== (0b1100 as $T
).bitxor(0b1010 as $T
));
37 assert
!(0b1110 as $T
== (0b0111 as $T
).shl(1));
38 assert
!(0b0111 as $T
== (0b1110 as $T
).shr(1));
39 assert
!(MAX
- (0b1011 as $T
) == (0b1011 as $T
).not());
42 const A
: $T
= 0b0101100;
43 const B
: $T
= 0b0100001;
44 const C
: $T
= 0b1111001;
50 fn test_count_ones() {
51 assert
!(A
.count_ones() == 3);
52 assert
!(B
.count_ones() == 2);
53 assert
!(C
.count_ones() == 5);
57 fn test_count_zeros() {
58 let bits
= mem
::size_of
::<$T
>() * 8;
59 assert
!(A
.count_zeros() == bits
as u32 - 3);
60 assert
!(B
.count_zeros() == bits
as u32 - 2);
61 assert
!(C
.count_zeros() == bits
as u32 - 5);
66 assert_eq
!(A
.rotate_left(6).rotate_right(2).rotate_right(4), A
);
67 assert_eq
!(B
.rotate_left(3).rotate_left(2).rotate_right(5), B
);
68 assert_eq
!(C
.rotate_left(6).rotate_right(2).rotate_right(4), C
);
70 // Rotating these should make no difference
72 // We test using 124 bits because to ensure that overlong bit shifts do
73 // not cause undefined behaviour. See #10183.
74 assert_eq
!(_0
.rotate_left(124), _0
);
75 assert_eq
!(_1
.rotate_left(124), _1
);
76 assert_eq
!(_0
.rotate_right(124), _0
);
77 assert_eq
!(_1
.rotate_right(124), _1
);
79 // Rotating by 0 should have no effect
80 assert_eq
!(A
.rotate_left(0), A
);
81 assert_eq
!(B
.rotate_left(0), B
);
82 assert_eq
!(C
.rotate_left(0), C
);
83 // Rotating by a multiple of word size should also have no effect
84 assert_eq
!(A
.rotate_left(64), A
);
85 assert_eq
!(B
.rotate_left(64), B
);
86 assert_eq
!(C
.rotate_left(64), C
);
90 fn test_swap_bytes() {
91 assert_eq
!(A
.swap_bytes().swap_bytes(), A
);
92 assert_eq
!(B
.swap_bytes().swap_bytes(), B
);
93 assert_eq
!(C
.swap_bytes().swap_bytes(), C
);
95 // Swapping these should make no difference
96 assert_eq
!(_0
.swap_bytes(), _0
);
97 assert_eq
!(_1
.swap_bytes(), _1
);
101 fn test_reverse_bits() {
102 assert_eq
!(A
.reverse_bits().reverse_bits(), A
);
103 assert_eq
!(B
.reverse_bits().reverse_bits(), B
);
104 assert_eq
!(C
.reverse_bits().reverse_bits(), C
);
106 // Swapping these should make no difference
107 assert_eq
!(_0
.reverse_bits(), _0
);
108 assert_eq
!(_1
.reverse_bits(), _1
);
113 assert_eq
!($T
::from_le(A
.to_le()), A
);
114 assert_eq
!($T
::from_le(B
.to_le()), B
);
115 assert_eq
!($T
::from_le(C
.to_le()), C
);
116 assert_eq
!($T
::from_le(_0
), _0
);
117 assert_eq
!($T
::from_le(_1
), _1
);
118 assert_eq
!(_0
.to_le(), _0
);
119 assert_eq
!(_1
.to_le(), _1
);
124 assert_eq
!($T
::from_be(A
.to_be()), A
);
125 assert_eq
!($T
::from_be(B
.to_be()), B
);
126 assert_eq
!($T
::from_be(C
.to_be()), C
);
127 assert_eq
!($T
::from_be(_0
), _0
);
128 assert_eq
!($T
::from_be(_1
), _1
);
129 assert_eq
!(_0
.to_be(), _0
);
130 assert_eq
!(_1
.to_be(), _1
);
134 fn test_unsigned_checked_div() {
135 assert
!((10 as $T
).checked_div(2) == Some(5));
136 assert
!((5 as $T
).checked_div(0) == None
);
139 fn from_str
<T
: FromStr
>(t
: &str) -> Option
<T
> {
140 FromStr
::from_str(t
).ok()
144 pub fn test_from_str() {
145 assert_eq
!(from_str
::<$T
>("0"), Some(0 as $T
));
146 assert_eq
!(from_str
::<$T
>("3"), Some(3 as $T
));
147 assert_eq
!(from_str
::<$T
>("10"), Some(10 as $T
));
148 assert_eq
!(from_str
::<u32>("123456789"), Some(123456789 as u32));
149 assert_eq
!(from_str
::<$T
>("00100"), Some(100 as $T
));
151 assert_eq
!(from_str
::<$T
>(""), None
);
152 assert_eq
!(from_str
::<$T
>(" "), None
);
153 assert_eq
!(from_str
::<$T
>("x"), None
);
157 pub fn test_parse_bytes() {
158 assert_eq
!($T
::from_str_radix("123", 10), Ok(123 as $T
));
159 assert_eq
!($T
::from_str_radix("1001", 2), Ok(9 as $T
));
160 assert_eq
!($T
::from_str_radix("123", 8), Ok(83 as $T
));
161 assert_eq
!(u16::from_str_radix("123", 16), Ok(291 as u16));
162 assert_eq
!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
163 assert_eq
!($T
::from_str_radix("z", 36), Ok(35 as $T
));
165 assert_eq
!($T
::from_str_radix("Z", 10).ok(), None
::<$T
>);
166 assert_eq
!($T
::from_str_radix("_", 2).ok(), None
::<$T
>);