]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/int_macros.rs
20bb12db694c0f1dcfbe5c1229ab26771829ffb0
[rust.git] / src / libcore / num / int_macros.rs
1 // Copyright 2012-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 #![doc(hidden)]
13
14 macro_rules! int_module (($T:ty, $bits:expr) => (
15
16 // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
17 // calling the `mem::size_of` function.
18 pub static BITS : uint = $bits;
19 // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
20 // calling the `mem::size_of` function.
21 pub static BYTES : uint = ($bits / 8);
22
23 // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
24 // calling the `Bounded::min_value` function.
25 pub static MIN: $T = (-1 as $T) << (BITS - 1);
26 // FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
27 // FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
28 // calling the `Bounded::max_value` function.
29 pub static MAX: $T = !MIN;
30
31 #[cfg(test)]
32 mod tests {
33     use prelude::*;
34     use super::*;
35
36     use int;
37     use num;
38     use num::Bitwise;
39     use num::CheckedDiv;
40
41     #[test]
42     fn test_overflows() {
43         assert!(MAX > 0);
44         assert!(MIN <= 0);
45         assert!(MIN + MAX + 1 == 0);
46     }
47
48     #[test]
49     fn test_num() {
50         num::test_num(10 as $T, 2 as $T);
51     }
52
53     #[test]
54     pub fn test_abs() {
55         assert!((1 as $T).abs() == 1 as $T);
56         assert!((0 as $T).abs() == 0 as $T);
57         assert!((-1 as $T).abs() == 1 as $T);
58     }
59
60     #[test]
61     fn test_abs_sub() {
62         assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
63         assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
64         assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
65         assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
66     }
67
68     #[test]
69     fn test_signum() {
70         assert!((1 as $T).signum() == 1 as $T);
71         assert!((0 as $T).signum() == 0 as $T);
72         assert!((-0 as $T).signum() == 0 as $T);
73         assert!((-1 as $T).signum() == -1 as $T);
74     }
75
76     #[test]
77     fn test_is_positive() {
78         assert!((1 as $T).is_positive());
79         assert!(!(0 as $T).is_positive());
80         assert!(!(-0 as $T).is_positive());
81         assert!(!(-1 as $T).is_positive());
82     }
83
84     #[test]
85     fn test_is_negative() {
86         assert!(!(1 as $T).is_negative());
87         assert!(!(0 as $T).is_negative());
88         assert!(!(-0 as $T).is_negative());
89         assert!((-1 as $T).is_negative());
90     }
91
92     #[test]
93     fn test_bitwise() {
94         assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
95         assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
96         assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
97         assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
98         assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
99         assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
100     }
101
102     #[test]
103     fn test_count_ones() {
104         assert!((0b0101100 as $T).count_ones() == 3);
105         assert!((0b0100001 as $T).count_ones() == 2);
106         assert!((0b1111001 as $T).count_ones() == 5);
107     }
108
109     #[test]
110     fn test_count_zeros() {
111         assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
112         assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
113         assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
114     }
115
116     #[test]
117     fn test_swap_bytes() {
118         let n: $T = 0b0101100; assert_eq!(n.swap_bytes().swap_bytes(), n);
119         let n: $T = 0b0100001; assert_eq!(n.swap_bytes().swap_bytes(), n);
120         let n: $T = 0b1111001; assert_eq!(n.swap_bytes().swap_bytes(), n);
121
122         // Swapping these should make no difference
123         let n: $T = 0;   assert_eq!(n.swap_bytes(), n);
124         let n: $T = -1;  assert_eq!(n.swap_bytes(), n);
125     }
126
127     #[test]
128     fn test_rotate() {
129         let n: $T = 0b0101100; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
130         let n: $T = 0b0100001; assert_eq!(n.rotate_left(3).rotate_left(2).rotate_right(5),  n);
131         let n: $T = 0b1111001; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
132
133         // Rotating these should make no difference
134         //
135         // We test using 124 bits because to ensure that overlong bit shifts do
136         // not cause undefined behaviour. See #10183.
137         let n: $T = 0;   assert_eq!(n.rotate_left(124), n);
138         let n: $T = -1;  assert_eq!(n.rotate_left(124), n);
139         let n: $T = 0;   assert_eq!(n.rotate_right(124), n);
140         let n: $T = -1;  assert_eq!(n.rotate_right(124), n);
141     }
142
143     #[test]
144     fn test_signed_checked_div() {
145         assert!(10i.checked_div(&2) == Some(5));
146         assert!(5i.checked_div(&0) == None);
147         assert!(int::MIN.checked_div(&-1) == None);
148     }
149 }
150
151 ))