OSDN Git Service

2002-03-12 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / ctype_members_char.cc
1 // 2000-02-16 bkoz
2
3 // Copyright (C) 2000, 2001, 2002 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 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // 22.2.1.3.2 ctype<char> members
31
32 #include <locale>
33 #include <vector>
34 #include <testsuite_hooks.h>
35
36 // XXX This test (test02) is not working for non-glibc locale models.
37 // { dg-do run { xfail *-*-* } }
38
39 class gnu_ctype: public std::ctype<char> { };
40
41 void test01()
42 {
43   bool test = true;
44   const char strlit00[] = "manilla, cebu, tandag PHILIPPINES";
45   const char strlit01[] = "MANILLA, CEBU, TANDAG PHILIPPINES";
46   const char strlit02[] = "manilla, cebu, tandag philippines";
47   const char c00 = 'S';
48   const char c10 = 's';
49   const char c20 = '9';
50   const char c30 = ' ';
51   const char c40 = '!';
52   const char c50 = 'F';
53   const char c60 = 'f';
54   const char c70 = 'X';
55   const char c80 = 'x';
56
57   gnu_ctype gctype;
58   char c100;
59   int len = std::char_traits<char>::length(strlit00);
60   char c_array[len + 1];
61
62   // sanity check ctype_base::mask members
63   int i01 = std::ctype_base::space;
64   int i02 = std::ctype_base::upper;
65   int i03 = std::ctype_base::lower;
66   int i04 = std::ctype_base::digit;
67   int i05 = std::ctype_base::punct;
68   int i06 = std::ctype_base::alpha;
69   int i07 = std::ctype_base::xdigit;
70   int i08 = std::ctype_base::alnum;
71   int i09 = std::ctype_base::graph;
72   int i10 = std::ctype_base::print;
73   int i11 = std::ctype_base::cntrl;
74   int i12 = sizeof(std::ctype_base::mask);
75   VERIFY ( i01 != i02);
76   VERIFY ( i02 != i03);
77   VERIFY ( i03 != i04);
78   VERIFY ( i04 != i05);
79   VERIFY ( i05 != i06);
80   VERIFY ( i06 != i07);
81   VERIFY ( i07 != i08);
82   VERIFY ( i08 != i09);
83   VERIFY ( i09 != i10);
84   VERIFY ( i10 != i11);
85   VERIFY ( i11 != i01);
86
87   // bool is(mask m, char c) const;
88   VERIFY( gctype.is(std::ctype_base::space, c30) );
89   VERIFY( gctype.is(std::ctype_base::upper, c00) );
90   VERIFY( gctype.is(std::ctype_base::lower, c10) );
91   VERIFY( gctype.is(std::ctype_base::digit, c20) );
92   VERIFY( gctype.is(std::ctype_base::punct, c40) );
93   VERIFY( gctype.is(std::ctype_base::alpha, c50) );
94   VERIFY( gctype.is(std::ctype_base::alpha, c60) );
95   VERIFY( gctype.is(std::ctype_base::xdigit, c20) );
96   VERIFY( !gctype.is(std::ctype_base::xdigit, c80) );
97   VERIFY( gctype.is(std::ctype_base::alnum, c50) );
98   VERIFY( gctype.is(std::ctype_base::alnum, c20) );
99   VERIFY( gctype.is(std::ctype_base::graph, c40) );
100   VERIFY( gctype.is(std::ctype_base::graph, c20) );
101
102   // const char* is(const char* low, const char* high, mask* vec) const
103   std::ctype_base::mask m00 = static_cast<std::ctype_base::mask>(0);
104   std::ctype_base::mask m01[3];
105   std::ctype_base::mask m02[13];
106   const char* cc0 = strlit00;
107   const char* cc1 = NULL;
108   const char* cc2 = NULL;
109
110   cc0 = strlit00;
111   m01[0] = m00;
112   m01[1] = m00;
113   m01[2] = m00;
114   cc1 = gctype.is(cc0, cc0, m01);
115   VERIFY( cc1 == strlit00 );
116   VERIFY( m01[0] == m00 );
117   VERIFY( m01[1] == m00 );
118   VERIFY( m01[2] == m00 );
119
120   cc0 = strlit00;
121   m01[0] = m00;
122   m01[1] = m00;
123   m01[2] = m00;
124   cc2 = gctype.is(cc0, cc0 + 3, m01);
125   VERIFY( cc2 == strlit00 + 3);
126   VERIFY( m01[0] != m00 );
127   VERIFY( m01[1] != m00 );
128   VERIFY( m01[2] != m00 );
129   VERIFY( gctype.is(m01[0], cc0[0]) );
130   VERIFY( gctype.is(m01[1], cc0[1]) );
131   VERIFY( gctype.is(m01[2], cc0[2]) );
132
133   cc0 = strlit01;
134   cc1 = gctype.is(cc0, cc0 + 13, m02);
135   VERIFY( cc1 == strlit01 + 13);
136   VERIFY( m02[6] != m00 );
137   VERIFY( m02[7] != m00 );
138   VERIFY( m02[8] != m00 );
139   VERIFY( m02[8] != m02[6] );
140   VERIFY( m02[6] != m02[7] );
141   VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alnum) );
142   VERIFY( static_cast<bool>(m02[6] & std::ctype_base::upper) );
143   VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alpha) );
144   VERIFY( static_cast<bool>(m02[7] & std::ctype_base::punct) );
145   VERIFY( static_cast<bool>(m02[8] & std::ctype_base::space) );
146   VERIFY( gctype.is(m02[6], cc0[6]) );
147   VERIFY( gctype.is(m02[7], cc0[7]) );
148   VERIFY( gctype.is(m02[8], cc0[8]) );
149
150   // char toupper(char c) const
151   c100 = gctype.toupper(c10);
152   VERIFY( c100 == c00 );
153
154   // char tolower(char c) const
155   c100 = gctype.tolower(c00);
156   VERIFY( c100 == c10 );
157
158   // char toupper(char* low, const char* hi) const
159   std::char_traits<char>::copy(c_array, strlit02, len + 1);
160   gctype.toupper(c_array, c_array + len);
161   VERIFY( !std::char_traits<char>::compare(c_array, strlit01, len - 1) );
162
163   // char tolower(char* low, const char* hi) const
164   std::char_traits<char>::copy(c_array, strlit01, len + 1);
165   gctype.tolower(c_array, c_array + len);
166   VERIFY( !std::char_traits<char>::compare(c_array, strlit02, len - 1) );
167
168
169 #ifdef DEBUG_ASSERT
170   assert(test);
171 #endif
172 }
173
174 // libstdc++/4456, libstdc++/4457, libstdc++/4458
175 void test02()
176 {
177   using namespace std;
178   typedef ctype_base::mask      mask;
179   typedef vector<mask>          vector_type;
180
181   bool test = true;
182
183   //  const int max = numeric_limits<char>::max();
184   const int max = 255;
185   const int ctype_mask_max = 10;
186   vector_type v_c(max);
187   vector_type v_de(max);
188
189   // "C"
190   locale loc_c = locale::classic();
191   const ctype<char>& ctype_c = use_facet<ctype<char> >(loc_c); 
192   for (int i = 0; i < max; ++i)
193     {
194       char c = static_cast<char>(i);
195       mask mask_test = static_cast<mask>(0);
196       mask mask_is = static_cast<mask>(0);
197       for (int j = 0; j <= ctype_mask_max; ++j)
198         {
199           mask_test = static_cast<mask>(1 << j);
200           if (ctype_c.is(mask_test, c))
201             mask_is |= mask_test;
202         }
203       v_c[i] = mask_is;
204     }   
205
206   // "de_DE"
207   locale loc_de("de_DE");
208   const ctype<char>& ctype_de = use_facet<ctype<char> >(loc_de); 
209   for (int i = 0; i < max; ++i)
210     {
211       char c = static_cast<char>(i);
212       mask mask_test = static_cast<mask>(0);
213       mask mask_is = static_cast<mask>(0);
214       for (int j = 0; j <= ctype_mask_max; ++j)
215         {
216           mask_test = static_cast<mask>(1 << j);
217           if (ctype_de.is(mask_test, c))
218             mask_is |= mask_test;
219         }
220       v_de[i] = mask_is;
221     }   
222
223 #if QUANNUM_VERBOSE_LYRICALLY_ADEPT_BAY_AREA_MCS_MODE
224     for (int i = 0; i < max; ++i)
225     {
226       char mark = v_c[i] == v_de[i] ? ' ' : '-';
227       cout << i << ' ' << mark << ' ' << static_cast<char>(i) << '\t' ;
228       cout << "v_c: " << setw(4) << v_c[i] << '\t';
229       cout << "v_de: " << setw(4) << v_de[i] << endl;
230     }
231     cout << (v_c == v_de) << endl;
232 #endif
233
234   VERIFY( v_c != v_de );
235 }
236
237 // Dietmar Kühl via Peter Schmid 
238 class comma_ctype: public std::ctype<char>
239 {
240 public:
241   comma_ctype(): std::ctype<char>() { }
242   static void get_table()
243   { classic_table(); }
244 };
245
246 // Per Liboriussen <liborius@stofanet.dk>
247 void test03()
248 {
249   bool test = true;
250   std::ctype_base::mask maskdata[256];
251   for (int i = 0; i < 256; ++i)
252     maskdata[i] = std::ctype_base::alpha;
253   std::ctype<char>* f = new std::ctype<char>(maskdata);
254   std::locale global;
255   std::locale loc(global, f);
256   for (int i = 0; i < 256; ++i) 
257     {
258       char ch = i;
259       VERIFY( std::isalpha(ch, loc) );
260     }
261 }
262
263 // libstdc++/5280
264 void test04()
265 {
266 #ifdef _GLIBCPP_HAVE_SETENV 
267   // Set the global locale to non-"C".
268   std::locale loc_de("de_DE");
269   std::locale::global(loc_de);
270
271   // Set LANG environment variable to de_DE.
272   const char* oldLANG = getenv("LANG");
273   if (!setenv("LANG", "de_DE", 1))
274     {
275       test01();
276       test02();
277       test03();
278       setenv("LANG", oldLANG ? oldLANG : "", 1);
279     }
280 #endif
281 }
282
283 int main() 
284 {
285   test01();
286   test02();
287   test03();
288   test04();
289   return 0;
290 }