summary refs log tree commit diff
path: root/src/libstd/num/uint_macros.rs
blob: 902c78c0a46f869e9f8306783fff25f53c0e7495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![doc(hidden)]

macro_rules! uint_module { ($T:ident) => (

#[cfg(test)]
mod tests {
    use prelude::v1::*;

    fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> {
        ::str::FromStr::from_str(t).ok()
    }

    #[test]
    pub fn test_from_str() {
        assert_eq!(from_str::<$T>("0"), Some(0 as $T));
        assert_eq!(from_str::<$T>("3"), Some(3 as $T));
        assert_eq!(from_str::<$T>("10"), Some(10 as $T));
        assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
        assert_eq!(from_str::<$T>("00100"), Some(100 as $T));

        assert_eq!(from_str::<$T>(""), None);
        assert_eq!(from_str::<$T>(" "), None);
        assert_eq!(from_str::<$T>("x"), None);
    }

    #[test]
    pub fn test_parse_bytes() {
        assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
        assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
        assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
        assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16));
        assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
        assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));

        assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
        assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
    }
}

) }