]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/numpunct/members/pod/2.cc
locale_facets.tcc: Use function object for __use_cache instead of template function.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / numpunct / members / pod / 2.cc
1 // 2003-07-09 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 #include <locale>
22 #include <sstream>
23 #include <ostream>
24 #include <stdexcept>
25 #include <ext/pod_char_traits.h>
26 #include <testsuite_hooks.h>
27
28 typedef unsigned short value_type;
29 typedef unsigned int int_type;
30 typedef __gnu_cxx::character<value_type, int_type> podchar_type;
31
32 // Member specializations for the existing facet classes.
33 // NB: This isn't especially portable. Perhaps a better way would be
34 // to just specialize all of numpunct and ctype.
35 namespace std
36 {
37 template<>
38 bool
39 ctype<podchar_type>::
40 do_is(mask __m, char_type __c) const { return true; }
41
42 template<>
43 const podchar_type*
44 ctype<podchar_type>::
45 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const
46 { return __lo; }
47
48 template<>
49 const podchar_type*
50 ctype<podchar_type>::
51 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
52 { return __lo; }
53
54 template<>
55 const podchar_type*
56 ctype<podchar_type>::
57 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
58 { return __lo; }
59
60 template<>
61 podchar_type
62 ctype<podchar_type>::
63 do_toupper(char_type __c) const
64 { return __c; }
65
66 template<>
67 const podchar_type*
68 ctype<podchar_type>::
69 do_toupper(char_type* __lo, const char_type* __hi) const
70 { return __hi; }
71
72 template<>
73 podchar_type
74 ctype<podchar_type>::
75 do_tolower(char_type __c) const
76 { return __c; }
77
78 template<>
79 const podchar_type*
80 ctype<podchar_type>::
81 do_tolower(char_type* __lo, const char_type* __hi) const
82 { return __hi; }
83
84 template<>
85 podchar_type
86 ctype<podchar_type>::
87 do_widen(char __c) const
88 {
89 char_type ret = { value_type(__c) };
90 return ret;
91 }
92
93 template<>
94 const char*
95 ctype<podchar_type>::
96 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
97 {
98 while (__lo < __hi)
99 {
100 *__dest = this->do_widen(*__lo);
101 ++__lo;
102 ++__dest;
103 }
104 return __hi;
105 }
106
107 template<>
108 char
109 ctype<podchar_type>::
110 do_narrow(char_type __wc, char) const
111 { return static_cast<char>(__wc.value); }
112
113 template<>
114 const podchar_type*
115 ctype<podchar_type>::
116 do_narrow(const podchar_type* __lo, const podchar_type* __hi,
117 char __dfault, char* __dest) const
118 {
119 while (__lo < __hi)
120 {
121 *__dest = this->do_narrow(*__lo, char());
122 ++__lo;
123 ++__dest;
124 }
125 return __hi;
126 }
127
128 template<>
129 ctype<podchar_type>::~ctype() { }
130
131 template<>
132 void
133 numpunct<podchar_type>::_M_initialize_numpunct(__c_locale __cloc)
134 {
135 if (!_M_data)
136 _M_data = new __numpunct_cache<podchar_type>;
137
138 _M_data->_M_grouping = "";
139 _M_data->_M_use_grouping = false;
140
141 _M_data->_M_decimal_point.value = value_type('.');
142 _M_data->_M_thousands_sep.value = value_type(',');
143
144 for (size_t i = 0; i < __num_base::_S_oend; ++i)
145 {
146 value_type v = __num_base::_S_atoms_out[i];
147 _M_data->_M_atoms_out[i].value = v;
148 }
149 _M_data->_M_atoms_out[__num_base::_S_oend] = podchar_type();
150
151 for (size_t i = 0; i < __num_base::_S_iend; ++i)
152 _M_data->_M_atoms_in[i].value = value_type(__num_base::_S_atoms_in[i]);
153 _M_data->_M_atoms_in[__num_base::_S_iend] = podchar_type();
154
155 // "true"
156 podchar_type* __truename = new podchar_type[4 + 1];
157 __truename[0].value = value_type('t');
158 __truename[1].value = value_type('r');
159 __truename[2].value = value_type('u');
160 __truename[3].value = value_type('e');
161 __truename[4] = podchar_type();
162 _M_data->_M_truename = __truename;
163
164 // "false"
165 podchar_type* __falsename = new podchar_type[5 + 1];
166 __falsename[0].value = value_type('f');
167 __falsename[1].value = value_type('a');
168 __falsename[2].value = value_type('l');
169 __falsename[3].value = value_type('s');
170 __falsename[4].value = value_type('e');
171 __falsename[5] = podchar_type();
172 _M_data->_M_falsename = __falsename;
173 }
174
175 template<>
176 numpunct<podchar_type>::~numpunct()
177 { delete _M_data; }
178 }
179
180 // Check for numpunct and ctype dependencies. Make sure that numpunct
181 // can be created without ctype.
182 void test01()
183 {
184 using namespace std;
185 typedef numpunct<podchar_type>::string_type string_type;
186 typedef basic_ostringstream<podchar_type> ostream_type;
187
188 bool test = true;
189
190 // Test formatted output.
191 ostream_type os;
192 const locale loc = locale::classic();
193 os.imbue(loc);
194 os.setf(ios_base::boolalpha);
195 os.exceptions(ios_base::badbit);
196
197 // 1: fail, no num_put.
198 try
199 {
200 // Calls to num_put.put will fail, as there's no num_put facet.
201 os << true;
202 test = false;
203 }
204 catch(const bad_cast& obj)
205 { }
206 catch(...)
207 { test = false; }
208 VERIFY( test );
209
210 // 2: fail, no ctype
211 const locale loc2(loc, new num_put<podchar_type>);
212 os.clear();
213 os.imbue(loc2);
214 try
215 {
216 // Calls to ctype.widen will fail, as there's no ctype facet.
217 os << true;
218 test = false;
219 }
220 catch(const bad_cast& obj)
221 { }
222 catch(...)
223 { test = false; }
224 VERIFY( test );
225
226 // 3: fail, no numpunct
227 const locale loc3(loc, new ctype<podchar_type>);
228 os.clear();
229 os.imbue(loc3);
230 try
231 {
232 // Formatted output fails as no numpunct.
233 os << true;
234 test = false;
235 }
236 catch(const bad_cast& obj)
237 { }
238 catch(...)
239 { test = false; }
240 VERIFY( test );
241
242 // 4: works.
243 const locale loc4(loc3, new numpunct<podchar_type>);
244 os.clear();
245 os.imbue(loc4);
246 try
247 {
248 os << long(500);
249 string_type s = os.str();
250 VERIFY( s.length() == 3 );
251
252 VERIFY( os.narrow(s[0], char()) == '5' );
253 VERIFY( os.narrow(s[1], char()) == '0' );
254 VERIFY( os.narrow(s[2], char()) == '0' );
255 }
256 catch(const bad_cast& obj)
257 { test = false; }
258 catch(...)
259 { test = false; }
260 VERIFY( test );
261 }
262
263 int main()
264 {
265 test01();
266 return 0;
267 }