Make test more lenient for custom clang version strings
[llvm-project.git] / libc / test / src / __support / str_to_integer_test.cpp
blob34b645b4b38c831d51d4fe084b265f600b46f98c
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/str_to_integer.h"
10 #include "src/errno/libc_errno.h"
11 #include <stddef.h>
13 #include "test/UnitTest/Test.h"
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.
19 TEST(LlvmLibcStrToIntegerTest, SimpleLength) {
20 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("12345", 10, 10);
21 EXPECT_FALSE(result.has_error());
22 EXPECT_EQ(result.parsed_len, ptrdiff_t(5));
23 ASSERT_EQ(result.value, 12345);
25 result = LIBC_NAMESPACE::internal::strtointeger<int>("12345", 10, 2);
26 EXPECT_FALSE(result.has_error());
27 EXPECT_EQ(result.parsed_len, ptrdiff_t(2));
28 ASSERT_EQ(result.value, 12);
30 result = LIBC_NAMESPACE::internal::strtointeger<int>("12345", 10, 0);
31 EXPECT_FALSE(result.has_error());
32 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
33 ASSERT_EQ(result.value, 0);
36 TEST(LlvmLibcStrToIntegerTest, LeadingSpaces) {
37 auto result =
38 LIBC_NAMESPACE::internal::strtointeger<int>(" 12345", 10, 15);
39 EXPECT_FALSE(result.has_error());
40 EXPECT_EQ(result.parsed_len, ptrdiff_t(10));
41 ASSERT_EQ(result.value, 12345);
43 result = LIBC_NAMESPACE::internal::strtointeger<int>(" 12345", 10, 10);
44 EXPECT_FALSE(result.has_error());
45 EXPECT_EQ(result.parsed_len, ptrdiff_t(10));
46 ASSERT_EQ(result.value, 12345);
48 result = LIBC_NAMESPACE::internal::strtointeger<int>(" 12345", 10, 7);
49 EXPECT_FALSE(result.has_error());
50 EXPECT_EQ(result.parsed_len, ptrdiff_t(7));
51 ASSERT_EQ(result.value, 12);
53 result = LIBC_NAMESPACE::internal::strtointeger<int>(" 12345", 10, 5);
54 EXPECT_FALSE(result.has_error());
55 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
56 ASSERT_EQ(result.value, 0);
58 result = LIBC_NAMESPACE::internal::strtointeger<int>(" 12345", 10, 0);
59 EXPECT_FALSE(result.has_error());
60 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
61 ASSERT_EQ(result.value, 0);
64 TEST(LlvmLibcStrToIntegerTest, LeadingSign) {
65 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("+12345", 10, 10);
66 EXPECT_FALSE(result.has_error());
67 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
68 ASSERT_EQ(result.value, 12345);
70 result = LIBC_NAMESPACE::internal::strtointeger<int>("-12345", 10, 10);
71 EXPECT_FALSE(result.has_error());
72 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
73 ASSERT_EQ(result.value, -12345);
75 result = LIBC_NAMESPACE::internal::strtointeger<int>("+12345", 10, 6);
76 EXPECT_FALSE(result.has_error());
77 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
78 ASSERT_EQ(result.value, 12345);
80 result = LIBC_NAMESPACE::internal::strtointeger<int>("-12345", 10, 6);
81 EXPECT_FALSE(result.has_error());
82 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
83 ASSERT_EQ(result.value, -12345);
85 result = LIBC_NAMESPACE::internal::strtointeger<int>("+12345", 10, 3);
86 EXPECT_FALSE(result.has_error());
87 EXPECT_EQ(result.parsed_len, ptrdiff_t(3));
88 ASSERT_EQ(result.value, 12);
90 result = LIBC_NAMESPACE::internal::strtointeger<int>("-12345", 10, 3);
91 EXPECT_FALSE(result.has_error());
92 EXPECT_EQ(result.parsed_len, ptrdiff_t(3));
93 ASSERT_EQ(result.value, -12);
95 result = LIBC_NAMESPACE::internal::strtointeger<int>("+12345", 10, 1);
96 EXPECT_FALSE(result.has_error());
97 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
98 ASSERT_EQ(result.value, 0);
100 result = LIBC_NAMESPACE::internal::strtointeger<int>("-12345", 10, 1);
101 EXPECT_FALSE(result.has_error());
102 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
103 ASSERT_EQ(result.value, 0);
105 result = LIBC_NAMESPACE::internal::strtointeger<int>("+12345", 10, 0);
106 EXPECT_FALSE(result.has_error());
107 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
108 ASSERT_EQ(result.value, 0);
110 result = LIBC_NAMESPACE::internal::strtointeger<int>("-12345", 10, 0);
111 EXPECT_FALSE(result.has_error());
112 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
113 ASSERT_EQ(result.value, 0);
116 TEST(LlvmLibcStrToIntegerTest, Base16PrefixAutoSelect) {
117 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 0, 10);
118 EXPECT_FALSE(result.has_error());
119 EXPECT_EQ(result.parsed_len, ptrdiff_t(7));
120 ASSERT_EQ(result.value, 0x12345);
122 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 0, 7);
123 EXPECT_FALSE(result.has_error());
124 EXPECT_EQ(result.parsed_len, ptrdiff_t(7));
125 ASSERT_EQ(result.value, 0x12345);
127 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 0, 5);
128 EXPECT_FALSE(result.has_error());
129 EXPECT_EQ(result.parsed_len, ptrdiff_t(5));
130 ASSERT_EQ(result.value, 0x123);
132 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 0, 2);
133 EXPECT_FALSE(result.has_error());
134 EXPECT_EQ(result.parsed_len, ptrdiff_t(1));
135 ASSERT_EQ(result.value, 0);
137 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 0, 0);
138 EXPECT_FALSE(result.has_error());
139 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
140 ASSERT_EQ(result.value, 0);
143 TEST(LlvmLibcStrToIntegerTest, Base16PrefixManualSelect) {
144 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 16, 10);
145 EXPECT_FALSE(result.has_error());
146 EXPECT_EQ(result.parsed_len, ptrdiff_t(7));
147 ASSERT_EQ(result.value, 0x12345);
149 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 16, 7);
150 EXPECT_FALSE(result.has_error());
151 EXPECT_EQ(result.parsed_len, ptrdiff_t(7));
152 ASSERT_EQ(result.value, 0x12345);
154 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 16, 5);
155 EXPECT_FALSE(result.has_error());
156 EXPECT_EQ(result.parsed_len, ptrdiff_t(5));
157 ASSERT_EQ(result.value, 0x123);
159 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 16, 2);
160 EXPECT_FALSE(result.has_error());
161 EXPECT_EQ(result.parsed_len, ptrdiff_t(1));
162 ASSERT_EQ(result.value, 0);
164 result = LIBC_NAMESPACE::internal::strtointeger<int>("0x12345", 16, 0);
165 EXPECT_FALSE(result.has_error());
166 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
167 ASSERT_EQ(result.value, 0);
170 TEST(LlvmLibcStrToIntegerTest, Base8PrefixAutoSelect) {
171 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 0, 10);
172 EXPECT_FALSE(result.has_error());
173 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
174 ASSERT_EQ(result.value, 012345);
176 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 0, 6);
177 EXPECT_FALSE(result.has_error());
178 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
179 ASSERT_EQ(result.value, 012345);
181 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 0, 4);
182 EXPECT_FALSE(result.has_error());
183 EXPECT_EQ(result.parsed_len, ptrdiff_t(4));
184 ASSERT_EQ(result.value, 0123);
186 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 0, 1);
187 EXPECT_FALSE(result.has_error());
188 EXPECT_EQ(result.parsed_len, ptrdiff_t(1));
189 ASSERT_EQ(result.value, 0);
191 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 0, 0);
192 EXPECT_FALSE(result.has_error());
193 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
194 ASSERT_EQ(result.value, 0);
197 TEST(LlvmLibcStrToIntegerTest, Base8PrefixManualSelect) {
198 auto result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 8, 10);
199 EXPECT_FALSE(result.has_error());
200 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
201 ASSERT_EQ(result.value, 012345);
203 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 8, 6);
204 EXPECT_FALSE(result.has_error());
205 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
206 ASSERT_EQ(result.value, 012345);
208 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 8, 4);
209 EXPECT_FALSE(result.has_error());
210 EXPECT_EQ(result.parsed_len, ptrdiff_t(4));
211 ASSERT_EQ(result.value, 0123);
213 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 8, 1);
214 EXPECT_FALSE(result.has_error());
215 EXPECT_EQ(result.parsed_len, ptrdiff_t(1));
216 ASSERT_EQ(result.value, 0);
218 result = LIBC_NAMESPACE::internal::strtointeger<int>("012345", 8, 0);
219 EXPECT_FALSE(result.has_error());
220 EXPECT_EQ(result.parsed_len, ptrdiff_t(0));
221 ASSERT_EQ(result.value, 0);
224 TEST(LlvmLibcStrToIntegerTest, CombinedTests) {
225 auto result =
226 LIBC_NAMESPACE::internal::strtointeger<int>(" -0x123", 0, 10);
227 EXPECT_FALSE(result.has_error());
228 EXPECT_EQ(result.parsed_len, ptrdiff_t(10));
229 ASSERT_EQ(result.value, -0x123);
231 result = LIBC_NAMESPACE::internal::strtointeger<int>(" -0x123", 0, 8);
232 EXPECT_FALSE(result.has_error());
233 EXPECT_EQ(result.parsed_len, ptrdiff_t(8));
234 ASSERT_EQ(result.value, -0x1);
236 result = LIBC_NAMESPACE::internal::strtointeger<int>(" -0x123", 0, 7);
237 EXPECT_FALSE(result.has_error());
238 EXPECT_EQ(result.parsed_len, ptrdiff_t(6));
239 ASSERT_EQ(result.value, 0);