]>
git.proxmox.com Git - rustc.git/blob - src/libstd/num/uint_macros.rs
555a5cc3e20e9d747006a0963c6bd99944cf5679
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.
12 #![allow(unsigned_negation)]
14 macro_rules
! uint_module
{ ($T
:ident
) => (
20 fn from_str
<T
: ::str::FromStr
>(t
: &str) -> Option
<T
> {
21 ::str::FromStr
::from_str(t
).ok()
25 pub fn test_from_str() {
26 assert_eq
!(from_str
::<$T
>("0"), Some(0 as $T
));
27 assert_eq
!(from_str
::<$T
>("3"), Some(3 as $T
));
28 assert_eq
!(from_str
::<$T
>("10"), Some(10 as $T
));
29 assert_eq
!(from_str
::<u32>("123456789"), Some(123456789 as u32));
30 assert_eq
!(from_str
::<$T
>("00100"), Some(100 as $T
));
32 assert_eq
!(from_str
::<$T
>(""), None
);
33 assert_eq
!(from_str
::<$T
>(" "), None
);
34 assert_eq
!(from_str
::<$T
>("x"), None
);
38 pub fn test_parse_bytes() {
39 assert_eq
!($T
::from_str_radix("123", 10), Ok(123 as $T
));
40 assert_eq
!($T
::from_str_radix("1001", 2), Ok(9 as $T
));
41 assert_eq
!($T
::from_str_radix("123", 8), Ok(83 as $T
));
42 assert_eq
!(u16::from_str_radix("123", 16), Ok(291 as u16));
43 assert_eq
!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
44 assert_eq
!($T
::from_str_radix("z", 36), Ok(35 as $T
));
46 assert_eq
!($T
::from_str_radix("Z", 10).ok(), None
::<$T
>);
47 assert_eq
!($T
::from_str_radix("_", 2).ok(), None
::<$T
>);