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.
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.
13 macro_rules! int_module (($T:ty, $T_i:ident) => (
18 use core::num::{FromStrRadix, Int, SignedInt};
19 use core::str::from_str;
20 use core::ops::{Shl, Shr, Not, BitXor, BitAnd, BitOr};
27 assert!(MIN + MAX + 1 == 0);
32 num::test_num(10 as $T, 2 as $T);
37 assert!((1 as $T).abs() == 1 as $T);
38 assert!((0 as $T).abs() == 0 as $T);
39 assert!((-1 as $T).abs() == 1 as $T);
44 assert!((1 as $T).signum() == 1 as $T);
45 assert!((0 as $T).signum() == 0 as $T);
46 assert!((-0 as $T).signum() == 0 as $T);
47 assert!((-1 as $T).signum() == -1 as $T);
51 fn test_is_positive() {
52 assert!((1 as $T).is_positive());
53 assert!(!(0 as $T).is_positive());
54 assert!(!(-0 as $T).is_positive());
55 assert!(!(-1 as $T).is_positive());
59 fn test_is_negative() {
60 assert!(!(1 as $T).is_negative());
61 assert!(!(0 as $T).is_negative());
62 assert!(!(-0 as $T).is_negative());
63 assert!((-1 as $T).is_negative());
67 fn test_bitwise_operators() {
68 assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T));
69 assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T));
70 assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T));
71 assert!(0b1110 as $T == (0b0111 as $T).shl(1));
72 assert!(0b0111 as $T == (0b1110 as $T).shr(1));
73 assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
76 static A: $T = 0b0101100;
77 static B: $T = 0b0100001;
78 static C: $T = 0b1111001;
84 fn test_count_ones() {
85 assert!(A.count_ones() == 3);
86 assert!(B.count_ones() == 2);
87 assert!(C.count_ones() == 5);
91 fn test_count_zeros() {
92 assert!(A.count_zeros() == BITS - 3);
93 assert!(B.count_zeros() == BITS - 2);
94 assert!(C.count_zeros() == BITS - 5);
99 assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
100 assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
101 assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
103 // Rotating these should make no difference
105 // We test using 124 bits because to ensure that overlong bit shifts do
106 // not cause undefined behaviour. See #10183.
107 assert_eq!(_0.rotate_left(124), _0);
108 assert_eq!(_1.rotate_left(124), _1);
109 assert_eq!(_0.rotate_right(124), _0);
110 assert_eq!(_1.rotate_right(124), _1);
112 // Rotating by 0 should have no effect
113 assert_eq!(A.rotate_left(0), A);
114 assert_eq!(B.rotate_left(0), B);
115 assert_eq!(C.rotate_left(0), C);
116 // Rotating by a multiple of word size should also have no effect
117 assert_eq!(A.rotate_left(64), A);
118 assert_eq!(B.rotate_left(64), B);
119 assert_eq!(C.rotate_left(64), C);
123 fn test_swap_bytes() {
124 assert_eq!(A.swap_bytes().swap_bytes(), A);
125 assert_eq!(B.swap_bytes().swap_bytes(), B);
126 assert_eq!(C.swap_bytes().swap_bytes(), C);
128 // Swapping these should make no difference
129 assert_eq!(_0.swap_bytes(), _0);
130 assert_eq!(_1.swap_bytes(), _1);
135 assert_eq!(Int::from_le(A.to_le()), A);
136 assert_eq!(Int::from_le(B.to_le()), B);
137 assert_eq!(Int::from_le(C.to_le()), C);
138 assert_eq!(Int::from_le(_0), _0);
139 assert_eq!(Int::from_le(_1), _1);
140 assert_eq!(_0.to_le(), _0);
141 assert_eq!(_1.to_le(), _1);
146 assert_eq!(Int::from_be(A.to_be()), A);
147 assert_eq!(Int::from_be(B.to_be()), B);
148 assert_eq!(Int::from_be(C.to_be()), C);
149 assert_eq!(Int::from_be(_0), _0);
150 assert_eq!(Int::from_be(_1), _1);
151 assert_eq!(_0.to_be(), _0);
152 assert_eq!(_1.to_be(), _1);
156 fn test_signed_checked_div() {
157 assert!(10i.checked_div(2) == Some(5));
158 assert!(5i.checked_div(0) == None);
159 assert!(int::MIN.checked_div(-1) == None);
164 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
165 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
166 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
167 assert_eq!(from_str::<i32>("123456789"), Some(123456789 as i32));
168 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
170 assert_eq!(from_str::<$T>("-1"), Some(-1 as $T));
171 assert_eq!(from_str::<$T>("-3"), Some(-3 as $T));
172 assert_eq!(from_str::<$T>("-10"), Some(-10 as $T));
173 assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32));
174 assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T));
176 assert_eq!(from_str::<$T>(""), None);
177 assert_eq!(from_str::<$T>(" "), None);
178 assert_eq!(from_str::<$T>("x"), None);
182 fn test_from_str_radix() {
183 assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T));
184 assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T));
185 assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T));
186 assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32));
187 assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32));
188 assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32));
189 assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T));
190 assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T));
192 assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T));
193 assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T));
194 assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T));
195 assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32));
196 assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32));
197 assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32));
198 assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T));
199 assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T));
201 assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>);
202 assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>);