1 // { dg-options "-DSIMULATOR_TEST" { target simulator } }
2 // { dg-do run { target c++11 } }
3
4 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // C++11 21.5 Numeric Conversions [string.conversions]
22
23 #include <string>
24 #include <limits>
25 #include <cstdint>
26 #include <cstdio>
27 #include <testsuite_hooks.h>
28
29 namespace test
30 {
31 static char buf[100];
32
33 // Canonical version of std::to_string(int) as specified in the standard.
to_string(int val)34 static std::string to_string(int val)
35 {
36 std::string str;
37 const int len = std::snprintf(buf, sizeof(buf), "%d", val);
38 VERIFY( len < (int)sizeof(buf) );
39 return std::string(buf, len);
40 }
41
to_string(unsigned int val)42 static std::string to_string(unsigned int val)
43 {
44 std::string str;
45 const int len = std::snprintf(buf, sizeof(buf), "%u", val);
46 VERIFY( len < (int)sizeof(buf) );
47 return std::string(buf, len);
48 }
49
to_string(long val)50 static std::string to_string(long val)
51 {
52 std::string str;
53 const int len = std::snprintf(buf, sizeof(buf), "%ld", val);
54 VERIFY( len < (int)sizeof(buf) );
55 return std::string(buf, len);
56 }
57
to_string(unsigned long val)58 static std::string to_string(unsigned long val)
59 {
60 std::string str;
61 const int len = std::snprintf(buf, sizeof(buf), "%lu", val);
62 VERIFY( len < (int)sizeof(buf) );
63 return std::string(buf, len);
64 }
65
to_string(long long val)66 static std::string to_string(long long val)
67 {
68 std::string str;
69 const int len = std::snprintf(buf, sizeof(buf), "%lld", val);
70 VERIFY( len < (int)sizeof(buf) );
71 return std::string(buf, len);
72 }
73
to_string(unsigned long long val)74 static std::string to_string(unsigned long long val)
75 {
76 std::string str;
77 const int len = std::snprintf(buf, sizeof(buf), "%llu", val);
78 VERIFY( len < (int)sizeof(buf) );
79 return std::string(buf, len);
80 }
81
82 } // namespace test
83
84 const std::uint_least32_t values[] = {
85 0x10, 0x30, 0x50, 0x80, 0xc0,
86 0x100, 0x180, 0x1c0, 0x200, 0x400, 0x800, 0xc00,
87 0x1000, 0x1800, 0x2000, 0x4000, 0x8000, 0xc000,
88 0x10000, 0x10101, 0x80000, 0x80706, 0xc0000, 0xccccc,
89 0x100000, 0x101010, 0x800000, 0x807060, 0xc0fefe, 0xc1d2e3f,
90 0x1000000, 0x1001000, 0x1008000, 0x1010000, 0x1080000, 0x1100000, 0x1234567,
91 0x10000000, 0x10101010, 0x12345678, 0x80000010, 0x87654321, 0xaaaaaaaa,
92 0xf0000000, 0xf0101010, 0xf0f00000, 0xf0f0f0f0, 0xf0ff0ff0, 0xff0ff00f,
93 0xffff0000, 0xffff00f0, 0xffff0ff0, 0xffffff00
94 };
95
96 const std::size_t empty_string_capacity = std::string().capacity();
97
98 #include <set>
99
100 template<typename T>
check_value(T val)101 void check_value(T val)
102 {
103 const std::string s = std::to_string(val);
104 const std::string expected = test::to_string(val);
105 VERIFY( s == expected );
106 VERIFY( s[s.size()] == '\0' ); // null-terminator not overwritten!
107 if (s.size() > empty_string_capacity)
108 VERIFY( s.capacity() == s.size() ); // GNU-specific guarantee
109 }
110
111 #ifdef SIMULATOR_TEST
112 const int width = 3;
113 #else
114 const int width = 16;
115 #endif
116
117 template<typename T>
check_values()118 void check_values()
119 {
120 #ifdef SIMULATOR_TEST
121 check_value((T)-1);
122 check_value((T)0);
123 check_value((T)+1);
124 #endif
125
126 for (auto v : values)
127 {
128 for (int i = -width; i < +width; ++i)
129 {
130 const T val = (T)v + i;
131 check_value(val);
132 }
133
134 if (std::numeric_limits<T>::digits > 32)
135 {
136 for (auto v2 : values)
137 {
138 for (int i = -width; i < +width; ++i)
139 {
140 typename std::make_unsigned<T>::type hi = v2;
141 hi += i;
142 hi <<= 32;
143 const T val = T(hi) | v;
144 check_value(val);
145 }
146 }
147 }
148 }
149 }
150
test02()151 void test02()
152 {
153 check_values<int>();
154 check_values<unsigned int>();
155 check_values<long>();
156 check_values<unsigned long>();
157 check_values<long long>();
158 check_values<unsigned long long>();
159 }
160
main()161 int main()
162 {
163 test02();
164 }
165