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.
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.
14 macro_rules! int_module (($T:ty, $bits:expr) => (
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);
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;
44 assert!(MIN + MAX + 1 == 0);
49 num::test_num(10 as $T, 2 as $T);
54 assert!((1 as $T).abs() == 1 as $T);
55 assert!((0 as $T).abs() == 0 as $T);
56 assert!((-1 as $T).abs() == 1 as $T);
61 assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
62 assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
63 assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
64 assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
69 assert!((1 as $T).signum() == 1 as $T);
70 assert!((0 as $T).signum() == 0 as $T);
71 assert!((-0 as $T).signum() == 0 as $T);
72 assert!((-1 as $T).signum() == -1 as $T);
76 fn test_is_positive() {
77 assert!((1 as $T).is_positive());
78 assert!(!(0 as $T).is_positive());
79 assert!(!(-0 as $T).is_positive());
80 assert!(!(-1 as $T).is_positive());
84 fn test_is_negative() {
85 assert!(!(1 as $T).is_negative());
86 assert!(!(0 as $T).is_negative());
87 assert!(!(-0 as $T).is_negative());
88 assert!((-1 as $T).is_negative());
92 fn test_bitwise_operators() {
93 assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
94 assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
95 assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
96 assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
97 assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
98 assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
101 static A: $T = 0b0101100;
102 static B: $T = 0b0100001;
103 static C: $T = 0b1111001;
109 fn test_count_ones() {
110 assert!(A.count_ones() == 3);
111 assert!(B.count_ones() == 2);
112 assert!(C.count_ones() == 5);
116 fn test_count_zeros() {
117 assert!(A.count_zeros() == BITS as $T - 3);
118 assert!(B.count_zeros() == BITS as $T - 2);
119 assert!(C.count_zeros() == BITS as $T - 5);
124 assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
125 assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
126 assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
128 // Rotating these should make no difference
130 // We test using 124 bits because to ensure that overlong bit shifts do
131 // not cause undefined behaviour. See #10183.
132 assert_eq!(_0.rotate_left(124), _0);
133 assert_eq!(_1.rotate_left(124), _1);
134 assert_eq!(_0.rotate_right(124), _0);
135 assert_eq!(_1.rotate_right(124), _1);
139 fn test_swap_bytes() {
140 assert_eq!(A.swap_bytes().swap_bytes(), A);
141 assert_eq!(B.swap_bytes().swap_bytes(), B);
142 assert_eq!(C.swap_bytes().swap_bytes(), C);
144 // Swapping these should make no difference
145 assert_eq!(_0.swap_bytes(), _0);
146 assert_eq!(_1.swap_bytes(), _1);
150 fn test_little_endian() {
151 assert_eq!(Int::from_little_endian(A.to_little_endian()), A);
152 assert_eq!(Int::from_little_endian(B.to_little_endian()), B);
153 assert_eq!(Int::from_little_endian(C.to_little_endian()), C);
154 assert_eq!(Int::from_little_endian(_0), _0);
155 assert_eq!(Int::from_little_endian(_1), _1);
156 assert_eq!(_0.to_little_endian(), _0);
157 assert_eq!(_1.to_little_endian(), _1);
161 fn test_big_endian() {
162 assert_eq!(Int::from_big_endian(A.to_big_endian()), A);
163 assert_eq!(Int::from_big_endian(B.to_big_endian()), B);
164 assert_eq!(Int::from_big_endian(C.to_big_endian()), C);
165 assert_eq!(Int::from_big_endian(_0), _0);
166 assert_eq!(Int::from_big_endian(_1), _1);
167 assert_eq!(_0.to_big_endian(), _0);
168 assert_eq!(_1.to_big_endian(), _1);
172 fn test_signed_checked_div() {
173 assert!(10i.checked_div(&2) == Some(5));
174 assert!(5i.checked_div(&0) == None);
175 assert!(int::MIN.checked_div(&-1) == None);