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 #![allow(unsigned_negation)]
16 macro_rules! uint_module { ($T:ty) => (
18 // String conversion functions and impl num -> str
23 use num::FromStrRadix;
25 fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> {
26 ::str::FromStr::from_str(t)
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));
37 assert_eq!(from_str::<$T>(""), None);
38 assert_eq!(from_str::<$T>(" "), None);
39 assert_eq!(from_str::<$T>("x"), None);
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));
51 assert_eq!(FromStrRadix::from_str_radix("Z", 10), None::<$T>);
52 assert_eq!(FromStrRadix::from_str_radix("_", 2), None::<$T>);
56 fn test_uint_to_str_overflow() {
57 let mut u8_val: u8 = 255_u8;
58 assert_eq!(u8_val.to_string(), "255");
61 assert_eq!(u8_val.to_string(), "0");
63 let mut u16_val: u16 = 65_535_u16;
64 assert_eq!(u16_val.to_string(), "65535");
67 assert_eq!(u16_val.to_string(), "0");
69 let mut u32_val: u32 = 4_294_967_295_u32;
70 assert_eq!(u32_val.to_string(), "4294967295");
73 assert_eq!(u32_val.to_string(), "0");
75 let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
76 assert_eq!(u64_val.to_string(), "18446744073709551615");
79 assert_eq!(u64_val.to_string(), "0");
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);
89 assert_eq!(from_str::<u8>("0"), Some(u8_val));
90 assert_eq!(from_str::<u8>("-1"), None);
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);
97 assert_eq!(from_str::<u16>("0"), Some(u16_val));
98 assert_eq!(from_str::<u16>("-1"), None);
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);
105 assert_eq!(from_str::<u32>("0"), Some(u32_val));
106 assert_eq!(from_str::<u32>("-1"), None);
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);
113 assert_eq!(from_str::<u64>("0"), Some(u64_val));
114 assert_eq!(from_str::<u64>("-1"), None);