]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/uint_macros.rs
rollup merge of #19577: aidancully/master
[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 /// Convert to a string as a byte slice in a given base.
21 ///
22 /// Use in place of x.to_string() when you do not need to store the string permanently
23 ///
24 /// # Examples
25 ///
26 /// ```
27 /// #![allow(deprecated)]
28 ///
29 /// std::uint::to_str_bytes(123, 10, |v| {
30 ///     assert!(v == "123".as_bytes());
31 /// });
32 /// ```
33 #[inline]
34 #[deprecated = "just use .to_string(), or a BufWriter with write! if you mustn't allocate"]
35 pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
36     use io::{Writer, Seek};
37     // The radix can be as low as 2, so we need at least 64 characters for a
38     // base 2 number, and then we need another for a possible '-' character.
39     let mut buf = [0u8, ..65];
40     let amt = {
41         let mut wr = ::io::BufWriter::new(&mut buf);
42         (write!(&mut wr, "{}", ::fmt::radix(n, radix as u8))).unwrap();
43         wr.tell().unwrap() as uint
44     };
45     f(buf[..amt])
46 }
47
48 #[cfg(test)]
49 mod tests {
50     use prelude::*;
51     use num::FromStrRadix;
52
53     #[test]
54     pub fn test_from_str() {
55         assert_eq!(from_str::<$T>("0"), Some(0u as $T));
56         assert_eq!(from_str::<$T>("3"), Some(3u as $T));
57         assert_eq!(from_str::<$T>("10"), Some(10u as $T));
58         assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
59         assert_eq!(from_str::<$T>("00100"), Some(100u as $T));
60
61         assert_eq!(from_str::<$T>(""), None);
62         assert_eq!(from_str::<$T>(" "), None);
63         assert_eq!(from_str::<$T>("x"), None);
64     }
65
66     #[test]
67     pub fn test_parse_bytes() {
68         assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123u as $T));
69         assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9u as $T));
70         assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83u as $T));
71         assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291u as u16));
72         assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535u as u16));
73         assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35u as $T));
74
75         assert_eq!(FromStrRadix::from_str_radix("Z", 10), None::<$T>);
76         assert_eq!(FromStrRadix::from_str_radix("_", 2), None::<$T>);
77     }
78
79     #[test]
80     fn test_uint_to_str_overflow() {
81         let mut u8_val: u8 = 255_u8;
82         assert_eq!(u8_val.to_string(), "255");
83
84         u8_val += 1 as u8;
85         assert_eq!(u8_val.to_string(), "0");
86
87         let mut u16_val: u16 = 65_535_u16;
88         assert_eq!(u16_val.to_string(), "65535");
89
90         u16_val += 1 as u16;
91         assert_eq!(u16_val.to_string(), "0");
92
93         let mut u32_val: u32 = 4_294_967_295_u32;
94         assert_eq!(u32_val.to_string(), "4294967295");
95
96         u32_val += 1 as u32;
97         assert_eq!(u32_val.to_string(), "0");
98
99         let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
100         assert_eq!(u64_val.to_string(), "18446744073709551615");
101
102         u64_val += 1 as u64;
103         assert_eq!(u64_val.to_string(), "0");
104     }
105
106     #[test]
107     fn test_uint_from_str_overflow() {
108         let mut u8_val: u8 = 255_u8;
109         assert_eq!(from_str::<u8>("255"), Some(u8_val));
110         assert_eq!(from_str::<u8>("256"), None);
111
112         u8_val += 1 as u8;
113         assert_eq!(from_str::<u8>("0"), Some(u8_val));
114         assert_eq!(from_str::<u8>("-1"), None);
115
116         let mut u16_val: u16 = 65_535_u16;
117         assert_eq!(from_str::<u16>("65535"), Some(u16_val));
118         assert_eq!(from_str::<u16>("65536"), None);
119
120         u16_val += 1 as u16;
121         assert_eq!(from_str::<u16>("0"), Some(u16_val));
122         assert_eq!(from_str::<u16>("-1"), None);
123
124         let mut u32_val: u32 = 4_294_967_295_u32;
125         assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
126         assert_eq!(from_str::<u32>("4294967296"), None);
127
128         u32_val += 1 as u32;
129         assert_eq!(from_str::<u32>("0"), Some(u32_val));
130         assert_eq!(from_str::<u32>("-1"), None);
131
132         let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
133         assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
134         assert_eq!(from_str::<u64>("18446744073709551616"), None);
135
136         u64_val += 1 as u64;
137         assert_eq!(from_str::<u64>("0"), Some(u64_val));
138         assert_eq!(from_str::<u64>("-1"), None);
139     }
140 }
141
142 ))