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