Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/to_string_int.cc

raw
1 // { dg-options "-DSIMULATOR_TEST" { target simulator } }
2 // { dg-do run { target c++11 } }
3
4 // Copyright (C) 2019-2025 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.
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
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
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
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
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
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>
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 }
108
109 #ifdef SIMULATOR_TEST
110 const int width = 3;
111 #else
112 const int width = 16;
113 #endif
114
115 template<typename T>
116 void check_values()
117 {
118 #ifdef SIMULATOR_TEST
119 check_value((T)-1);
120 check_value((T)0);
121 check_value((T)+1);
122 #endif
123
124 for (auto v : values)
125 {
126 for (int i = -width; i < +width; ++i)
127 {
128 const T val = (T)v + i;
129 check_value(val);
130 }
131
132 if (std::numeric_limits<T>::digits > 32)
133 {
134 for (auto v2 : values)
135 {
136 for (int i = -width; i < +width; ++i)
137 {
138 typename std::make_unsigned<T>::type hi = v2;
139 hi += i;
140 hi <<= 32;
141 const T val = T(hi) | v;
142 check_value(val);
143 }
144 }
145 }
146 }
147 }
148
149 void test02()
150 {
151 check_values<int>();
152 check_values<unsigned int>();
153 check_values<long>();
154 check_values<unsigned long>();
155 check_values<long long>();
156 check_values<unsigned long long>();
157 }
158
159 int main()
160 {
161 test02();
162 }