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
51
52
53
54
55
56
57
//! Module implementing parsing for `U256` type.

use crate::uint::U256;

impl_from_str! {
    impl FromStr for U256;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::from_str_radix;
    use core::num::IntErrorKind;

    #[test]
    fn from_str() {
        assert_eq!("42".parse::<U256>().unwrap(), 42);
    }

    #[test]
    fn from_str_prefixed() {
        assert_eq!(from_str_radix::<U256>("0b101", 2, Some("0b")).unwrap(), 5);
        assert_eq!(from_str_radix::<U256>("0xf", 16, Some("0x")).unwrap(), 15);
    }

    #[test]
    fn from_str_errors() {
        assert_eq!(
            from_str_radix::<U256>("", 2, None).unwrap_err().kind(),
            &IntErrorKind::Empty,
        );
        assert_eq!(
            from_str_radix::<U256>("?", 2, None).unwrap_err().kind(),
            &IntErrorKind::InvalidDigit,
        );
        assert_eq!(
            from_str_radix::<U256>("1", 16, Some("0x"))
                .unwrap_err()
                .kind(),
            &IntErrorKind::InvalidDigit,
        );
        assert_eq!(
            from_str_radix::<U256>("-1", 10, None).unwrap_err().kind(),
            &IntErrorKind::InvalidDigit,
        );
        assert_eq!(
            from_str_radix::<U256>(
                "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
                36,
                None
            )
            .unwrap_err()
            .kind(),
            &IntErrorKind::PosOverflow,
        );
    }
}