]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/wchar_t/stoi.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / numeric_conversions / wchar_t / stoi.cc
1 // { dg-options "-std=gnu++0x" }
2 // 2008-06-15 Paolo Carlini <paolo.carlini@oracle.com>
3
4 // Copyright (C) 2008, 2009 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 // 21.4 Numeric Conversions [string.conversions]
22
23 #include <string>
24 #include <limits>
25 #include <stdexcept>
26 #include <testsuite_hooks.h>
27
28 void
29 test01()
30 {
31 #ifdef _GLIBCXX_USE_C99
32
33 bool test __attribute__((unused)) = false;
34 using namespace std;
35
36 try
37 {
38 wstring one;
39 stoi(one);
40 }
41 catch(std::invalid_argument)
42 {
43 test = true;
44 }
45 catch(...)
46 {
47 }
48 VERIFY( test );
49
50 test = false;
51 try
52 {
53 wstring one(L"a");
54 stoi(one);
55 }
56 catch(std::invalid_argument)
57 {
58 test = true;
59 }
60 catch(...)
61 {
62 }
63 VERIFY( test );
64
65 int i1 = 0;
66 try
67 {
68 wstring one(L"a");
69 i1 = stoi(one, 0, 16);
70 }
71 catch(...)
72 {
73 test = false;
74 }
75 VERIFY( test );
76 VERIFY( i1 == 10 );
77
78 size_t idx1 = 0;
79 try
80 {
81 wstring one(L"78");
82 i1 = stoi(one, &idx1, 8);
83 }
84 catch(...)
85 {
86 test = false;
87 }
88 VERIFY( test );
89 VERIFY( i1 == 7 );
90 VERIFY( idx1 = 1 );
91
92 try
93 {
94 wstring one(L"10112");
95 i1 = stoi(one, &idx1, 2);
96 }
97 catch(...)
98 {
99 test = false;
100 }
101 VERIFY( test );
102 VERIFY( i1 == 11 );
103 VERIFY( idx1 == 4 );
104
105 try
106 {
107 wstring one(L"0XE");
108 i1 = stoi(one, &idx1, 0);
109 }
110 catch(...)
111 {
112 test = false;
113 }
114 VERIFY( test );
115 VERIFY( i1 == 14 );
116 VERIFY( idx1 == 3 );
117
118 test = false;
119 try
120 {
121 wstring one(1000, L'9');
122 i1 = stoi(one);
123 }
124 catch(std::out_of_range)
125 {
126 test = true;
127 }
128 catch(...)
129 {
130 }
131 VERIFY( test );
132 VERIFY( i1 == 14 );
133
134 try
135 {
136 i1 = numeric_limits<int>::max();
137 wstring one(to_wstring((long long)i1));
138 i1 = stoi(one);
139 }
140 catch(...)
141 {
142 test = false;
143 }
144 VERIFY( test );
145 VERIFY( i1 == numeric_limits<int>::max() );
146
147 try
148 {
149 i1 = numeric_limits<int>::min();
150 wstring one(to_wstring((long long)i1));
151 i1 = stoi(one);
152 }
153 catch(...)
154 {
155 test = false;
156 }
157 VERIFY( test );
158 VERIFY( i1 == numeric_limits<int>::min() );
159
160 test = false;
161 i1 = 1;
162 try
163 {
164 long long ll0 = numeric_limits<int>::max();
165 ++ll0;
166 wstring one(to_wstring(ll0));
167 i1 = stoi(one);
168 }
169 catch(std::out_of_range)
170 {
171 test = true;
172 }
173 catch(...)
174 {
175 }
176 VERIFY( test );
177 VERIFY( i1 == 1 );
178
179 test = false;
180 try
181 {
182 long long ll1 = numeric_limits<int>::min();
183 --ll1;
184 wstring one(to_wstring(ll1));
185 i1 = stoi(one);
186 }
187 catch(std::out_of_range)
188 {
189 test = true;
190 }
191 catch(...)
192 {
193 }
194 VERIFY( test );
195 VERIFY( i1 == 1 );
196
197 #endif
198 }
199
200 int main()
201 {
202 test01();
203 return 0;
204 }