]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/int_macros.rs
940b036ca907be036b4a7003ba70186d79c66738
[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 num;
19     use core::num::CheckedDiv;
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_abs_sub() {
42         assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
43         assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
44         assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
45         assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
46     }
47
48     #[test]
49     fn test_signum() {
50         assert!((1 as $T).signum() == 1 as $T);
51         assert!((0 as $T).signum() == 0 as $T);
52         assert!((-0 as $T).signum() == 0 as $T);
53         assert!((-1 as $T).signum() == -1 as $T);
54     }
55
56     #[test]
57     fn test_is_positive() {
58         assert!((1 as $T).is_positive());
59         assert!(!(0 as $T).is_positive());
60         assert!(!(-0 as $T).is_positive());
61         assert!(!(-1 as $T).is_positive());
62     }
63
64     #[test]
65     fn test_is_negative() {
66         assert!(!(1 as $T).is_negative());
67         assert!(!(0 as $T).is_negative());
68         assert!(!(-0 as $T).is_negative());
69         assert!((-1 as $T).is_negative());
70     }
71
72     #[test]
73     fn test_bitwise_operators() {
74         assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
75         assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
76         assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
77         assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
78         assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
79         assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
80     }
81
82     static A: $T = 0b0101100;
83     static B: $T = 0b0100001;
84     static C: $T = 0b1111001;
85
86     static _0: $T = 0;
87     static _1: $T = !0;
88
89     #[test]
90     fn test_count_ones() {
91         assert!(A.count_ones() == 3);
92         assert!(B.count_ones() == 2);
93         assert!(C.count_ones() == 5);
94     }
95
96     #[test]
97     fn test_count_zeros() {
98         assert!(A.count_zeros() == BITS as $T - 3);
99         assert!(B.count_zeros() == BITS as $T - 2);
100         assert!(C.count_zeros() == BITS as $T - 5);
101     }
102
103     #[test]
104     fn test_rotate() {
105         assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
106         assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
107         assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
108
109         // Rotating these should make no difference
110         //
111         // We test using 124 bits because to ensure that overlong bit shifts do
112         // not cause undefined behaviour. See #10183.
113         assert_eq!(_0.rotate_left(124), _0);
114         assert_eq!(_1.rotate_left(124), _1);
115         assert_eq!(_0.rotate_right(124), _0);
116         assert_eq!(_1.rotate_right(124), _1);
117     }
118
119     #[test]
120     fn test_swap_bytes() {
121         assert_eq!(A.swap_bytes().swap_bytes(), A);
122         assert_eq!(B.swap_bytes().swap_bytes(), B);
123         assert_eq!(C.swap_bytes().swap_bytes(), C);
124
125         // Swapping these should make no difference
126         assert_eq!(_0.swap_bytes(), _0);
127         assert_eq!(_1.swap_bytes(), _1);
128     }
129
130     #[test]
131     fn test_le() {
132         assert_eq!(Int::from_le(A.to_le()), A);
133         assert_eq!(Int::from_le(B.to_le()), B);
134         assert_eq!(Int::from_le(C.to_le()), C);
135         assert_eq!(Int::from_le(_0), _0);
136         assert_eq!(Int::from_le(_1), _1);
137         assert_eq!(_0.to_le(), _0);
138         assert_eq!(_1.to_le(), _1);
139     }
140
141     #[test]
142     fn test_be() {
143         assert_eq!(Int::from_be(A.to_be()), A);
144         assert_eq!(Int::from_be(B.to_be()), B);
145         assert_eq!(Int::from_be(C.to_be()), C);
146         assert_eq!(Int::from_be(_0), _0);
147         assert_eq!(Int::from_be(_1), _1);
148         assert_eq!(_0.to_be(), _0);
149         assert_eq!(_1.to_be(), _1);
150     }
151
152     #[test]
153     fn test_signed_checked_div() {
154         assert!(10i.checked_div(&2) == Some(5));
155         assert!(5i.checked_div(&0) == None);
156         assert!(int::MIN.checked_div(&-1) == None);
157     }
158 }
159
160 ))