Debian Codegraph @

tests/fast_int.cpp

raw
1 #ifndef __cplusplus
2 #error fastfloat requires a C++ compiler
3 #endif
4
5 // We want to enable the tests only for C++17 and above.
6 #ifndef FASTFLOAT_CPLUSPLUS
7 #if defined(_MSVC_LANG) && !defined(__clang__)
8 #define FASTFLOAT_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG)
9 #else
10 #define FASTFLOAT_CPLUSPLUS __cplusplus
11 #endif
12 #endif
13
14 #if FASTFLOAT_CPLUSPLUS >= 201703L
15
16 #include <cstdlib>
17 #include <iostream>
18 #include <vector>
19 #include <string_view>
20 #include <cstring>
21 #include "fast_float/fast_float.h"
22 #include <cstdint>
23
24 /*
25 all tests conducted are to check fast_float::from_chars functionality with int
26 and unsigned test cases include: int basic tests - numbers only, numbers with
27 strings behind, decimals, negative numbers unsigned basic tests - numbers only,
28 numbers with strings behind, decimals int invalid tests - strings only, strings
29 with numbers behind, space in front of number, plus sign in front of number
30 unsigned invalid tests - strings only, strings with numbers behind, space in
31 front of number, plus/minus sign in front of number int out of range tests -
32 numbers exceeding int bit size for 8, 16, 32, and 64 bits unsigned out of range
33 tests - numbers exceeding unsigned bit size 8, 16, 32, and 64 bits int pointer
34 tests - points to first character that is not recognized as int unsigned pointer
35 tests - points to first character that is not recognized as unsigned
36 int/unsigned base 2 tests - numbers are converted from binary to decimal
37 octal tests - numbers are converted from octal to decimal
38 hex tests - numbers are converted from hex to decimal (Note: 0x and 0X are
39 considered invalid) invalid base tests - any base not within 2-36 is invalid out
40 of range base tests - numbers exceeding int/unsigned bit size after converted
41 from base (Note: only 64 bit int and unsigned are tested) within range base
42 tests - max/min numbers are still within int/unsigned bit size after converted
43 from base (Note: only 64 bit int and unsigned are tested) leading zeros tests -
44 ignores all zeroes in front of valid number after converted from base
45 */
46
47 int main() {
48 // int basic test
49 std::vector<int> const int_basic_test_expected{0, 10, -40, 1001, 9};
50 std::vector<std::string_view> const int_basic_test{"0", "10 ", "-40",
51 "1001 with text", "9.999"};
52
53 for (std::size_t i = 0; i < int_basic_test.size(); ++i) {
54 auto const f = int_basic_test[i];
55 int result;
56 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
57
58 if (answer.ec != std::errc()) {
59 if (answer.ec == std::errc::invalid_argument) {
60 std::cerr << "could not convert to int for input: \"" << f
61 << "\" because of invalid argument" << std::endl;
62 } else if (answer.ec == std::errc::result_out_of_range) {
63 std::cerr << "could not convert to int for input: \"" << f
64 << "\" because it's out of range" << std::endl;
65 } else {
66 std::cerr << "could not convert to int for input: \"" << f
67 << "\" because of an unknown error" << std::endl;
68 }
69 return EXIT_FAILURE;
70 } else if (result != int_basic_test_expected[i]) {
71 std::cerr << "result \"" << f << "\" did not match with expected int: "
72 << int_basic_test_expected[i] << std::endl;
73 return EXIT_FAILURE;
74 }
75 }
76
77 // unsigned basic test
78 std::vector<unsigned> const unsigned_basic_test_expected{0, 10, 1001, 9};
79 std::vector<std::string_view> const unsigned_basic_test{
80 "0", "10 ", "1001 with text", "9.999"};
81
82 for (std::size_t i = 0; i < unsigned_basic_test.size(); ++i) {
83 auto const &f = unsigned_basic_test[i];
84 unsigned result;
85 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
86 if (answer.ec != std::errc()) {
87 std::cerr << "could not convert to unsigned for input: \"" << f << "\""
88 << std::endl;
89 return EXIT_FAILURE;
90 } else if (result != unsigned_basic_test_expected[i]) {
91 std::cerr << "result \"" << f
92 << "\" did not match with expected unsigned: "
93 << unsigned_basic_test_expected[i] << std::endl;
94 return EXIT_FAILURE;
95 }
96 }
97
98 // int invalid error test
99 std::vector<std::string_view> const int_invalid_argument_test{
100 "text", "text with 1002", "+50", " 50"};
101
102 for (std::size_t i = 0; i < int_invalid_argument_test.size(); ++i) {
103 auto const &f = int_invalid_argument_test[i];
104 int result;
105 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
106 if (answer.ec != std::errc::invalid_argument) {
107 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
108 << "\"" << std::endl;
109 return EXIT_FAILURE;
110 }
111 }
112
113 // unsigned invalid error test
114 std::vector<std::string_view> const unsigned_invalid_argument_test{
115 "text", "text with 1002", "+50", " 50", "-50"};
116
117 for (std::size_t i = 0; i < unsigned_invalid_argument_test.size(); ++i) {
118 auto const &f = unsigned_invalid_argument_test[i];
119 unsigned result;
120 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
121 if (answer.ec != std::errc::invalid_argument) {
122 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
123 << "\"" << std::endl;
124 return EXIT_FAILURE;
125 }
126 }
127
128 // int out of range error test #1 (8 bit)
129 std::vector<std::string_view> const int_out_of_range_test_1{
130 "2000000000000000000000", "128", "-129"};
131
132 for (std::size_t i = 0; i < int_out_of_range_test_1.size(); ++i) {
133 auto const &f = int_out_of_range_test_1[i];
134 int8_t result;
135 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
136 if (answer.ec != std::errc::result_out_of_range) {
137 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
138 << "\"" << std::endl;
139 return EXIT_FAILURE;
140 }
141 }
142
143 // int out of range error test #2 (16 bit)
144 std::vector<std::string_view> const int_out_of_range_test_2{
145 "2000000000000000000000", "32768", "-32769"};
146
147 for (std::size_t i = 0; i < int_out_of_range_test_2.size(); ++i) {
148 auto const &f = int_out_of_range_test_2[i];
149 int16_t result;
150 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
151 if (answer.ec != std::errc::result_out_of_range) {
152 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
153 << "\"" << std::endl;
154 return EXIT_FAILURE;
155 }
156 }
157
158 // int out of range error test #3 (32 bit)
159 std::vector<std::string_view> const int_out_of_range_test_3{
160 "2000000000000000000000", "2147483648", "-2147483649"};
161
162 for (std::size_t i = 0; i < int_out_of_range_test_3.size(); ++i) {
163 auto const &f = int_out_of_range_test_3[i];
164 int32_t result;
165 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
166 if (answer.ec != std::errc::result_out_of_range) {
167 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
168 << "\"" << std::endl;
169 return EXIT_FAILURE;
170 }
171 }
172
173 // int out of range error test #4 (64 bit)
174 std::vector<std::string_view> const int_out_of_range_test_4{
175 "2000000000000000000000", "9223372036854775808", "-9223372036854775809"};
176
177 for (std::size_t i = 0; i < int_out_of_range_test_4.size(); ++i) {
178 auto const &f = int_out_of_range_test_4[i];
179 int64_t result;
180 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
181 if (answer.ec != std::errc::result_out_of_range) {
182 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
183 << "\"" << std::endl;
184 return EXIT_FAILURE;
185 }
186 }
187
188 // unsigned out of range error test #1 (8 bit)
189 std::vector<std::string_view> const unsigned_out_of_range_test_1{
190 "2000000000000000000000", "256"};
191
192 for (std::size_t i = 0; i < unsigned_out_of_range_test_1.size(); ++i) {
193 auto const &f = unsigned_out_of_range_test_1[i];
194 uint8_t result;
195 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
196 if (answer.ec != std::errc::result_out_of_range) {
197 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
198 << "\"" << std::endl;
199 return EXIT_FAILURE;
200 }
201 }
202
203 // unsigned out of range error test #2 (16 bit)
204 std::vector<std::string_view> const unsigned_out_of_range_test_2{
205 "2000000000000000000000", "65536"};
206
207 for (std::size_t i = 0; i < unsigned_out_of_range_test_2.size(); ++i) {
208 auto const &f = unsigned_out_of_range_test_2[i];
209 uint16_t result;
210 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
211 if (answer.ec != std::errc::result_out_of_range) {
212 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
213 << "\"" << std::endl;
214 return EXIT_FAILURE;
215 }
216 }
217
218 // unsigned out of range error test #3 (32 bit)
219 std::vector<std::string_view> const unsigned_out_of_range_test_3{
220 "2000000000000000000000", "4294967296"};
221
222 for (std::size_t i = 0; i < unsigned_out_of_range_test_3.size(); ++i) {
223 auto const &f = unsigned_out_of_range_test_3[i];
224 uint32_t result;
225 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
226 if (answer.ec != std::errc::result_out_of_range) {
227 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
228 << "\"" << std::endl;
229 return EXIT_FAILURE;
230 }
231 }
232
233 // unsigned out of range error test #4 (64 bit)
234 std::vector<std::string_view> const unsigned_out_of_range_test_4{
235 "2000000000000000000000", "18446744073709551616"};
236
237 for (std::size_t i = 0; i < unsigned_out_of_range_test_4.size(); ++i) {
238 auto const &f = unsigned_out_of_range_test_4[i];
239 uint64_t result;
240 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
241 if (answer.ec != std::errc::result_out_of_range) {
242 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
243 << "\"" << std::endl;
244 return EXIT_FAILURE;
245 }
246 }
247
248 // int pointer test #1 (only numbers)
249 std::vector<std::string_view> const int_pointer_test_1{"0", "010", "-40"};
250
251 for (std::size_t i = 0; i < int_pointer_test_1.size(); ++i) {
252 auto const &f = int_pointer_test_1[i];
253 int result;
254 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
255 if (answer.ec != std::errc()) {
256 std::cerr << "could not convert to int for input: \"" << f << "\""
257 << std::endl;
258 return EXIT_FAILURE;
259 } else if (strcmp(answer.ptr, "") != 0) {
260 std::cerr << "ptr of result " << f
261 << " did not match with expected ptr: \"\"" << std::endl;
262 return EXIT_FAILURE;
263 }
264 }
265
266 // int pointer test #2 (string behind numbers)
267 std::string_view const int_pointer_test_2 = "1001 with text";
268
269 auto const &f2 = int_pointer_test_2;
270 int result2;
271 auto answer2 =
272 fast_float::from_chars(f2.data(), f2.data() + f2.size(), result2);
273 if (strcmp(answer2.ptr, " with text") != 0) {
274 std::cerr << "ptr of result " << f2
275 << " did not match with expected ptr: \"with text\"" << std::endl;
276 return EXIT_FAILURE;
277 }
278
279 // int pointer test #3 (string with newline behind numbers)
280 std::string_view const int_pointer_test_3 = "1001 with text\n";
281
282 auto const &f3 = int_pointer_test_3;
283 int result3;
284 auto answer3 =
285 fast_float::from_chars(f3.data(), f3.data() + f3.size(), result3);
286 if (strcmp(answer3.ptr, " with text\n") != 0) {
287 std::cerr << "ptr of result " << f3
288 << " did not match with expected ptr: with text" << std::endl;
289 return EXIT_FAILURE;
290 }
291
292 // int pointer test #4 (float)
293 std::string_view const int_pointer_test_4 = "9.999";
294
295 auto const &f4 = int_pointer_test_4;
296 int result4;
297 auto answer4 =
298 fast_float::from_chars(f4.data(), f4.data() + f4.size(), result4);
299 if (strcmp(answer4.ptr, ".999") != 0) {
300 std::cerr << "ptr of result " << f4
301 << " did not match with expected ptr: .999" << std::endl;
302 return EXIT_FAILURE;
303 }
304
305 // int pointer test #5 (invalid int)
306 std::string_view const int_pointer_test_5 = "+50";
307
308 auto const &f5 = int_pointer_test_5;
309 int result5;
310 auto answer5 =
311 fast_float::from_chars(f5.data(), f5.data() + f5.size(), result5);
312 if (strcmp(answer5.ptr, "+50") != 0) {
313 std::cerr << "ptr of result " << f5
314 << " did not match with expected ptr: +50" << std::endl;
315 return EXIT_FAILURE;
316 }
317
318 // unsigned pointer test #2 (string behind numbers)
319 std::string_view const unsigned_pointer_test_1 = "1001 with text";
320
321 auto const &f6 = unsigned_pointer_test_1;
322 unsigned result6;
323 auto answer6 =
324 fast_float::from_chars(f6.data(), f6.data() + f6.size(), result6);
325 if (strcmp(answer6.ptr, " with text") != 0) {
326 std::cerr << "ptr of result " << f6
327 << " did not match with expected ptr: with text" << std::endl;
328 return EXIT_FAILURE;
329 }
330
331 // unsigned pointer test #2 (invalid unsigned)
332 std::string_view const unsigned_pointer_test_2 = "-50";
333
334 auto const &f7 = unsigned_pointer_test_2;
335 unsigned result7;
336 auto answer7 =
337 fast_float::from_chars(f7.data(), f7.data() + f7.size(), result7);
338 if (strcmp(answer7.ptr, "-50") != 0) {
339 std::cerr << "ptr of result " << f7
340 << " did not match with expected ptr: -50" << std::endl;
341 return EXIT_FAILURE;
342 }
343
344 // int base 2 test
345 std::vector<int> const int_base_2_test_expected{0, 1, 4, 2, -1};
346 std::vector<std::string_view> const int_base_2_test{"0", "1", "100", "010",
347 "-1"};
348
349 for (std::size_t i = 0; i < int_base_2_test.size(); ++i) {
350 auto const f = int_base_2_test[i];
351 int result;
352 auto answer =
353 fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
354 if (answer.ec != std::errc()) {
355 std::cerr << "could not convert to int for input: \"" << f << "\""
356 << std::endl;
357 return EXIT_FAILURE;
358 } else if (result != int_base_2_test_expected[i]) {
359 std::cerr << "result " << f << " did not match with expected int: "
360 << int_base_2_test_expected[i] << std::endl;
361 return EXIT_FAILURE;
362 }
363 }
364
365 // unsigned base 2 test
366 std::vector<unsigned> const unsigned_base_2_test_expected{0, 1, 4, 2};
367 std::vector<std::string_view> const unsigned_base_2_test{"0", "1", "100",
368 "010"};
369
370 for (std::size_t i = 0; i < unsigned_base_2_test.size(); ++i) {
371 auto const &f = unsigned_base_2_test[i];
372 unsigned result;
373 auto answer =
374 fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
375 if (answer.ec != std::errc()) {
376 std::cerr << "could not convert to unsigned for input: \"" << f << "\""
377 << std::endl;
378 return EXIT_FAILURE;
379 } else if (result != unsigned_base_2_test_expected[i]) {
380 std::cerr << "result " << f << " did not match with expected unsigned: "
381 << unsigned_base_2_test_expected[i] << std::endl;
382 return EXIT_FAILURE;
383 }
384 }
385
386 // int invalid error base 2 test
387 std::vector<std::string_view> const int_invalid_argument_base_2_test{"2", "A",
388 "-2"};
389
390 for (std::size_t i = 0; i < int_invalid_argument_base_2_test.size(); ++i) {
391 auto const &f = int_invalid_argument_base_2_test[i];
392 int result;
393 auto answer =
394 fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
395 if (answer.ec != std::errc::invalid_argument) {
396 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
397 << "\"" << std::endl;
398 return EXIT_FAILURE;
399 }
400 }
401
402 // unsigned invalid error base 2 test
403 std::vector<std::string_view> const unsigned_invalid_argument_base_2_test{
404 "2", "A", "-1", "-2"};
405
406 for (std::size_t i = 0; i < unsigned_invalid_argument_base_2_test.size();
407 ++i) {
408 auto const &f = unsigned_invalid_argument_base_2_test[i];
409 unsigned result;
410 auto answer =
411 fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
412 if (answer.ec != std::errc::invalid_argument) {
413 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
414 << "\"" << std::endl;
415 return EXIT_FAILURE;
416 }
417 }
418
419 // octal test
420 std::vector<int> const base_octal_test_expected{0, 1, 7, 8, 9};
421 std::vector<std::string_view> const base_octal_test{"0", "1", "07", "010",
422 "0011"};
423
424 for (std::size_t i = 0; i < base_octal_test.size(); ++i) {
425 auto const &f = base_octal_test[i];
426 int result;
427 auto answer =
428 fast_float::from_chars(f.data(), f.data() + f.size(), result, 8);
429 if (answer.ec != std::errc()) {
430 std::cerr << "could not convert to int for input: \"" << f << "\""
431 << std::endl;
432 return EXIT_FAILURE;
433 } else if (result != base_octal_test_expected[i]) {
434 std::cerr << "result " << f << " did not match with expected int: "
435 << base_octal_test_expected[i] << std::endl;
436 return EXIT_FAILURE;
437 }
438 }
439
440 // hex test
441 std::vector<int> const base_hex_test_expected{0, 1, 15, 31, 0, 16};
442 std::vector<std::string_view> const base_hex_test{"0", "1", "F",
443 "01f", "0x11", "10X11"};
444
445 for (std::size_t i = 0; i < base_hex_test.size(); ++i) {
446 auto const &f = base_hex_test[i];
447 int result;
448 auto answer =
449 fast_float::from_chars(f.data(), f.data() + f.size(), result, 16);
450 if (answer.ec != std::errc()) {
451 std::cerr << "could not convert to int for input: \"" << f << "\""
452 << std::endl;
453 return EXIT_FAILURE;
454 } else if (result != base_hex_test_expected[i]) {
455 std::cerr << "result " << f << " did not match with expected int: "
456 << base_hex_test_expected[i] << std::endl;
457 return EXIT_FAILURE;
458 }
459 }
460
461 // invalid base test #1 (-1)
462 std::vector<std::string_view> const invalid_base_test_1{"0", "1", "-1", "F",
463 "10Z"};
464
465 for (std::size_t i = 0; i < invalid_base_test_1.size(); ++i) {
466 auto const &f = invalid_base_test_1[i];
467 int result;
468 auto answer =
469 fast_float::from_chars(f.data(), f.data() + f.size(), result, -1);
470 if (answer.ec != std::errc::invalid_argument) {
471 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
472 << "\"" << std::endl;
473 return EXIT_FAILURE;
474 }
475 }
476
477 // invalid base test #2 (37)
478 std::vector<std::string_view> const invalid_base_test_2{"0", "1", "F", "Z",
479 "10Z"};
480
481 for (std::size_t i = 0; i < invalid_base_test_2.size(); ++i) {
482 auto const &f = invalid_base_test_2[i];
483 int result;
484 auto answer =
485 fast_float::from_chars(f.data(), f.data() + f.size(), result, 37);
486 if (answer.ec != std::errc::invalid_argument) {
487 std::cerr << "expected error should be 'invalid_argument' for: \"" << f
488 << "\"" << std::endl;
489 return EXIT_FAILURE;
490 }
491 }
492
493 // int out of range error base test (64 bit)
494 std::vector<std::string_view> const int_out_of_range_base_test{
495 "1000000000000000000000000000000000000000000000000000000000000000",
496 "-1000000000000000000000000000000000000000000000000000000000000001",
497 "2021110011022210012102010021220101220222",
498 "-2021110011022210012102010021220101221000",
499 "20000000000000000000000000000000",
500 "-20000000000000000000000000000001",
501 "1104332401304422434310311213",
502 "-1104332401304422434310311214",
503 "1540241003031030222122212",
504 "-1540241003031030222122213",
505 "22341010611245052052301",
506 "-22341010611245052052302",
507 "1000000000000000000000",
508 "-1000000000000000000001",
509 "67404283172107811828",
510 "-67404283172107811830",
511 "9223372036854775808",
512 "-9223372036854775809",
513 "1728002635214590698",
514 "-1728002635214590699",
515 "41A792678515120368",
516 "-41A792678515120369",
517 "10B269549075433C38",
518 "-10B269549075433C39",
519 "4340724C6C71DC7A8",
520 "-4340724C6C71DC7A9",
521 "160E2AD3246366808",
522 "-160E2AD3246366809",
523 "8000000000000000",
524 "-8000000000000001",
525 "33D3D8307B214009",
526 "-33D3D8307B21400A",
527 "16AGH595DF825FA8",
528 "-16AGH595DF825FA9",
529 "BA643DCI0FFEEHI",
530 "-BA643DCI0FFEEI0",
531 "5CBFJIA3FH26JA8",
532 "-5CBFJIA3FH26JA9",
533 "2HEICIIIE82DH98",
534 "-2HEICIIIE82DH99",
535 "1ADAIBB21DCKFA8",
536 "-1ADAIBB21DCKFA9",
537 "I6K448CF4192C3",
538 "-I6K448CF4192C4",
539 "ACD772JNC9L0L8",
540 "-ACD772JNC9L0L9",
541 "64IE1FOCNN5G78",
542 "-64IE1FOCNN5G79",
543 "3IGOECJBMCA688",
544 "-3IGOECJBMCA689",
545 "27C48L5B37OAOQ",
546 "-27C48L5B37OAP0",
547 "1BK39F3AH3DMQ8",
548 "-1BK39F3AH3DMQ9",
549 "Q1SE8F0M04ISC",
550 "-Q1SE8F0M04ISD",
551 "HAJPPBC1FC208",
552 "-HAJPPBC1FC209",
553 "BM03I95HIA438",
554 "-BM03I95HIA439",
555 "8000000000000",
556 "-8000000000001",
557 "5HG4CK9JD4U38",
558 "-5HG4CK9JD4U39",
559 "3TDTK1V8J6TPQ",
560 "-3TDTK1V8J6TPR",
561 "2PIJMIKEXRXP8",
562 "-2PIJMIKEXRXP9",
563 "1Y2P0IJ32E8E8",
564 "-1Y2P0IJ32E8E9"};
565
566 for (std::size_t i = 0; i < int_out_of_range_base_test.size(); ++i) {
567 auto const &f = int_out_of_range_base_test[i];
568 int64_t result;
569 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result,
570 int(2 + (i / 2)));
571 if (answer.ec != std::errc::result_out_of_range) {
572 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
573 << "\"" << std::endl;
574 return EXIT_FAILURE;
575 }
576 }
577
578 // unsigned out of range error base test (64 bit)
579 std::vector<std::string_view> const unsigned_out_of_range_base_test{
580 "10000000000000000000000000000000000000000000000000000000000000000",
581 "11112220022122120101211020120210210211221",
582 "100000000000000000000000000000000",
583 "2214220303114400424121122431",
584 "3520522010102100444244424",
585 "45012021522523134134602",
586 "2000000000000000000000",
587 "145808576354216723757",
588 "18446744073709551616",
589 "335500516A429071285",
590 "839365134A2A240714",
591 "219505A9511A867B73",
592 "8681049ADB03DB172",
593 "2C1D56B648C6CD111",
594 "10000000000000000",
595 "67979G60F5428011",
596 "2D3FGB0B9CG4BD2G",
597 "141C8786H1CCAAGH",
598 "B53BJH07BE4DJ0G",
599 "5E8G4GGG7G56DIG",
600 "2L4LF104353J8KG",
601 "1DDH88H2782I516",
602 "L12EE5FN0JI1IG",
603 "C9C336O0MLB7EG",
604 "7B7N2PCNIOKCGG",
605 "4EO8HFAM6FLLMP",
606 "2NC6J26L66RHOG",
607 "1N3RSH11F098RO",
608 "14L9LKMO30O40G",
609 "ND075IB45K86G",
610 "G000000000000",
611 "B1W8P7J5Q9R6G",
612 "7ORP63SH4DPHI",
613 "5G24A25TWKWFG",
614 "3W5E11264SGSG"};
615 int base_unsigned = 2;
616 for (std::size_t i = 0; i < unsigned_out_of_range_base_test.size(); ++i) {
617 auto const &f = unsigned_out_of_range_base_test[i];
618 uint64_t result;
619 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result,
620 base_unsigned);
621 if (answer.ec != std::errc::result_out_of_range) {
622 std::cerr << "expected error for should be 'result_out_of_range': \"" << f
623 << "\"" << std::endl;
624 return EXIT_FAILURE;
625 }
626 ++base_unsigned;
627 }
628
629 // just within range base test (64 bit)
630 std::vector<std::string_view> const int_within_range_base_test{
631 "111111111111111111111111111111111111111111111111111111111111111",
632 "-1000000000000000000000000000000000000000000000000000000000000000",
633 "2021110011022210012102010021220101220221",
634 "-2021110011022210012102010021220101220222",
635 "13333333333333333333333333333333",
636 "-20000000000000000000000000000000",
637 "1104332401304422434310311212",
638 "-1104332401304422434310311213",
639 "1540241003031030222122211",
640 "-1540241003031030222122212",
641 "22341010611245052052300",
642 "-22341010611245052052301",
643 "777777777777777777777",
644 "-1000000000000000000000",
645 "67404283172107811827",
646 "-67404283172107811828",
647 "9223372036854775807",
648 "-9223372036854775808",
649 "1728002635214590697",
650 "-1728002635214590698",
651 "41A792678515120367",
652 "-41A792678515120368",
653 "10B269549075433C37",
654 "-10B269549075433C38",
655 "4340724C6C71DC7A7",
656 "-4340724C6C71DC7A8",
657 "160E2AD3246366807",
658 "-160E2AD3246366808",
659 "7FFFFFFFFFFFFFFF",
660 "-8000000000000000",
661 "33D3D8307B214008",
662 "-33D3D8307B214009",
663 "16AGH595DF825FA7",
664 "-16AGH595DF825FA8",
665 "BA643DCI0FFEEHH",
666 "-BA643DCI0FFEEHI",
667 "5CBFJIA3FH26JA7",
668 "-5CBFJIA3FH26JA8",
669 "2HEICIIIE82DH97",
670 "-2HEICIIIE82DH98",
671 "1ADAIBB21DCKFA7",
672 "-1ADAIBB21DCKFA8",
673 "I6K448CF4192C2",
674 "-I6K448CF4192C3",
675 "ACD772JNC9L0L7",
676 "-ACD772JNC9L0L8",
677 "64IE1FOCNN5G77",
678 "-64IE1FOCNN5G78",
679 "3IGOECJBMCA687",
680 "-3IGOECJBMCA688",
681 "27C48L5B37OAOP",
682 "-27C48L5B37OAOQ",
683 "1BK39F3AH3DMQ7",
684 "-1BK39F3AH3DMQ8",
685 "Q1SE8F0M04ISB",
686 "-Q1SE8F0M04ISC",
687 "HAJPPBC1FC207",
688 "-HAJPPBC1FC208",
689 "BM03I95HIA437",
690 "-BM03I95HIA438",
691 "7VVVVVVVVVVVV",
692 "-8000000000000",
693 "5HG4CK9JD4U37",
694 "-5HG4CK9JD4U38",
695 "3TDTK1V8J6TPP",
696 "-3TDTK1V8J6TPQ",
697 "2PIJMIKEXRXP7",
698 "-2PIJMIKEXRXP8",
699 "1Y2P0IJ32E8E7",
700 "-1Y2P0IJ32E8E8"};
701
702 for (std::size_t i = 0; i < int_within_range_base_test.size(); ++i) {
703 auto const &f = int_within_range_base_test[i];
704 int64_t result;
705 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result,
706 int(2 + (i / 2)));
707 if (answer.ec != std::errc()) {
708 std::cerr << "converting " << f
709 << " to int failed (most likely out of range)" << std::endl;
710 return EXIT_FAILURE;
711 }
712 }
713
714 // unsigned within range base test (64 bit)
715 std::vector<std::string_view> const unsigned_within_range_base_test{
716 "1111111111111111111111111111111111111111111111111111111111111111",
717 "11112220022122120101211020120210210211220",
718 "33333333333333333333333333333333",
719 "2214220303114400424121122430",
720 "3520522010102100444244423",
721 "45012021522523134134601",
722 "1777777777777777777777",
723 "145808576354216723756",
724 "18446744073709551615",
725 "335500516A429071284",
726 "839365134A2A240713",
727 "219505A9511A867B72",
728 "8681049ADB03DB171",
729 "2C1D56B648C6CD110",
730 "FFFFFFFFFFFFFFFF",
731 "67979G60F5428010",
732 "2D3FGB0B9CG4BD2F",
733 "141C8786H1CCAAGG",
734 "B53BJH07BE4DJ0F",
735 "5E8G4GGG7G56DIF",
736 "2L4LF104353J8KF",
737 "1DDH88H2782I515",
738 "L12EE5FN0JI1IF",
739 "C9C336O0MLB7EF",
740 "7B7N2PCNIOKCGF",
741 "4EO8HFAM6FLLMO",
742 "2NC6J26L66RHOF",
743 "1N3RSH11F098RN",
744 "14L9LKMO30O40F",
745 "ND075IB45K86F",
746 "FVVVVVVVVVVVV",
747 "B1W8P7J5Q9R6F",
748 "7ORP63SH4DPHH",
749 "5G24A25TWKWFF",
750 "3W5E11264SGSF"};
751 int base_unsigned2 = 2;
752 for (std::size_t i = 0; i < unsigned_within_range_base_test.size(); ++i) {
753 auto const &f = unsigned_within_range_base_test[i];
754 uint64_t result;
755 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result,
756 base_unsigned2);
757 if (answer.ec != std::errc()) {
758 std::cerr << "converting " << f
759 << " to unsigned failed (most likely out of range)"
760 << std::endl;
761 return EXIT_FAILURE;
762 }
763 ++base_unsigned2;
764 }
765
766 // int leading zeros test
767 std::vector<std::string_view> const int_leading_zeros_test{
768 "000000000000000000000000000000000000000000000000000000000000000000000011"
769 "11110111",
770 "000000000000000000000000000000000000000000000000001101121",
771 "000000000000000000000000000000000000000033313",
772 "00000000000000000000000000000013030",
773 "0000000000000000000000000000004411",
774 "0000000000000000000000000000002650",
775 "0000000000000000000000000000001767",
776 "0000000000000000000000000000001347",
777 "0000000000000000000000000000001015",
778 "00000000000000000000843",
779 "00000000000000000000707",
780 "00000000000000000000601",
781 "00000000000000000000527",
782 "0000000000000000000047A",
783 "000000000000000000003F7",
784 "0000000000000000000038C",
785 "00000000000000000000327",
786 "000000000000000000002F8",
787 "000000000000000000002AF",
788 "00000000000000000000267",
789 "00000000000000000000223",
790 "000000000000000000001L3",
791 "000000000000000000001I7",
792 "000000000000000000001FF",
793 "000000000000000000001D1",
794 "000000000000000000001AG",
795 "00000000000000000000187",
796 "00000000000000000000160",
797 "0000000000000000000013P",
798 "0000000000000000000011N",
799 "00000000000000000000VN",
800 "00000000000000000000UP",
801 "00000000000000000000TT",
802 "00000000000000000000T0",
803 "00000000000000000000S7"};
804
805 for (std::size_t i = 0; i < int_leading_zeros_test.size(); ++i) {
806 auto const &f = int_leading_zeros_test[i];
807 int result;
808 auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result,
809 int(i + 2));
810 if (answer.ec != std::errc()) {
811 std::cerr << "could not convert to int for input: \"" << f << "\""
812 << std::endl;
813 return EXIT_FAILURE;
814 } else if (result != 1015) {
815 std::cerr << "result " << f
816 << " did not match with expected int: " << 1015 << std::endl;
817 return EXIT_FAILURE;
818 }
819 }
820 // issue 235
821 {
822 std::vector<char> s = {'0'};
823 s.shrink_to_fit();
824 int foo;
825 auto answer = fast_float::from_chars(s.data(), s.data() + s.size(), foo);
826 if (answer.ec != std::errc()) {
827 std::cerr << "could not convert to int for input: '0'" << std::endl;
828 return EXIT_FAILURE;
829 } else if (foo != 0) {
830 std::cerr << "expected zero: " << foo << std::endl;
831 return EXIT_FAILURE;
832 }
833 }
834 // dont parse UTF-16 code units of emojis as int if low byte is ascii digit
835 {
836 const std::u16string emojis[] = {
837 u"ℹ", u"ℹ️", u"☸", u"☸️", u"☹", u"☹️", u"✳", u"✳️",
838 u"✴", u"✴️", u"⤴", u"⤴️", u"⤵", u"⤵️", u"〰", u"〰️",
839 };
840 bool failed = false;
841 auto array_size = sizeof(emojis) / sizeof(emojis[0]);
842 for (size_t i = 0; i < array_size; i++) {
843 auto e = emojis[i];
844 int foo;
845 auto answer = fast_float::from_chars(e.data(), e.data() + e.size(), foo);
846 if (answer.ec == std::errc()) {
847 failed = true;
848 std::cerr << "Incorrectly parsed emoji #" << i << " as integer " << foo
849 << "." << std::endl;
850 }
851 }
852
853 if (failed) {
854 return EXIT_FAILURE;
855 }
856 }
857 // dont parse UTF-32 code points of emojis as int if low byte is ascii digit
858 {
859 const std::u32string emojis[] = {
860 U"ℹ",
861 U"ℹ️",
862 U"☸",
863 U"☸️",
864 U"☹",
865 U"☹️",
866 U"✳",
867 U"✳️",
868 U"✴",
869 U"✴️",
870 U"⤴",
871 U"⤴️",
872 U"⤵",
873 U"⤵️",
874 U"〰",
875 U"〰️",
876 U"🈲",
877 U"🈳",
878 U"🈴",
879 U"🈵",
880 U"🈶",
881 U"🈷",
882 U"🈷️",
883 U"🈸",
884 U"🈹",
885 U"🌰",
886 U"🌱",
887 U"🌲",
888 U"🌳",
889 U"🌴",
890 U"🌵",
891 U"🌶",
892 U"🌶️",
893 U"🌷",
894 U"🌸",
895 U"🌹",
896 U"🐰",
897 U"🐱",
898 U"🐲",
899 U"🐳",
900 U"🐴",
901 U"🐵",
902 U"🐶",
903 U"🐷",
904 U"🐸",
905 U"🐹",
906 U"🔰",
907 U"🔱",
908 U"🔲",
909 U"🔳",
910 U"🔴",
911 U"🔵",
912 U"🔶",
913 U"🔷",
914 U"🔸",
915 U"🔹",
916 U"😰",
917 U"😱",
918 U"😲",
919 U"😳",
920 U"😴",
921 U"😵",
922 U"😵‍💫",
923 U"😶",
924 U"😶‍🌫",
925 U"😶‍🌫️",
926 U"😷",
927 U"😸",
928 U"😹",
929 U"🤰",
930 U"🤰🏻",
931 U"🤰🏼",
932 U"🤰🏽",
933 U"🤰🏾",
934 U"🤰🏿",
935 U"🤱",
936 U"🤱🏻",
937 U"🤱🏼",
938 U"🤱🏽",
939 U"🤱🏾",
940 U"🤱🏿",
941 U"🤲",
942 U"🤲🏻",
943 U"🤲🏼",
944 U"🤲🏽",
945 U"🤲🏾",
946 U"🤲🏿",
947 U"🤳",
948 U"🤳🏻",
949 U"🤳🏼",
950 U"🤳🏽",
951 U"🤳🏾",
952 U"🤳🏿",
953 U"🤴",
954 U"🤴🏻",
955 U"🤴🏼",
956 U"🤴🏽",
957 U"🤴🏾",
958 U"🤴🏿",
959 U"🤵",
960 U"🤵‍♀",
961 U"🤵‍♀️",
962 U"🤵‍♂",
963 U"🤵‍♂️",
964 U"🤵🏻",
965 U"🤵🏻‍♀",
966 U"🤵🏻‍♀️",
967 U"🤵🏻‍♂",
968 U"🤵🏻‍♂️",
969 U"🤵🏼",
970 U"🤵🏼‍♀",
971 U"🤵🏼‍♀️",
972 U"🤵🏼‍♂",
973 U"🤵🏼‍♂️",
974 U"🤵🏽",
975 U"🤵🏽‍♀",
976 U"🤵🏽‍♀️",
977 U"🤵🏽‍♂",
978 U"🤵🏽‍♂️",
979 U"🤵🏾",
980 U"🤵🏾‍♀",
981 U"🤵🏾‍♀️",
982 U"🤵🏾‍♂",
983 U"🤵🏾‍♂️",
984 U"🤵🏿",
985 U"🤵🏿‍♀",
986 U"🤵🏿‍♀️",
987 U"🤵🏿‍♂",
988 U"🤵🏿‍♂️",
989 U"🤶",
990 U"🤶🏻",
991 U"🤶🏼",
992 U"🤶🏽",
993 U"🤶🏾",
994 U"🤶🏿",
995 U"🤷",
996 U"🤷‍♀",
997 U"🤷‍♀️",
998 U"🤷‍♂",
999 U"🤷‍♂️",
1000 U"🤷🏻",
1001 U"🤷🏻‍♀",
1002 U"🤷🏻‍♀️",
1003 U"🤷🏻‍♂",
1004 U"🤷🏻‍♂️",
1005 U"🤷🏼",
1006 U"🤷🏼‍♀",
1007 U"🤷🏼‍♀️",
1008 U"🤷🏼‍♂",
1009 U"🤷🏼‍♂️",
1010 U"🤷🏽",
1011 U"🤷🏽‍♀",
1012 U"🤷🏽‍♀️",
1013 U"🤷🏽‍♂",
1014 U"🤷🏽‍♂️",
1015 U"🤷🏾",
1016 U"🤷🏾‍♀",
1017 U"🤷🏾‍♀️",
1018 U"🤷🏾‍♂",
1019 U"🤷🏾‍♂️",
1020 U"🤷🏿",
1021 U"🤷🏿‍♀",
1022 U"🤷🏿‍♀️",
1023 U"🤷🏿‍♂",
1024 U"🤷🏿‍♂️",
1025 U"🤸",
1026 U"🤸‍♀",
1027 U"🤸‍♀️",
1028 U"🤸‍♂",
1029 U"🤸‍♂️",
1030 U"🤸🏻",
1031 U"🤸🏻‍♀",
1032 U"🤸🏻‍♀️",
1033 U"🤸🏻‍♂",
1034 U"🤸🏻‍♂️",
1035 U"🤸🏼",
1036 U"🤸🏼‍♀",
1037 U"🤸🏼‍♀️",
1038 U"🤸🏼‍♂",
1039 U"🤸🏼‍♂️",
1040 U"🤸🏽",
1041 U"🤸🏽‍♀",
1042 U"🤸🏽‍♀️",
1043 U"🤸🏽‍♂",
1044 U"🤸🏽‍♂️",
1045 U"🤸🏾",
1046 U"🤸🏾‍♀",
1047 U"🤸🏾‍♀️",
1048 U"🤸🏾‍♂",
1049 U"🤸🏾‍♂️",
1050 U"🤸🏿",
1051 U"🤸🏿‍♀",
1052 U"🤸🏿‍♀️",
1053 U"🤸🏿‍♂",
1054 U"🤸🏿‍♂️",
1055 U"🤹",
1056 U"🤹‍♀",
1057 U"🤹‍♀️",
1058 U"🤹‍♂",
1059 U"🤹‍♂️",
1060 U"🤹🏻",
1061 U"🤹🏻‍♀",
1062 U"🤹🏻‍♀️",
1063 U"🤹🏻‍♂",
1064 U"🤹🏻‍♂️",
1065 U"🤹🏼",
1066 U"🤹🏼‍♀",
1067 U"🤹🏼‍♀️",
1068 U"🤹🏼‍♂",
1069 U"🤹🏼‍♂️",
1070 U"🤹🏽",
1071 U"🤹🏽‍♀",
1072 U"🤹🏽‍♀️",
1073 U"🤹🏽‍♂",
1074 U"🤹🏽‍♂️",
1075 U"🤹🏾",
1076 U"🤹🏾‍♀",
1077 U"🤹🏾‍♀️",
1078 U"🤹🏾‍♂",
1079 U"🤹🏾‍♂️",
1080 U"🤹🏿",
1081 U"🤹🏿‍♀",
1082 U"🤹🏿‍♀️",
1083 U"🤹🏿‍♂",
1084 U"🤹🏿‍♂️",
1085 };
1086 bool failed = false;
1087 auto array_size = sizeof(emojis) / sizeof(emojis[0]);
1088 for (size_t i = 0; i < array_size; i++) {
1089 auto e = emojis[i];
1090 int foo;
1091 auto answer = fast_float::from_chars(e.data(), e.data() + e.size(), foo);
1092 if (answer.ec == std::errc()) {
1093 failed = true;
1094 std::cerr << "Incorrectly parsed emoji #" << i << " as integer " << foo
1095 << "." << std::endl;
1096 }
1097 }
1098
1099 if (failed) {
1100 return EXIT_FAILURE;
1101 }
1102 }
1103
1104 return EXIT_SUCCESS;
1105 }
1106 #else
1107 #include <iostream>
1108 #include <cstdlib>
1109
1110 int main() {
1111 std::cerr << "The test requires C++17." << std::endl;
1112 return EXIT_SUCCESS;
1113 }
1114 #endif