Debian Codegraph @

third_party/rust/ron/tests/numbers.rs

raw
1 use ron::{
2 de::from_str,
3 error::{Error, Position, SpannedError},
4 };
5
6 #[test]
7 fn test_hex() {
8 assert_eq!(from_str("0x507"), Ok(0x507));
9 assert_eq!(from_str("0x1A5"), Ok(0x1A5));
10 assert_eq!(from_str("0x53C537"), Ok(0x53C537));
11
12 assert_eq!(
13 from_str::<u8>("0x"),
14 Err(SpannedError {
15 code: Error::ExpectedInteger,
16 position: Position { line: 1, col: 3 },
17 })
18 );
19 assert_eq!(
20 from_str::<u8>("0x_1"),
21 Err(SpannedError {
22 code: Error::UnderscoreAtBeginning,
23 position: Position { line: 1, col: 3 },
24 })
25 );
26 assert_eq!(
27 from_str::<u8>("0xFFF"),
28 Err(SpannedError {
29 code: Error::IntegerOutOfBounds,
30 position: Position { line: 1, col: 6 },
31 })
32 );
33 }
34
35 #[test]
36 fn test_bin() {
37 assert_eq!(from_str("0b101"), Ok(0b101));
38 assert_eq!(from_str("0b001"), Ok(0b001));
39 assert_eq!(from_str("0b100100"), Ok(0b100100));
40
41 assert_eq!(
42 from_str::<u8>("0b"),
43 Err(SpannedError {
44 code: Error::ExpectedInteger,
45 position: Position { line: 1, col: 3 },
46 })
47 );
48 assert_eq!(
49 from_str::<u8>("0b_1"),
50 Err(SpannedError {
51 code: Error::UnderscoreAtBeginning,
52 position: Position { line: 1, col: 3 },
53 })
54 );
55 assert_eq!(
56 from_str::<u8>("0b111111111"),
57 Err(SpannedError {
58 code: Error::IntegerOutOfBounds,
59 position: Position { line: 1, col: 12 },
60 })
61 );
62 }
63
64 #[test]
65 fn test_oct() {
66 assert_eq!(from_str("0o1461"), Ok(0o1461));
67 assert_eq!(from_str("0o051"), Ok(0o051));
68 assert_eq!(from_str("0o150700"), Ok(0o150700));
69
70 assert_eq!(
71 from_str::<u8>("0o"),
72 Err(SpannedError {
73 code: Error::ExpectedInteger,
74 position: Position { line: 1, col: 3 },
75 })
76 );
77 assert_eq!(
78 from_str::<u8>("0o_1"),
79 Err(SpannedError {
80 code: Error::UnderscoreAtBeginning,
81 position: Position { line: 1, col: 3 },
82 })
83 );
84 assert_eq!(
85 from_str::<u8>("0o77777"),
86 Err(SpannedError {
87 code: Error::IntegerOutOfBounds,
88 position: Position { line: 1, col: 8 },
89 })
90 );
91 }
92
93 #[test]
94 fn test_dec() {
95 assert_eq!(from_str("1461"), Ok(1461));
96 assert_eq!(from_str("51"), Ok(51));
97 assert_eq!(from_str("150700"), Ok(150700));
98
99 assert_eq!(
100 from_str::<i8>("-_1"),
101 Err(SpannedError {
102 code: Error::UnderscoreAtBeginning,
103 position: Position { line: 1, col: 2 },
104 })
105 );
106 assert_eq!(
107 from_str::<u8>("256"),
108 Err(SpannedError {
109 code: Error::IntegerOutOfBounds,
110 position: Position { line: 1, col: 4 },
111 })
112 );
113 }