]> git.proxmox.com Git - rustc.git/blob - src/libstd/num/uint_macros.rs
8d4f0344beb62b767bc92927bceb3306ce63708f
[rustc.git] / src / libstd / num / uint_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 #![unstable(feature = "std_misc")]
12 #![doc(hidden)]
13 #![allow(unsigned_negation)]
14
15 macro_rules! uint_module { ($T:ty) => (
16
17 #[cfg(test)]
18 mod tests {
19 use prelude::v1::*;
20 use num::FromStrRadix;
21
22 fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> {
23 ::str::FromStr::from_str(t).ok()
24 }
25
26 #[test]
27 pub fn test_from_str() {
28 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
29 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
30 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
31 assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
32 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
33
34 assert_eq!(from_str::<$T>(""), None);
35 assert_eq!(from_str::<$T>(" "), None);
36 assert_eq!(from_str::<$T>("x"), None);
37 }
38
39 #[test]
40 pub fn test_parse_bytes() {
41 assert_eq!(FromStrRadix::from_str_radix("123", 10), Ok(123 as $T));
42 assert_eq!(FromStrRadix::from_str_radix("1001", 2), Ok(9 as $T));
43 assert_eq!(FromStrRadix::from_str_radix("123", 8), Ok(83 as $T));
44 assert_eq!(FromStrRadix::from_str_radix("123", 16), Ok(291 as u16));
45 assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Ok(65535 as u16));
46 assert_eq!(FromStrRadix::from_str_radix("z", 36), Ok(35 as $T));
47
48 assert_eq!(FromStrRadix::from_str_radix("Z", 10).ok(), None::<$T>);
49 assert_eq!(FromStrRadix::from_str_radix("_", 2).ok(), None::<$T>);
50 }
51
52 #[test]
53 fn test_uint_to_str_overflow() {
54 let mut u8_val: u8 = 255_u8;
55 assert_eq!(u8_val.to_string(), "255");
56
57 u8_val += 1 as u8;
58 assert_eq!(u8_val.to_string(), "0");
59
60 let mut u16_val: u16 = 65_535_u16;
61 assert_eq!(u16_val.to_string(), "65535");
62
63 u16_val += 1 as u16;
64 assert_eq!(u16_val.to_string(), "0");
65
66 let mut u32_val: u32 = 4_294_967_295_u32;
67 assert_eq!(u32_val.to_string(), "4294967295");
68
69 u32_val += 1 as u32;
70 assert_eq!(u32_val.to_string(), "0");
71
72 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
73 assert_eq!(u64_val.to_string(), "18446744073709551615");
74
75 u64_val += 1 as u64;
76 assert_eq!(u64_val.to_string(), "0");
77 }
78
79 #[test]
80 fn test_uint_from_str_overflow() {
81 let mut u8_val: u8 = 255_u8;
82 assert_eq!(from_str::<u8>("255"), Some(u8_val));
83 assert_eq!(from_str::<u8>("256"), None);
84
85 u8_val += 1 as u8;
86 assert_eq!(from_str::<u8>("0"), Some(u8_val));
87 assert_eq!(from_str::<u8>("-1"), None);
88
89 let mut u16_val: u16 = 65_535_u16;
90 assert_eq!(from_str::<u16>("65535"), Some(u16_val));
91 assert_eq!(from_str::<u16>("65536"), None);
92
93 u16_val += 1 as u16;
94 assert_eq!(from_str::<u16>("0"), Some(u16_val));
95 assert_eq!(from_str::<u16>("-1"), None);
96
97 let mut u32_val: u32 = 4_294_967_295_u32;
98 assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
99 assert_eq!(from_str::<u32>("4294967296"), None);
100
101 u32_val += 1 as u32;
102 assert_eq!(from_str::<u32>("0"), Some(u32_val));
103 assert_eq!(from_str::<u32>("-1"), None);
104
105 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
106 assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
107 assert_eq!(from_str::<u64>("18446744073709551616"), None);
108
109 u64_val += 1 as u64;
110 assert_eq!(from_str::<u64>("0"), Some(u64_val));
111 assert_eq!(from_str::<u64>("-1"), None);
112 }
113 }
114
115 ) }