Debian Codegraph @

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

raw
1 // { dg-do run { target c++11 } }
2
3 // 2008-06-15 Paolo Carlini <[email protected]>
4
5 // Copyright (C) 2008-2025 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 // 21.4 Numeric Conversions [string.conversions]
23
24 #include <string>
25 #include <limits>
26 #include <stdexcept>
27 #include <testsuite_hooks.h>
28
29 void
30 test01()
31 {
32 bool test = false;
33 using namespace std;
34
35 try
36 {
37 string one;
38 stoi(one);
39 }
40 catch(const std::invalid_argument&)
41 {
42 test = true;
43 }
44 catch(...)
45 {
46 }
47 VERIFY( test );
48
49 test = false;
50 try
51 {
52 string one("a");
53 stoi(one);
54 }
55 catch(const std::invalid_argument&)
56 {
57 test = true;
58 }
59 catch(...)
60 {
61 }
62 VERIFY( test );
63
64 int i1 = 0;
65 try
66 {
67 string one("a");
68 i1 = stoi(one, 0, 16);
69 }
70 catch(...)
71 {
72 test = false;
73 }
74 VERIFY( test );
75 VERIFY( i1 == 10 );
76
77 size_t idx1 = 0;
78 try
79 {
80 string one("78");
81 i1 = stoi(one, &idx1, 8);
82 }
83 catch(...)
84 {
85 test = false;
86 }
87 VERIFY( test );
88 VERIFY( i1 == 7 );
89 VERIFY( idx1 = 1 );
90
91 try
92 {
93 string one("10112");
94 i1 = stoi(one, &idx1, 2);
95 }
96 catch(...)
97 {
98 test = false;
99 }
100 VERIFY( test );
101 VERIFY( i1 == 11 );
102 VERIFY( idx1 == 4 );
103
104 try
105 {
106 string one("0XE");
107 i1 = stoi(one, &idx1, 0);
108 }
109 catch(...)
110 {
111 test = false;
112 }
113 VERIFY( test );
114 VERIFY( i1 == 14 );
115 VERIFY( idx1 == 3 );
116
117 test = false;
118 try
119 {
120 string one(1000, '9');
121 i1 = stoi(one);
122 }
123 catch(const std::out_of_range&)
124 {
125 test = true;
126 }
127 catch(...)
128 {
129 }
130 VERIFY( test );
131 VERIFY( i1 == 14 );
132
133 try
134 {
135 i1 = numeric_limits<int>::max();
136 string one(to_string((long long)i1));
137 i1 = stoi(one);
138 }
139 catch(...)
140 {
141 test = false;
142 }
143 VERIFY( test );
144 VERIFY( i1 == numeric_limits<int>::max() );
145
146 try
147 {
148 i1 = numeric_limits<int>::min();
149 string one(to_string((long long)i1));
150 i1 = stoi(one);
151 }
152 catch(...)
153 {
154 test = false;
155 }
156 VERIFY( test );
157 VERIFY( i1 == numeric_limits<int>::min() );
158
159 test = false;
160 i1 = 1;
161 try
162 {
163 long long ll0 = numeric_limits<int>::max();
164 ++ll0;
165 string one(to_string(ll0));
166 i1 = stoi(one);
167 }
168 catch(const std::out_of_range&)
169 {
170 test = true;
171 }
172 catch(...)
173 {
174 }
175 VERIFY( test );
176 VERIFY( i1 == 1 );
177
178 test = false;
179 try
180 {
181 long long ll1 = numeric_limits<int>::min();
182 --ll1;
183 string one(to_string(ll1));
184 i1 = stoi(one);
185 }
186 catch(const std::out_of_range&)
187 {
188 test = true;
189 }
190 catch(...)
191 {
192 }
193 VERIFY( test );
194 VERIFY( i1 == 1 );
195 }
196
197 int main()
198 {
199 test01();
200 return 0;
201 }