1 // Copyright (C) 2017-2020 Free Software Foundation, Inc.
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
18 // <charconv> is supported in C++14 as a GNU extension
19 // { dg-do run { target c++14 } }
20
21 #include <charconv>
22 #include <string>
23
24 template<typename I>
25 bool
check_from_chars(I expected,std::string s,int base=0,char term='\\0')26 check_from_chars(I expected, std::string s, int base = 0, char term = '\0')
27 {
28 const char* begin = s.data();
29 const char* end = s.data() + s.length();
30 I val;
31 std::from_chars_result r = base == 0
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)
35 && val == expected;
36 }
37
38 #include <climits>
39 #include <testsuite_hooks.h>
40
41 void
test01()42 test01()
43 {
44 // Using base 10
45 VERIFY( check_from_chars(123, "123") );
46 VERIFY( check_from_chars(-123, "-123") );
47 VERIFY( check_from_chars(123, "123a", 10, 'a') );
48 VERIFY( check_from_chars(123, "0000000000000000000000000000123") );
49 VERIFY( check_from_chars(123, "0000000000000000000000000000123a", 10, 'a') );
50 }
51
52 void
test02()53 test02()
54 {
55 // "0x" parsed as "0" not as hex prefix:
56 for (int base = 2; base < 34; ++base)
57 {
58 VERIFY( check_from_chars(0, "0x1", base, 'x') );
59 VERIFY( check_from_chars(0, "0X1", base, 'X') );
60 }
61
62 VERIFY( check_from_chars(1123, "0x1", 34) );
63 VERIFY( check_from_chars(1123, "0X1", 34) );
64 VERIFY( check_from_chars(1156, "0x1", 35) );
65 VERIFY( check_from_chars(1156, "0X1", 35) );
66 VERIFY( check_from_chars(1189, "0x1", 36) );
67 VERIFY( check_from_chars(1189, "0X1", 36) );
68
69 VERIFY( check_from_chars(1155, "xx", 34) );
70 VERIFY( check_from_chars(1155, "XX", 34) );
71 VERIFY( check_from_chars(1155, "Xx", 34) );
72 VERIFY( check_from_chars(1224, "yy", 35) );
73 VERIFY( check_from_chars(1224, "YY", 35) );
74 VERIFY( check_from_chars(1224, "yY", 35) );
75 VERIFY( check_from_chars(1295, "zz", 36) );
76 VERIFY( check_from_chars(1295, "ZZ", 36) );
77 VERIFY( check_from_chars(1295, "Zz", 36) );
78
79 // Parsing stops at first invalid digit for the given base:
80 VERIFY( check_from_chars(1, "01234", 2, '2') );
81 VERIFY( check_from_chars(27, "1234", 4, '4') );
82 VERIFY( check_from_chars(1155, "xxy", 34, 'y') );
83 VERIFY( check_from_chars(1224, "yyz", 35, 'z') );
84 }
85
86 int
main()87 main()
88 {
89 test01();
90 test02();
91 }
92