1 | //===-- Unittests for str_to_integer --------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/__support/str_to_integer.h" |
10 | #include "src/errno/libc_errno.h" |
11 | #include <stddef.h> |
12 | |
13 | #include "test/UnitTest/Test.h" |
14 | |
15 | // This file is for testing the src_len argument and other internal interface |
16 | // features. Primary testing is done in stdlib/StrolTest.cpp through the public |
17 | // interface. |
18 | |
19 | TEST(LlvmLibcStrToIntegerTest, SimpleLength) { |
20 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "12345" , base: 10, src_len: 10); |
21 | EXPECT_FALSE(result.has_error()); |
22 | EXPECT_EQ(result.parsed_len, ptrdiff_t(5)); |
23 | ASSERT_EQ(result.value, 12345); |
24 | |
25 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "12345" , base: 10, src_len: 2); |
26 | EXPECT_FALSE(result.has_error()); |
27 | EXPECT_EQ(result.parsed_len, ptrdiff_t(2)); |
28 | ASSERT_EQ(result.value, 12); |
29 | |
30 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "12345" , base: 10, src_len: 0); |
31 | EXPECT_FALSE(result.has_error()); |
32 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
33 | ASSERT_EQ(result.value, 0); |
34 | } |
35 | |
36 | TEST(LlvmLibcStrToIntegerTest, LeadingSpaces) { |
37 | auto result = |
38 | LIBC_NAMESPACE::internal::strtointeger<int>(src: " 12345" , base: 10, src_len: 15); |
39 | EXPECT_FALSE(result.has_error()); |
40 | EXPECT_EQ(result.parsed_len, ptrdiff_t(10)); |
41 | ASSERT_EQ(result.value, 12345); |
42 | |
43 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " 12345" , base: 10, src_len: 10); |
44 | EXPECT_FALSE(result.has_error()); |
45 | EXPECT_EQ(result.parsed_len, ptrdiff_t(10)); |
46 | ASSERT_EQ(result.value, 12345); |
47 | |
48 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " 12345" , base: 10, src_len: 7); |
49 | EXPECT_FALSE(result.has_error()); |
50 | EXPECT_EQ(result.parsed_len, ptrdiff_t(7)); |
51 | ASSERT_EQ(result.value, 12); |
52 | |
53 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " 12345" , base: 10, src_len: 5); |
54 | EXPECT_FALSE(result.has_error()); |
55 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
56 | ASSERT_EQ(result.value, 0); |
57 | |
58 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " 12345" , base: 10, src_len: 0); |
59 | EXPECT_FALSE(result.has_error()); |
60 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
61 | ASSERT_EQ(result.value, 0); |
62 | } |
63 | |
64 | TEST(LlvmLibcStrToIntegerTest, LeadingSign) { |
65 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "+12345" , base: 10, src_len: 10); |
66 | EXPECT_FALSE(result.has_error()); |
67 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
68 | ASSERT_EQ(result.value, 12345); |
69 | |
70 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "-12345" , base: 10, src_len: 10); |
71 | EXPECT_FALSE(result.has_error()); |
72 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
73 | ASSERT_EQ(result.value, -12345); |
74 | |
75 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "+12345" , base: 10, src_len: 6); |
76 | EXPECT_FALSE(result.has_error()); |
77 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
78 | ASSERT_EQ(result.value, 12345); |
79 | |
80 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "-12345" , base: 10, src_len: 6); |
81 | EXPECT_FALSE(result.has_error()); |
82 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
83 | ASSERT_EQ(result.value, -12345); |
84 | |
85 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "+12345" , base: 10, src_len: 3); |
86 | EXPECT_FALSE(result.has_error()); |
87 | EXPECT_EQ(result.parsed_len, ptrdiff_t(3)); |
88 | ASSERT_EQ(result.value, 12); |
89 | |
90 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "-12345" , base: 10, src_len: 3); |
91 | EXPECT_FALSE(result.has_error()); |
92 | EXPECT_EQ(result.parsed_len, ptrdiff_t(3)); |
93 | ASSERT_EQ(result.value, -12); |
94 | |
95 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "+12345" , base: 10, src_len: 1); |
96 | EXPECT_FALSE(result.has_error()); |
97 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
98 | ASSERT_EQ(result.value, 0); |
99 | |
100 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "-12345" , base: 10, src_len: 1); |
101 | EXPECT_FALSE(result.has_error()); |
102 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
103 | ASSERT_EQ(result.value, 0); |
104 | |
105 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "+12345" , base: 10, src_len: 0); |
106 | EXPECT_FALSE(result.has_error()); |
107 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
108 | ASSERT_EQ(result.value, 0); |
109 | |
110 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "-12345" , base: 10, src_len: 0); |
111 | EXPECT_FALSE(result.has_error()); |
112 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
113 | ASSERT_EQ(result.value, 0); |
114 | } |
115 | |
116 | TEST(LlvmLibcStrToIntegerTest, Base16PrefixAutoSelect) { |
117 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 0, src_len: 10); |
118 | EXPECT_FALSE(result.has_error()); |
119 | EXPECT_EQ(result.parsed_len, ptrdiff_t(7)); |
120 | ASSERT_EQ(result.value, 0x12345); |
121 | |
122 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 0, src_len: 7); |
123 | EXPECT_FALSE(result.has_error()); |
124 | EXPECT_EQ(result.parsed_len, ptrdiff_t(7)); |
125 | ASSERT_EQ(result.value, 0x12345); |
126 | |
127 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 0, src_len: 5); |
128 | EXPECT_FALSE(result.has_error()); |
129 | EXPECT_EQ(result.parsed_len, ptrdiff_t(5)); |
130 | ASSERT_EQ(result.value, 0x123); |
131 | |
132 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 0, src_len: 2); |
133 | EXPECT_FALSE(result.has_error()); |
134 | EXPECT_EQ(result.parsed_len, ptrdiff_t(1)); |
135 | ASSERT_EQ(result.value, 0); |
136 | |
137 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 0, src_len: 0); |
138 | EXPECT_FALSE(result.has_error()); |
139 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
140 | ASSERT_EQ(result.value, 0); |
141 | } |
142 | |
143 | TEST(LlvmLibcStrToIntegerTest, Base16PrefixManualSelect) { |
144 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 16, src_len: 10); |
145 | EXPECT_FALSE(result.has_error()); |
146 | EXPECT_EQ(result.parsed_len, ptrdiff_t(7)); |
147 | ASSERT_EQ(result.value, 0x12345); |
148 | |
149 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 16, src_len: 7); |
150 | EXPECT_FALSE(result.has_error()); |
151 | EXPECT_EQ(result.parsed_len, ptrdiff_t(7)); |
152 | ASSERT_EQ(result.value, 0x12345); |
153 | |
154 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 16, src_len: 5); |
155 | EXPECT_FALSE(result.has_error()); |
156 | EXPECT_EQ(result.parsed_len, ptrdiff_t(5)); |
157 | ASSERT_EQ(result.value, 0x123); |
158 | |
159 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 16, src_len: 2); |
160 | EXPECT_FALSE(result.has_error()); |
161 | EXPECT_EQ(result.parsed_len, ptrdiff_t(1)); |
162 | ASSERT_EQ(result.value, 0); |
163 | |
164 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "0x12345" , base: 16, src_len: 0); |
165 | EXPECT_FALSE(result.has_error()); |
166 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
167 | ASSERT_EQ(result.value, 0); |
168 | } |
169 | |
170 | TEST(LlvmLibcStrToIntegerTest, Base8PrefixAutoSelect) { |
171 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 0, src_len: 10); |
172 | EXPECT_FALSE(result.has_error()); |
173 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
174 | ASSERT_EQ(result.value, 012345); |
175 | |
176 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 0, src_len: 6); |
177 | EXPECT_FALSE(result.has_error()); |
178 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
179 | ASSERT_EQ(result.value, 012345); |
180 | |
181 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 0, src_len: 4); |
182 | EXPECT_FALSE(result.has_error()); |
183 | EXPECT_EQ(result.parsed_len, ptrdiff_t(4)); |
184 | ASSERT_EQ(result.value, 0123); |
185 | |
186 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 0, src_len: 1); |
187 | EXPECT_FALSE(result.has_error()); |
188 | EXPECT_EQ(result.parsed_len, ptrdiff_t(1)); |
189 | ASSERT_EQ(result.value, 0); |
190 | |
191 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 0, src_len: 0); |
192 | EXPECT_FALSE(result.has_error()); |
193 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
194 | ASSERT_EQ(result.value, 0); |
195 | } |
196 | |
197 | TEST(LlvmLibcStrToIntegerTest, Base8PrefixManualSelect) { |
198 | auto result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 8, src_len: 10); |
199 | EXPECT_FALSE(result.has_error()); |
200 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
201 | ASSERT_EQ(result.value, 012345); |
202 | |
203 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 8, src_len: 6); |
204 | EXPECT_FALSE(result.has_error()); |
205 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
206 | ASSERT_EQ(result.value, 012345); |
207 | |
208 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 8, src_len: 4); |
209 | EXPECT_FALSE(result.has_error()); |
210 | EXPECT_EQ(result.parsed_len, ptrdiff_t(4)); |
211 | ASSERT_EQ(result.value, 0123); |
212 | |
213 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 8, src_len: 1); |
214 | EXPECT_FALSE(result.has_error()); |
215 | EXPECT_EQ(result.parsed_len, ptrdiff_t(1)); |
216 | ASSERT_EQ(result.value, 0); |
217 | |
218 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: "012345" , base: 8, src_len: 0); |
219 | EXPECT_FALSE(result.has_error()); |
220 | EXPECT_EQ(result.parsed_len, ptrdiff_t(0)); |
221 | ASSERT_EQ(result.value, 0); |
222 | } |
223 | |
224 | TEST(LlvmLibcStrToIntegerTest, CombinedTests) { |
225 | auto result = |
226 | LIBC_NAMESPACE::internal::strtointeger<int>(src: " -0x123" , base: 0, src_len: 10); |
227 | EXPECT_FALSE(result.has_error()); |
228 | EXPECT_EQ(result.parsed_len, ptrdiff_t(10)); |
229 | ASSERT_EQ(result.value, -0x123); |
230 | |
231 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " -0x123" , base: 0, src_len: 8); |
232 | EXPECT_FALSE(result.has_error()); |
233 | EXPECT_EQ(result.parsed_len, ptrdiff_t(8)); |
234 | ASSERT_EQ(result.value, -0x1); |
235 | |
236 | result = LIBC_NAMESPACE::internal::strtointeger<int>(src: " -0x123" , base: 0, src_len: 7); |
237 | EXPECT_FALSE(result.has_error()); |
238 | EXPECT_EQ(result.parsed_len, ptrdiff_t(6)); |
239 | ASSERT_EQ(result.value, 0); |
240 | } |
241 | |