]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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. | |
4 | // | |
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. | |
10 | ||
9346a6ac | 11 | macro_rules! int_module { ($T:ident, $T_i:ident) => ( |
1a4d82fc JJ |
12 | #[cfg(test)] |
13 | mod tests { | |
14 | use core::$T_i::*; | |
c34b1796 | 15 | use core::isize; |
1a4d82fc | 16 | use core::ops::{Shl, Shr, Not, BitXor, BitAnd, BitOr}; |
7453a54e SL |
17 | use core::mem; |
18 | ||
1a4d82fc JJ |
19 | use num; |
20 | ||
21 | #[test] | |
22 | fn test_overflows() { | |
23 | assert!(MAX > 0); | |
24 | assert!(MIN <= 0); | |
25 | assert!(MIN + MAX + 1 == 0); | |
26 | } | |
27 | ||
28 | #[test] | |
29 | fn test_num() { | |
30 | num::test_num(10 as $T, 2 as $T); | |
31 | } | |
32 | ||
33 | #[test] | |
34 | pub fn test_abs() { | |
35 | assert!((1 as $T).abs() == 1 as $T); | |
36 | assert!((0 as $T).abs() == 0 as $T); | |
37 | assert!((-1 as $T).abs() == 1 as $T); | |
38 | } | |
39 | ||
40 | #[test] | |
41 | fn test_signum() { | |
42 | assert!((1 as $T).signum() == 1 as $T); | |
43 | assert!((0 as $T).signum() == 0 as $T); | |
44 | assert!((-0 as $T).signum() == 0 as $T); | |
45 | assert!((-1 as $T).signum() == -1 as $T); | |
46 | } | |
47 | ||
48 | #[test] | |
49 | fn test_is_positive() { | |
50 | assert!((1 as $T).is_positive()); | |
51 | assert!(!(0 as $T).is_positive()); | |
52 | assert!(!(-0 as $T).is_positive()); | |
53 | assert!(!(-1 as $T).is_positive()); | |
54 | } | |
55 | ||
56 | #[test] | |
57 | fn test_is_negative() { | |
58 | assert!(!(1 as $T).is_negative()); | |
59 | assert!(!(0 as $T).is_negative()); | |
60 | assert!(!(-0 as $T).is_negative()); | |
61 | assert!((-1 as $T).is_negative()); | |
62 | } | |
63 | ||
64 | #[test] | |
65 | fn test_bitwise_operators() { | |
66 | assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T)); | |
67 | assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T)); | |
68 | assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T)); | |
69 | assert!(0b1110 as $T == (0b0111 as $T).shl(1)); | |
70 | assert!(0b0111 as $T == (0b1110 as $T).shr(1)); | |
71 | assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not()); | |
72 | } | |
73 | ||
c34b1796 AL |
74 | const A: $T = 0b0101100; |
75 | const B: $T = 0b0100001; | |
76 | const C: $T = 0b1111001; | |
1a4d82fc | 77 | |
c34b1796 AL |
78 | const _0: $T = 0; |
79 | const _1: $T = !0; | |
1a4d82fc JJ |
80 | |
81 | #[test] | |
82 | fn test_count_ones() { | |
83 | assert!(A.count_ones() == 3); | |
84 | assert!(B.count_ones() == 2); | |
85 | assert!(C.count_ones() == 5); | |
86 | } | |
87 | ||
88 | #[test] | |
89 | fn test_count_zeros() { | |
7453a54e SL |
90 | let bits = mem::size_of::<$T>() * 8; |
91 | assert!(A.count_zeros() == bits as u32 - 3); | |
92 | assert!(B.count_zeros() == bits as u32 - 2); | |
93 | assert!(C.count_zeros() == bits as u32 - 5); | |
1a4d82fc JJ |
94 | } |
95 | ||
96 | #[test] | |
97 | fn test_rotate() { | |
98 | assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A); | |
99 | assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B); | |
100 | assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C); | |
101 | ||
102 | // Rotating these should make no difference | |
103 | // | |
104 | // We test using 124 bits because to ensure that overlong bit shifts do | |
105 | // not cause undefined behaviour. See #10183. | |
106 | assert_eq!(_0.rotate_left(124), _0); | |
107 | assert_eq!(_1.rotate_left(124), _1); | |
108 | assert_eq!(_0.rotate_right(124), _0); | |
109 | assert_eq!(_1.rotate_right(124), _1); | |
110 | ||
111 | // Rotating by 0 should have no effect | |
112 | assert_eq!(A.rotate_left(0), A); | |
113 | assert_eq!(B.rotate_left(0), B); | |
114 | assert_eq!(C.rotate_left(0), C); | |
115 | // Rotating by a multiple of word size should also have no effect | |
116 | assert_eq!(A.rotate_left(64), A); | |
117 | assert_eq!(B.rotate_left(64), B); | |
118 | assert_eq!(C.rotate_left(64), C); | |
119 | } | |
120 | ||
121 | #[test] | |
122 | fn test_swap_bytes() { | |
123 | assert_eq!(A.swap_bytes().swap_bytes(), A); | |
124 | assert_eq!(B.swap_bytes().swap_bytes(), B); | |
125 | assert_eq!(C.swap_bytes().swap_bytes(), C); | |
126 | ||
127 | // Swapping these should make no difference | |
128 | assert_eq!(_0.swap_bytes(), _0); | |
129 | assert_eq!(_1.swap_bytes(), _1); | |
130 | } | |
131 | ||
132 | #[test] | |
133 | fn test_le() { | |
9346a6ac AL |
134 | assert_eq!($T::from_le(A.to_le()), A); |
135 | assert_eq!($T::from_le(B.to_le()), B); | |
136 | assert_eq!($T::from_le(C.to_le()), C); | |
137 | assert_eq!($T::from_le(_0), _0); | |
138 | assert_eq!($T::from_le(_1), _1); | |
1a4d82fc JJ |
139 | assert_eq!(_0.to_le(), _0); |
140 | assert_eq!(_1.to_le(), _1); | |
141 | } | |
142 | ||
143 | #[test] | |
144 | fn test_be() { | |
9346a6ac AL |
145 | assert_eq!($T::from_be(A.to_be()), A); |
146 | assert_eq!($T::from_be(B.to_be()), B); | |
147 | assert_eq!($T::from_be(C.to_be()), C); | |
148 | assert_eq!($T::from_be(_0), _0); | |
149 | assert_eq!($T::from_be(_1), _1); | |
1a4d82fc JJ |
150 | assert_eq!(_0.to_be(), _0); |
151 | assert_eq!(_1.to_be(), _1); | |
152 | } | |
153 | ||
154 | #[test] | |
155 | fn test_signed_checked_div() { | |
9346a6ac AL |
156 | assert!((10 as $T).checked_div(2) == Some(5)); |
157 | assert!((5 as $T).checked_div(0) == None); | |
c34b1796 | 158 | assert!(isize::MIN.checked_div(-1) == None); |
1a4d82fc JJ |
159 | } |
160 | ||
161 | #[test] | |
162 | fn test_from_str() { | |
163 | fn from_str<T: ::std::str::FromStr>(t: &str) -> Option<T> { | |
85aaf69f | 164 | ::std::str::FromStr::from_str(t).ok() |
1a4d82fc JJ |
165 | } |
166 | assert_eq!(from_str::<$T>("0"), Some(0 as $T)); | |
167 | assert_eq!(from_str::<$T>("3"), Some(3 as $T)); | |
168 | assert_eq!(from_str::<$T>("10"), Some(10 as $T)); | |
169 | assert_eq!(from_str::<i32>("123456789"), Some(123456789 as i32)); | |
170 | assert_eq!(from_str::<$T>("00100"), Some(100 as $T)); | |
171 | ||
172 | assert_eq!(from_str::<$T>("-1"), Some(-1 as $T)); | |
173 | assert_eq!(from_str::<$T>("-3"), Some(-3 as $T)); | |
174 | assert_eq!(from_str::<$T>("-10"), Some(-10 as $T)); | |
175 | assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32)); | |
176 | assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T)); | |
177 | ||
178 | assert_eq!(from_str::<$T>(""), None); | |
179 | assert_eq!(from_str::<$T>(" "), None); | |
180 | assert_eq!(from_str::<$T>("x"), None); | |
181 | } | |
182 | ||
183 | #[test] | |
184 | fn test_from_str_radix() { | |
9346a6ac AL |
185 | assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T)); |
186 | assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T)); | |
187 | assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T)); | |
188 | assert_eq!(i32::from_str_radix("123", 16), Ok(291 as i32)); | |
189 | assert_eq!(i32::from_str_radix("ffff", 16), Ok(65535 as i32)); | |
190 | assert_eq!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32)); | |
191 | assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T)); | |
192 | assert_eq!($T::from_str_radix("Z", 36), Ok(35 as $T)); | |
193 | ||
194 | assert_eq!($T::from_str_radix("-123", 10), Ok(-123 as $T)); | |
195 | assert_eq!($T::from_str_radix("-1001", 2), Ok(-9 as $T)); | |
196 | assert_eq!($T::from_str_radix("-123", 8), Ok(-83 as $T)); | |
197 | assert_eq!(i32::from_str_radix("-123", 16), Ok(-291 as i32)); | |
198 | assert_eq!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32)); | |
199 | assert_eq!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32)); | |
200 | assert_eq!($T::from_str_radix("-z", 36), Ok(-35 as $T)); | |
201 | assert_eq!($T::from_str_radix("-Z", 36), Ok(-35 as $T)); | |
202 | ||
203 | assert_eq!($T::from_str_radix("Z", 35).ok(), None::<$T>); | |
204 | assert_eq!($T::from_str_radix("-9", 2).ok(), None::<$T>); | |
1a4d82fc | 205 | } |
c34b1796 AL |
206 | |
207 | #[test] | |
208 | fn test_pow() { | |
209 | let mut r = 2 as $T; | |
210 | ||
54a0048b SL |
211 | assert_eq!(r.pow(2), 4 as $T); |
212 | assert_eq!(r.pow(0), 1 as $T); | |
c34b1796 | 213 | r = -2 as $T; |
54a0048b SL |
214 | assert_eq!(r.pow(2), 4 as $T); |
215 | assert_eq!(r.pow(3), -8 as $T); | |
c34b1796 | 216 | } |
1a4d82fc JJ |
217 | } |
218 | ||
219 | )} |