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! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
16 pub static BITS : uint = $bits;
17 pub static BYTES : uint = ($bits / 8);
19 pub static MIN: $T = 0 as $T;
20 pub static MAX: $T = 0 as $T - 1 as $T;
22 impl CheckedDiv for $T {
24 fn checked_div(&self, v: &$T) -> Option<$T> {
38 fn lt(&self, other: &$T) -> bool { (*self) < (*other) }
44 fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
49 fn default() -> $T { 0 }
57 fn is_zero(&self) -> bool { *self == 0 }
66 impl Add<$T,$T> for $T {
68 fn add(&self, other: &$T) -> $T { *self + *other }
72 impl Sub<$T,$T> for $T {
74 fn sub(&self, other: &$T) -> $T { *self - *other }
78 impl Mul<$T,$T> for $T {
80 fn mul(&self, other: &$T) -> $T { *self * *other }
84 impl Div<$T,$T> for $T {
86 fn div(&self, other: &$T) -> $T { *self / *other }
90 impl Rem<$T,$T> for $T {
92 fn rem(&self, other: &$T) -> $T { *self % *other }
98 fn neg(&self) -> $T { -*self }
101 impl Unsigned for $T {}
104 impl BitOr<$T,$T> for $T {
106 fn bitor(&self, other: &$T) -> $T { *self | *other }
110 impl BitAnd<$T,$T> for $T {
112 fn bitand(&self, other: &$T) -> $T { *self & *other }
116 impl BitXor<$T,$T> for $T {
118 fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
122 impl Shl<$T,$T> for $T {
124 fn shl(&self, other: &$T) -> $T { *self << *other }
128 impl Shr<$T,$T> for $T {
130 fn shr(&self, other: &$T) -> $T { *self >> *other }
134 impl Not<$T> for $T {
136 fn not(&self) -> $T { !*self }
139 impl Bounded for $T {
141 fn min_value() -> $T { MIN }
144 fn max_value() -> $T { MAX }
149 // String conversion functions and impl str -> num
151 /// Parse a byte slice as a number in the given base.
153 pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
154 strconv::from_str_bytes_common(buf, radix, false, false, false,
155 strconv::ExpNone, false, false)
158 impl FromStr for $T {
160 fn from_str(s: &str) -> Option<$T> {
161 strconv::from_str_common(s, 10u, false, false, false,
162 strconv::ExpNone, false, false)
166 impl FromStrRadix for $T {
168 fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
169 strconv::from_str_common(s, radix, false, false, false,
170 strconv::ExpNone, false, false)
174 // String conversion functions and impl num -> str
176 /// Convert to a string as a byte slice in a given base.
178 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
179 // The radix can be as low as 2, so we need at least 64 characters for a
181 let mut buf = [0u8, ..64];
183 strconv::int_to_str_bytes_common(n, radix, strconv::SignNone, |i| {
190 impl ToStrRadix for $T {
191 /// Convert to a string in a given base.
193 fn to_str_radix(&self, radix: uint) -> ~str {
195 strconv::int_to_str_bytes_common(*self, radix, strconv::SignNone, |i| {
198 // We know we generated valid utf-8, so we don't need to go through that
200 unsafe { str::raw::from_utf8_owned(buf) }
204 impl Primitive for $T {}
206 impl Bitwise for $T {
207 /// Returns the number of ones in the binary representation of the number.
209 fn count_ones(&self) -> $T {
210 (*self as $T_SIGNED).count_ones() as $T
213 /// Returns the number of leading zeros in the in the binary representation
216 fn leading_zeros(&self) -> $T {
217 (*self as $T_SIGNED).leading_zeros() as $T
220 /// Returns the number of trailing zeros in the in the binary representation
223 fn trailing_zeros(&self) -> $T {
224 (*self as $T_SIGNED).trailing_zeros() as $T
240 fn test_overflows() {
243 assert_eq!(MIN + MAX + 1, 0);
248 num::test_num(10 as $T, 2 as $T);
253 assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
254 assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
255 assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
256 assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
257 assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
258 assert_eq!(MAX - (0b1011 as $T), (0b1011 as $T).not());
262 fn test_count_ones() {
263 assert_eq!((0b0101100 as $T).count_ones(), 3);
264 assert_eq!((0b0100001 as $T).count_ones(), 2);
265 assert_eq!((0b1111001 as $T).count_ones(), 5);
269 fn test_count_zeros() {
270 assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3);
271 assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2);
272 assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
276 pub fn test_to_str() {
277 assert_eq!((0 as $T).to_str_radix(10u), ~"0");
278 assert_eq!((1 as $T).to_str_radix(10u), ~"1");
279 assert_eq!((2 as $T).to_str_radix(10u), ~"2");
280 assert_eq!((11 as $T).to_str_radix(10u), ~"11");
281 assert_eq!((11 as $T).to_str_radix(16u), ~"b");
282 assert_eq!((255 as $T).to_str_radix(16u), ~"ff");
283 assert_eq!((0xff as $T).to_str_radix(10u), ~"255");
287 pub fn test_from_str() {
288 assert_eq!(from_str::<$T>("0"), Some(0u as $T));
289 assert_eq!(from_str::<$T>("3"), Some(3u as $T));
290 assert_eq!(from_str::<$T>("10"), Some(10u as $T));
291 assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
292 assert_eq!(from_str::<$T>("00100"), Some(100u as $T));
294 assert!(from_str::<$T>("").is_none());
295 assert!(from_str::<$T>(" ").is_none());
296 assert!(from_str::<$T>("x").is_none());
300 pub fn test_parse_bytes() {
302 assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123u as $T));
303 assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9u as $T));
304 assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83u as $T));
305 assert_eq!(u16::parse_bytes("123".as_bytes(), 16u), Some(291u as u16));
306 assert_eq!(u16::parse_bytes("ffff".as_bytes(), 16u), Some(65535u as u16));
307 assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35u as $T));
309 assert!(parse_bytes("Z".as_bytes(), 10u).is_none());
310 assert!(parse_bytes("_".as_bytes(), 2u).is_none());
314 fn test_uint_to_str_overflow() {
315 let mut u8_val: u8 = 255_u8;
316 assert_eq!(u8_val.to_str(), ~"255");
319 assert_eq!(u8_val.to_str(), ~"0");
321 let mut u16_val: u16 = 65_535_u16;
322 assert_eq!(u16_val.to_str(), ~"65535");
325 assert_eq!(u16_val.to_str(), ~"0");
327 let mut u32_val: u32 = 4_294_967_295_u32;
328 assert_eq!(u32_val.to_str(), ~"4294967295");
331 assert_eq!(u32_val.to_str(), ~"0");
333 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
334 assert_eq!(u64_val.to_str(), ~"18446744073709551615");
337 assert_eq!(u64_val.to_str(), ~"0");
341 fn test_uint_from_str_overflow() {
342 let mut u8_val: u8 = 255_u8;
343 assert_eq!(from_str::<u8>("255"), Some(u8_val));
344 assert!(from_str::<u8>("256").is_none());
347 assert_eq!(from_str::<u8>("0"), Some(u8_val));
348 assert!(from_str::<u8>("-1").is_none());
350 let mut u16_val: u16 = 65_535_u16;
351 assert_eq!(from_str::<u16>("65535"), Some(u16_val));
352 assert!(from_str::<u16>("65536").is_none());
355 assert_eq!(from_str::<u16>("0"), Some(u16_val));
356 assert!(from_str::<u16>("-1").is_none());
358 let mut u32_val: u32 = 4_294_967_295_u32;
359 assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
360 assert!(from_str::<u32>("4294967296").is_none());
363 assert_eq!(from_str::<u32>("0"), Some(u32_val));
364 assert!(from_str::<u32>("-1").is_none());
366 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
367 assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
368 assert!(from_str::<u64>("18446744073709551616").is_none());
371 assert_eq!(from_str::<u64>("0"), Some(u64_val));
372 assert!(from_str::<u64>("-1").is_none());
377 pub fn to_str_radix1() {
378 100u.to_str_radix(1u);
383 pub fn to_str_radix37() {
384 100u.to_str_radix(37u);
388 fn test_unsigned_checked_div() {
389 assert_eq!(10u.checked_div(&2), Some(5));
390 assert_eq!(5u.checked_div(&0), None);