* decl.c (grokdeclarator): Only check TREE_OVERFLOW on INTEGER_CST.
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_character.h
blobc4817d54f88701894b4c423ace0ec13f42068c47
1 // -*- C++ -*-
3 // Testing character type and state type with char_traits and codecvt
4 // specializations for the C++ library testsuite.
5 //
6 // Copyright (C) 2003, 2005 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 2, or (at your option)
12 // any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING. If not, write to the Free
21 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // USA.
24 // As a special exception, you may use this file as part of a free software
25 // library without restriction. Specifically, if other files instantiate
26 // templates or use macros or inline functions from this file, or you compile
27 // this file and link it with other files to produce an executable, this
28 // file does not by itself cause the resulting executable to be covered by
29 // the GNU General Public License. This exception does not however
30 // invalidate any other reasons why the executable file might be covered by
31 // the GNU General Public License.
33 #ifndef _GLIBCXX_TESTSUITE_CHARACTER_H
34 #define _GLIBCXX_TESTSUITE_CHARACTER_H
36 #include <climits>
37 #include <string> // for char_traits
38 #include <locale> // for codecvt
39 #include <ext/pod_char_traits.h>
41 namespace __gnu_test
43 struct pod_int
45 int value;
48 inline bool
49 operator==(const pod_int& lhs, const pod_int& rhs)
50 { return lhs.value == rhs.value; }
52 inline bool
53 operator<(const pod_int& lhs, const pod_int& rhs)
54 { return lhs.value < rhs.value; }
56 struct pod_state
58 unsigned long value;
61 inline bool
62 operator==(const pod_state& lhs, const pod_state& rhs)
63 { return lhs.value == rhs.value; }
65 inline bool
66 operator<(const pod_state& lhs, const pod_state& rhs)
67 { return lhs.value < rhs.value; }
69 // Alternate character types.
70 using __gnu_cxx::character;
71 typedef character<unsigned char, pod_int, pod_state> pod_char;
72 typedef character<unsigned char, unsigned int, pod_state> pod_uchar;
73 typedef character<unsigned short, unsigned int> pod_ushort;
74 typedef character<unsigned int, unsigned long> pod_uint;
76 // Specializations.
77 // pod_char
78 template<>
79 template<typename V2>
80 inline pod_char::char_type
81 pod_char::char_type::from(const V2& v)
83 char_type ret = { static_cast<value_type>(v.value) };
84 return ret;
87 template<>
88 template<typename V2>
89 inline V2
90 pod_char::char_type::to(const char_type& c)
92 V2 ret = { c.value };
93 return ret;
96 // pod_uchar
97 template<>
98 template<typename V2>
99 inline pod_uchar::char_type
100 pod_uchar::char_type::from(const V2& v)
102 char_type ret;
103 ret.value = (v >> 5);
104 return ret;
107 template<>
108 template<typename V2>
109 inline V2
110 pod_uchar::char_type::to(const char_type& c)
111 { return static_cast<V2>(c.value << 5); }
112 }; // namespace __gnu_test
114 namespace std
116 // codecvt specialization
118 // The conversion performed by the specialization is not supposed to
119 // be useful, rather it has been designed to demonstrate the
120 // essential features of stateful conversions:
121 // * Number and value of bytes for each internal character depends on the
122 // state in addition to the character itself.
123 // * Unshift produces an unshift sequence and resets the state. On input
124 // the unshift sequence causes the state to be reset.
126 // The conversion for output is as follows:
127 // 1. Calculate the value tmp by xor-ing the state and the internal
128 // character
129 // 2. Split tmp into either two or three bytes depending on the value of
130 // state. Output those bytes.
131 // 3. tmp becomes the new value of state.
132 template<>
133 class codecvt<__gnu_test::pod_uchar, char, __gnu_test::pod_state>
134 : public __codecvt_abstract_base<__gnu_test::pod_uchar, char,
135 __gnu_test::pod_state>
137 public:
138 typedef codecvt_base::result result;
139 typedef __gnu_test::pod_uchar intern_type;
140 typedef char extern_type;
141 typedef __gnu_test::pod_state state_type;
142 typedef __codecvt_abstract_base<intern_type, extern_type, state_type>
143 base_type;
145 explicit codecvt(size_t refs = 0) : base_type(refs)
148 static locale::id id;
150 protected:
151 ~codecvt()
154 virtual result
155 do_out(state_type& state, const intern_type* from,
156 const intern_type* from_end, const intern_type*& from_next,
157 extern_type* to, extern_type* to_limit,
158 extern_type*& to_next) const
160 while (from < from_end && to < to_limit)
162 unsigned char tmp = (state.value ^ from->value);
163 if (state.value & 0x8)
165 if (to >= to_limit - 2)
166 break;
167 *to++ = (tmp & 0x7);
168 *to++ = ((tmp >> 3) & 0x7);
169 *to++ = ((tmp >> 6) & 0x3);
171 else
173 if (to >= to_limit - 1)
174 break;
175 *to++ = (tmp & 0xf);
176 *to++ = ((tmp >> 4) & 0xf);
178 state.value = tmp;
179 ++from;
182 from_next = from;
183 to_next = to;
184 return (from < from_end) ? partial : ok;
187 virtual result
188 do_in(state_type& state, const extern_type* from,
189 const extern_type* from_end, const extern_type*& from_next,
190 intern_type* to, intern_type* to_limit,
191 intern_type*& to_next) const
193 while (from < from_end && to < to_limit)
195 unsigned char c = *from;
196 if (c & 0xc0)
198 // Unshift sequence
199 state.value &= c;
200 ++from;
201 continue;
204 unsigned char tmp;
205 if (state.value & 0x8)
207 if (from >= from_end - 2)
208 break;
209 tmp = (*from++ & 0x7);
210 tmp |= ((*from++ << 3) & 0x38);
211 tmp |= ((*from++ << 6) & 0xc0);
213 else
215 if (from >= from_end - 1)
216 break;
217 tmp = (*from++ & 0xf);
218 tmp |= ((*from++ << 4) & 0xf0);
220 to->value = (tmp ^ state.value);
221 state.value = tmp;
222 ++to;
225 from_next = from;
226 to_next = to;
227 return (from < from_end) ? partial : ok;
230 virtual result
231 do_unshift(state_type& state, extern_type* to, extern_type* to_limit,
232 extern_type*& to_next) const
234 for (unsigned int i = 0; i < CHAR_BIT; ++i)
236 unsigned int mask = (1 << i);
237 if (state.value & mask)
239 if (to == to_limit)
241 to_next = to;
242 return partial;
245 state.value &= ~mask;
246 *to++ = static_cast<unsigned char>(~mask);
250 to_next = to;
251 return state.value == 0 ? ok : error;
254 virtual int
255 do_encoding() const throw()
256 { return -1; }
258 virtual bool
259 do_always_noconv() const throw()
260 { return false; }
262 virtual int
263 do_length(state_type& state, const extern_type* from,
264 const extern_type* end, size_t max) const
266 const extern_type* beg = from;
267 while (from < end && max)
269 unsigned char c = *from;
270 if (c & 0xc0)
272 // Unshift sequence
273 state.value &= c;
274 ++from;
275 continue;
278 unsigned char tmp;
279 if (state.value & 0x8)
281 if (from >= end - 2)
282 break;
283 tmp = (*from++ & 0x7);
284 tmp |= ((*from++ << 3) & 0x38);
285 tmp |= ((*from++ << 6) & 0xc0);
287 else
289 if (from >= end - 1)
290 break;
291 tmp = (*from++ & 0xf);
292 tmp |= ((*from++ << 4) & 0xf0);
294 state.value = tmp;
295 --max;
297 return from - beg;
300 // Maximum 8 bytes unshift sequence followed by max 3 bytes for
301 // one character.
302 virtual int
303 do_max_length() const throw()
304 { return 11; }
307 template<>
308 class ctype<__gnu_test::pod_uchar>
309 : public __ctype_abstract_base<__gnu_test::pod_uchar>
311 public:
312 typedef __gnu_test::pod_uchar char_type;
314 explicit ctype(size_t refs = 0)
315 : __ctype_abstract_base<__gnu_test::pod_uchar>(refs) { }
317 static locale::id id;
319 protected:
320 ~ctype()
323 virtual bool
324 do_is(mask m, char_type c) const
325 { return false; }
327 virtual const char_type*
328 do_is(const char_type* low, const char_type* high, mask* vec) const
330 fill_n(vec, high - low, mask());
331 return high;
334 virtual const char_type*
335 do_scan_is(mask m, const char_type* low, const char_type* high) const
336 { return high; }
338 virtual const char_type*
339 do_scan_not(mask m, const char_type* low, const char_type* high) const
340 { return low; }
342 virtual char_type
343 do_toupper(char_type c) const
344 { return c; }
346 virtual const char_type*
347 do_toupper(char_type* low, const char_type* high) const
348 { return high; }
350 virtual char_type
351 do_tolower(char_type c) const
352 { return c; }
354 virtual const char_type*
355 do_tolower(char_type* low, const char_type* high) const
356 { return high; }
358 virtual char_type
359 do_widen(char c) const
360 { return __gnu_test::pod_uchar::from<char>(c); }
362 virtual const char*
363 do_widen(const char* low, const char* high, char_type* dest) const
365 transform(low, high, dest, &__gnu_test::pod_uchar::from<char>);
366 return high;
369 virtual char
370 do_narrow(char_type, char dfault) const
371 { return dfault; }
373 virtual const char_type*
374 do_narrow(const char_type* low, const char_type* high,
375 char dfault, char* dest) const
377 fill_n(dest, high - low, dfault);
378 return high;
382 // numpunct specializations
383 template<>
384 class numpunct<__gnu_test::pod_uint>
385 : public locale::facet
387 public:
388 typedef __gnu_test::pod_uint char_type;
389 typedef basic_string<char_type> string_type;
391 static locale::id id;
393 explicit
394 numpunct(size_t refs = 0)
395 : locale::facet(refs)
396 { }
398 char_type
399 decimal_point() const
400 { return this->do_decimal_point(); }
402 char_type
403 thousands_sep() const
404 { return this->do_thousands_sep(); }
406 string
407 grouping() const
408 { return this->do_grouping(); }
410 string_type
411 truename() const
412 { return this->do_truename(); }
414 string_type
415 falsename() const
416 { return this->do_falsename(); }
418 protected:
419 ~numpunct()
420 { }
422 virtual char_type
423 do_decimal_point() const
424 { return char_type(); }
426 virtual char_type
427 do_thousands_sep() const
428 { return char_type(); }
430 virtual string
431 do_grouping() const
432 { return string(); }
434 virtual string_type
435 do_truename() const
436 { return string_type(); }
438 virtual string_type
439 do_falsename() const
440 { return string_type(); }
443 template<>
444 class moneypunct<__gnu_test::pod_uint>
445 : public locale::facet, public money_base
447 public:
448 typedef __gnu_test::pod_uint char_type;
449 typedef basic_string<char_type> string_type;
451 static locale::id id;
452 static const bool intl = false;
454 explicit
455 moneypunct(size_t refs = 0)
456 : locale::facet(refs)
459 char_type
460 decimal_point() const
461 { return this->do_decimal_point(); }
463 char_type
464 thousands_sep() const
465 { return this->do_thousands_sep(); }
467 string
468 grouping() const
469 { return this->do_grouping(); }
471 string_type
472 curr_symbol() const
473 { return this->do_curr_symbol(); }
475 string_type
476 positive_sign() const
477 { return this->do_positive_sign(); }
479 string_type
480 negative_sign() const
481 { return this->do_negative_sign(); }
484 frac_digits() const
485 { return this->do_frac_digits(); }
487 pattern
488 pos_format() const
489 { return this->do_pos_format(); }
491 pattern
492 neg_format() const
493 { return this->do_neg_format(); }
495 protected:
496 ~moneypunct()
497 { }
499 virtual char_type
500 do_decimal_point() const
501 { return char_type(); }
503 virtual char_type
504 do_thousands_sep() const
505 { return char_type(); }
507 virtual string
508 do_grouping() const
509 { return string(); }
511 virtual string_type
512 do_curr_symbol() const
513 { return string_type(); }
515 string_type
516 do_positive_sign() const
517 { return string_type(); }
519 string_type
520 do_negative_sign() const
521 { return string_type(); }
524 do_frac_digits() const
525 { return 0; }
527 pattern
528 do_pos_format() const
529 { return pattern(); }
531 pattern
532 do_neg_format() const
533 { return pattern(); }
535 } // namespace std
537 #endif // _GLIBCXX_TESTSUITE_CHARACTER_H