1 | import strconv |
2 | |
3 | fn test_atoi() { |
4 | assert strconv.atoi('16')! == 16 |
5 | assert strconv.atoi('+16')! == 16 |
6 | assert strconv.atoi('-16')! == -16 |
7 | |
8 | // invalid strings |
9 | if x := strconv.atoi('str') { |
10 | println(x) |
11 | assert false |
12 | } else { |
13 | assert true |
14 | } |
15 | if x := strconv.atoi('string_longer_than_10_chars') { |
16 | println(x) |
17 | assert false |
18 | } else { |
19 | assert true |
20 | } |
21 | if x := strconv.atoi('') { |
22 | println(x) |
23 | assert false |
24 | } else { |
25 | assert true |
26 | } |
27 | } |
28 | |
29 | fn test_parse_int() { |
30 | // symbols coverage |
31 | assert strconv.parse_int('1234567890', 10, 32)! == 1234567890 |
32 | assert strconv.parse_int('19aAbBcCdDeEfF', 16, 64)! == 0x19aAbBcCdDeEfF |
33 | // Different bases |
34 | assert strconv.parse_int('16', 16, 0)! == 0x16 |
35 | assert strconv.parse_int('16', 8, 0)! == 0o16 |
36 | assert strconv.parse_int('11', 2, 0)! == 3 |
37 | // Different bit sizes |
38 | assert strconv.parse_int('127', 10, 8)! == 127 |
39 | assert strconv.parse_int('128', 10, 8)! == 127 |
40 | assert strconv.parse_int('32767', 10, 16)! == 32767 |
41 | assert strconv.parse_int('32768', 10, 16)! == 32767 |
42 | assert strconv.parse_int('2147483647', 10, 32)! == 2147483647 |
43 | assert strconv.parse_int('2147483648', 10, 32)! == 2147483647 |
44 | assert strconv.parse_int('9223372036854775807', 10, 64)! == 9223372036854775807 |
45 | assert strconv.parse_int('9223372036854775808', 10, 64)! == 9223372036854775807 |
46 | assert strconv.parse_int('baobab', 36, 64)? == 683058467 |
47 | // Invalid bit sizes |
48 | if x := strconv.parse_int('123', 10, -1) { |
49 | println(x) |
50 | assert false |
51 | } else { |
52 | assert true |
53 | } |
54 | if x := strconv.parse_int('123', 10, 65) { |
55 | println(x) |
56 | assert false |
57 | } else { |
58 | assert true |
59 | } |
60 | } |
61 | |
62 | fn test_common_parse_uint2() { |
63 | mut result, mut error := strconv.common_parse_uint2('1', 10, 8) |
64 | assert result == 1 |
65 | assert error == 0 |
66 | result, error = strconv.common_parse_uint2('123', 10, 8) |
67 | assert result == 123 |
68 | assert error == 0 |
69 | result, error = strconv.common_parse_uint2('123', 10, 65) |
70 | assert result == 0 |
71 | assert error == -2 |
72 | result, error = strconv.common_parse_uint2('123', 10, -1) |
73 | assert result == 0 |
74 | assert error == -2 |
75 | result, error = strconv.common_parse_uint2('', 10, 8) |
76 | assert result == 0 |
77 | assert error == 1 |
78 | result, error = strconv.common_parse_uint2('1a', 10, 8) |
79 | assert result == 1 |
80 | assert error == 2 |
81 | result, error = strconv.common_parse_uint2('12a', 10, 8) |
82 | assert result == 12 |
83 | assert error == 3 |
84 | result, error = strconv.common_parse_uint2('123a', 10, 8) |
85 | assert result == 123 |
86 | assert error == 4 |
87 | } |
88 | |
89 | fn test_common_parse_uint2_fail() { |
90 | mut ascii_characters := [' ', '!', '"', '#', '\$', '%', '&', "'", '(', ')', '*', '+', ',', |
91 | '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', |
92 | '}', '~'] |
93 | mut special_characters := [':', ';', '<', '=', '>', '?', '@', 'X', 'Y', 'Z', '[', '\\', ']', |
94 | '^', '_', '`'] |
95 | |
96 | num0, err0 := strconv.common_parse_uint2('1Ab', 16, 32) |
97 | assert num0 == 427 |
98 | assert err0 == 0 |
99 | |
100 | for ch in ascii_characters { |
101 | // println("ch: [${ch}]") |
102 | txt_str := '${ch[0]:c}12Ab' |
103 | num, err := strconv.common_parse_uint2(txt_str, 16, 32) |
104 | assert err != 0 |
105 | } |
106 | |
107 | for ch in special_characters { |
108 | // println("ch: [${ch}]") |
109 | txt_str := '${ch[0]:c}12Ab' |
110 | num, err := strconv.common_parse_uint2(txt_str, 16, 32) |
111 | assert err != 0 |
112 | } |
113 | } |
114 | |
115 | fn test_common_parse_uint2_compatibility() { |
116 | test_list := [ |
117 | '1234,1234', |
118 | '1_234,1234', |
119 | '1_2_34,1234', |
120 | '_12__34,1', |
121 | '12__34,1', |
122 | '_1234,1', |
123 | '1234_,1', |
124 | '0x1234,4660', |
125 | '0x_1234,4660', |
126 | '0x1_234,4660', |
127 | '0x1_2_3_4,4660', |
128 | '0_x1234,1', |
129 | '0x1234_,1', |
130 | '0o1234,668', |
131 | '0o_1234,668', |
132 | '0o1_234,668', |
133 | '0o1_2_3_4,668', |
134 | '0_o1234,1', |
135 | '0o1234_,1', |
136 | '0b111,7', |
137 | '0b_111,7', |
138 | '0b1_11,7', |
139 | '0b1_1_1,7', |
140 | '0_b111,1', |
141 | '0b111_,1', |
142 | '0xa,10', |
143 | '0xA,10', |
144 | '0xf,15', |
145 | '0xf,15', |
146 | '0_xf,1', |
147 | '0x_0_0_f_,1', |
148 | '0x_0_0__f,1', |
149 | '0x_0_0_f,15', |
150 | ] |
151 | |
152 | for tst in test_list { |
153 | query := tst.split(',') |
154 | mut a0 := strconv.common_parse_uint(query[0], 0, 32, true, true) or { 1 } |
155 | // println("${a0} => ${query[1]}") |
156 | assert a0.str() == query[1] |
157 | } |
158 | } |