1// Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
2//
3// This file is part of the GNU ISO C++ Library.  This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 2, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING.  If not, write to the Free
16// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17// USA.
18
19// As a special exception, you may use this file as part of a free software
20// library without restriction.  Specifically, if other files instantiate
21// templates or use macros or inline functions from this file, or you compile
22// this file and link it with other files to produce an executable, this
23// file does not by itself cause the resulting executable to be covered by
24// the GNU General Public License.  This exception does not however
25// invalidate any other reasons why the executable file might be covered by
26// the GNU General Public License.
27
28// 22.2.1.3.2 ctype<char> members
29
30#include <locale>
31#include <vector>
32#include <testsuite_hooks.h>
33
34// XXX This test (test02) is not working for non-glibc locale models.
35// { dg-do run { xfail *-*-* } }
36
37#ifdef _GLIBCPP_USE_WCHAR_T
38typedef wchar_t char_type;
39class gnu_ctype: public std::ctype<char_type> { };
40
41void test01()
42{
43  bool test = true;
44  const char_type strlit00[] = L"manilla, cebu, tandag PHILIPPINES";
45  const char_type strlit01[] = L"MANILLA, CEBU, TANDAG PHILIPPINES";
46  const char_type strlit02[] = L"manilla, cebu, tandag philippines";
47  const char_type c00 = L'S';
48  const char_type c10 = L's';
49  const char_type c20 = L'9';
50  const char_type c30 = L' ';
51  const char_type c40 = L'!';
52  const char_type c50 = L'F';
53  const char_type c60 = L'f';
54  const char_type c70 = L'X';
55  const char_type c80 = L'x';
56
57  gnu_ctype gctype;
58  char_type c100;
59  int len = std::char_traits<char_type>::length(strlit00);
60  char_type 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_type 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_type* cc0 = strlit00;
107  const char_type* cc1 = NULL;
108  const char_type* 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#if 0
127  VERIFY( m01[0] != m00 );
128  VERIFY( m01[1] != m00 );
129  VERIFY( m01[2] != m00 );
130  VERIFY( gctype.is(m01[0], cc0[0]) );
131  VERIFY( gctype.is(m01[1], cc0[1]) );
132  VERIFY( gctype.is(m01[2], cc0[2]) );
133#endif
134
135  cc0 = strlit01;
136  cc1 = gctype.is(cc0, cc0 + 13, m02);
137  VERIFY( cc1 == strlit01 + 13);
138#if 0
139  VERIFY( m02[6] != m00 );
140  VERIFY( m02[7] != m00 );
141  VERIFY( m02[8] != m00 );
142  VERIFY( m02[8] != m02[6] );
143  VERIFY( m02[6] != m02[7] );
144  VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alnum) );
145  VERIFY( static_cast<bool>(m02[6] & std::ctype_base::upper) );
146  VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alpha) );
147  VERIFY( static_cast<bool>(m02[7] & std::ctype_base::punct) );
148  VERIFY( static_cast<bool>(m02[8] & std::ctype_base::space) );
149  VERIFY( gctype.is(m02[6], cc0[6]) );
150  VERIFY( gctype.is(m02[7], cc0[7]) );
151  VERIFY( gctype.is(m02[8], cc0[8]) );
152#endif
153}
154
155// libstdc++/5280
156void test04()
157{
158#ifdef _GLIBCPP_HAVE_SETENV
159  // Set the global locale to non-"C".
160  std::locale loc_de("de_DE");
161  std::locale::global(loc_de);
162
163  // Set LANG environment variable to de_DE.
164  const char* oldLANG = getenv("LANG");
165  if (!setenv("LANG", "de_DE", 1))
166    {
167      test01();
168      setenv("LANG", oldLANG ? oldLANG : "", 1);
169    }
170#endif
171}
172
173// http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
174void test05()
175{
176  bool test = true;
177
178  const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
179  if (tentLANG != NULL)
180    {
181      std::string preLANG = tentLANG;
182      test01();
183      std::string postLANG = std::setlocale(LC_ALL, NULL);
184      VERIFY( preLANG == postLANG );
185    }
186}
187#endif
188
189int main()
190{
191#ifdef _GLIBCPP_USE_WCHAR_T
192  test01();
193  test04();
194  test05();
195#endif
196  return 0;
197}
198