]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | // Copyright (C) 2017-2020 Free Software Foundation, Inc. |
804b7cc4 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
c104e8f1 JW |
18 | // <charconv> is supported in C++14 as a GNU extension |
19 | // { dg-do run { target c++14 } } | |
804b7cc4 JW |
20 | |
21 | #include <charconv> | |
c104e8f1 | 22 | #include <string> |
804b7cc4 JW |
23 | |
24 | template<typename I> | |
25 | bool | |
c104e8f1 | 26 | check_from_chars(I expected, std::string s, int base = 0, char term = '\0') |
804b7cc4 | 27 | { |
c104e8f1 JW |
28 | const char* begin = s.data(); |
29 | const char* end = s.data() + s.length(); | |
804b7cc4 JW |
30 | I val; |
31 | std::from_chars_result r = base == 0 | |
c104e8f1 JW |
32 | ? std::from_chars(begin, end, val) |
33 | : std::from_chars(begin, end, val, base); | |
34 | return r.ec == std::errc{} && (r.ptr == end || *r.ptr == term) && val == expected; | |
804b7cc4 JW |
35 | } |
36 | ||
37 | #include <climits> | |
38 | #include <testsuite_hooks.h> | |
39 | ||
40 | void | |
41 | test01() | |
42 | { | |
43 | // Using base 10 | |
44 | VERIFY( check_from_chars(123, "123") ); | |
45 | VERIFY( check_from_chars(-123, "-123") ); | |
46 | VERIFY( check_from_chars(123, "123a", 10, 'a') ); | |
47 | VERIFY( check_from_chars(123, "0000000000000000000000000000123") ); | |
48 | VERIFY( check_from_chars(123, "0000000000000000000000000000123a", 10, 'a') ); | |
49 | } | |
50 | ||
51 | void | |
52 | test02() | |
53 | { | |
54 | // "0x" parsed as "0" not as hex prefix: | |
55 | VERIFY( check_from_chars(0, "0x1", 10, 'x') ); | |
56 | VERIFY( check_from_chars(0, "0X1", 10, 'X') ); | |
57 | VERIFY( check_from_chars(0, "0x1", 16, 'x') ); | |
58 | VERIFY( check_from_chars(0, "0X1", 16, 'X') ); | |
59 | ||
60 | VERIFY( check_from_chars(1155, "xx", 34) ); | |
61 | VERIFY( check_from_chars(1155, "XX", 34) ); | |
62 | VERIFY( check_from_chars(1155, "Xx", 34) ); | |
63 | VERIFY( check_from_chars(1224, "yy", 35) ); | |
64 | VERIFY( check_from_chars(1224, "YY", 35) ); | |
65 | VERIFY( check_from_chars(1224, "yY", 35) ); | |
66 | VERIFY( check_from_chars(1295, "zz", 36) ); | |
67 | VERIFY( check_from_chars(1295, "ZZ", 36) ); | |
68 | VERIFY( check_from_chars(1295, "Zz", 36) ); | |
69 | ||
70 | // Parsing stops at first invalid digit for the given base: | |
71 | VERIFY( check_from_chars(1, "01234", 2, '2') ); | |
72 | VERIFY( check_from_chars(27, "1234", 4, '4') ); | |
73 | VERIFY( check_from_chars(1155, "xxy", 34, 'y') ); | |
74 | VERIFY( check_from_chars(1224, "yyz", 35, 'z') ); | |
75 | } | |
76 | ||
77 | int | |
78 | main() | |
79 | { | |
80 | test01(); | |
81 | test02(); | |
82 | } |