]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/int_macros.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libcoretest / num / int_macros.rs
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
11 #![macro_escape]
12
13 macro_rules! int_module (($T:ty, $T_i:ident) => (
14 #[cfg(test)]
15 mod tests {
16     use core::$T_i::*;
17     use core::int;
18     use core::num::{FromStrRadix, Int, SignedInt};
19     use core::ops::{Shl, Shr, Not, BitXor, BitAnd, BitOr};
20     use num;
21
22     #[test]
23     fn test_overflows() {
24         assert!(MAX > 0);
25         assert!(MIN <= 0);
26         assert!(MIN + MAX + 1 == 0);
27     }
28
29     #[test]
30     fn test_num() {
31         num::test_num(10 as $T, 2 as $T);
32     }
33
34     #[test]
35     pub fn test_abs() {
36         assert!((1 as $T).abs() == 1 as $T);
37         assert!((0 as $T).abs() == 0 as $T);
38         assert!((-1 as $T).abs() == 1 as $T);
39     }
40
41     #[test]
42     fn test_signum() {
43         assert!((1 as $T).signum() == 1 as $T);
44         assert!((0 as $T).signum() == 0 as $T);
45         assert!((-0 as $T).signum() == 0 as $T);
46         assert!((-1 as $T).signum() == -1 as $T);
47     }
48
49     #[test]
50     fn test_is_positive() {
51         assert!((1 as $T).is_positive());
52         assert!(!(0 as $T).is_positive());
53         assert!(!(-0 as $T).is_positive());
54         assert!(!(-1 as $T).is_positive());
55     }
56
57     #[test]
58     fn test_is_negative() {
59         assert!(!(1 as $T).is_negative());
60         assert!(!(0 as $T).is_negative());
61         assert!(!(-0 as $T).is_negative());
62         assert!((-1 as $T).is_negative());
63     }
64
65     #[test]
66     fn test_bitwise_operators() {
67         assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T));
68         assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T));
69         assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T));
70         assert!(0b1110 as $T == (0b0111 as $T).shl(1));
71         assert!(0b0111 as $T == (0b1110 as $T).shr(1));
72         assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
73     }
74
75     static A: $T = 0b0101100;
76     static B: $T = 0b0100001;
77     static C: $T = 0b1111001;
78
79     static _0: $T = 0;
80     static _1: $T = !0;
81
82     #[test]
83     fn test_count_ones() {
84         assert!(A.count_ones() == 3);
85         assert!(B.count_ones() == 2);
86         assert!(C.count_ones() == 5);
87     }
88
89     #[test]
90     fn test_count_zeros() {
91         assert!(A.count_zeros() == BITS - 3);
92         assert!(B.count_zeros() == BITS - 2);
93         assert!(C.count_zeros() == BITS - 5);
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() {
134         assert_eq!(Int::from_le(A.to_le()), A);
135         assert_eq!(Int::from_le(B.to_le()), B);
136         assert_eq!(Int::from_le(C.to_le()), C);
137         assert_eq!(Int::from_le(_0), _0);
138         assert_eq!(Int::from_le(_1), _1);
139         assert_eq!(_0.to_le(), _0);
140         assert_eq!(_1.to_le(), _1);
141     }
142
143     #[test]
144     fn test_be() {
145         assert_eq!(Int::from_be(A.to_be()), A);
146         assert_eq!(Int::from_be(B.to_be()), B);
147         assert_eq!(Int::from_be(C.to_be()), C);
148         assert_eq!(Int::from_be(_0), _0);
149         assert_eq!(Int::from_be(_1), _1);
150         assert_eq!(_0.to_be(), _0);
151         assert_eq!(_1.to_be(), _1);
152     }
153
154     #[test]
155     fn test_signed_checked_div() {
156         assert!(10i.checked_div(2) == Some(5));
157         assert!(5i.checked_div(0) == None);
158         assert!(int::MIN.checked_div(-1) == None);
159     }
160
161     #[test]
162     fn test_from_str() {
163         fn from_str<T: ::std::str::FromStr>(t: &str) -> Option<T> {
164             ::std::str::FromStr::from_str(t)
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() {
185         assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T));
186         assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T));
187         assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T));
188         assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32));
189         assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32));
190         assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32));
191         assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T));
192         assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T));
193
194         assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T));
195         assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T));
196         assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T));
197         assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32));
198         assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32));
199         assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32));
200         assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T));
201         assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T));
202
203         assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>);
204         assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>);
205     }
206 }
207
208 ));