ebea4031e3bbaee86b9e9dd2858f45578b5bab38
[gcc.git] / libstdc++-v3 / testsuite / 22_locale / ctype_char_members.cc
1 // 2000-02-16 bkoz
2
3 // Copyright (C) 2000 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 // NB: Don't include any other headers in this file.
34 #include <debug_assert.h>
35
36 class gnu_ctype: public std::ctype<char> { };
37
38 void test01()
39 {
40 bool test = true;
41 const char strlit00[] = "manilla, cebu, tandag PHILIPPINES";
42 const char strlit01[] = "MANILLA, CEBU, TANDAG PHILIPPINES";
43 const char strlit02[] = "manilla, cebu, tandag philippines";
44 const char c00 = 'S';
45 const char c10 = 's';
46 const char c20 = '9';
47 const char c30 = ' ';
48 const char c40 = '!';
49 const char c50 = 'F';
50 const char c60 = 'f';
51 const char c70 = 'X';
52 const char c80 = 'x';
53
54 gnu_ctype gctype;
55 char c100;
56 int len = std::char_traits<char>::length(strlit00);
57 char c_array[len + 1];
58
59 // sanity check ctype_base::mask members
60 int i01 = std::ctype_base::space;
61 int i02 = std::ctype_base::upper;
62 int i03 = std::ctype_base::lower;
63 int i04 = std::ctype_base::digit;
64 int i05 = std::ctype_base::punct;
65 int i06 = std::ctype_base::alpha;
66 int i07 = std::ctype_base::xdigit;
67 int i08 = std::ctype_base::alnum;
68 int i09 = std::ctype_base::graph;
69 int i10 = std::ctype_base::print;
70 int i11 = std::ctype_base::cntrl;
71 int i12 = sizeof(std::ctype_base::mask);
72 VERIFY ( i01 != i02);
73 VERIFY ( i02 != i03);
74 VERIFY ( i03 != i04);
75 VERIFY ( i04 != i05);
76 VERIFY ( i05 != i06);
77 VERIFY ( i06 != i07);
78 VERIFY ( i07 != i08);
79 VERIFY ( i08 != i09);
80 VERIFY ( i09 != i10);
81 VERIFY ( i10 != i11);
82 VERIFY ( i11 != i01);
83
84 // bool is(mask m, char c) const;
85 VERIFY( gctype.is(std::ctype_base::space, c30) );
86 VERIFY( gctype.is(std::ctype_base::upper, c00) );
87 VERIFY( gctype.is(std::ctype_base::lower, c10) );
88 VERIFY( gctype.is(std::ctype_base::digit, c20) );
89 VERIFY( gctype.is(std::ctype_base::punct, c40) );
90 VERIFY( gctype.is(std::ctype_base::alpha, c50) );
91 VERIFY( gctype.is(std::ctype_base::alpha, c60) );
92 VERIFY( gctype.is(std::ctype_base::xdigit, c20) );
93 VERIFY( !gctype.is(std::ctype_base::xdigit, c80) );
94 VERIFY( gctype.is(std::ctype_base::alnum, c50) );
95 VERIFY( gctype.is(std::ctype_base::alnum, c20) );
96 VERIFY( gctype.is(std::ctype_base::graph, c40) );
97 VERIFY( gctype.is(std::ctype_base::graph, c20) );
98
99 // const char* is(const char* low, const char* high, mask* vec) const
100 std::ctype_base::mask m00 = static_cast<std::ctype_base::mask>(0);
101 std::ctype_base::mask m01[3];
102 std::ctype_base::mask m02[13];
103 const char* cc0 = strlit00;
104 const char* cc1 = NULL;
105 const char* cc2 = NULL;
106
107 cc0 = strlit00;
108 m01[0] = m00;
109 m01[1] = m00;
110 m01[2] = m00;
111 cc1 = gctype.is(cc0, cc0, m01);
112 VERIFY( cc1 == strlit00 );
113 VERIFY( m01[0] == m00 );
114 VERIFY( m01[1] == m00 );
115 VERIFY( m01[2] == m00 );
116
117 cc0 = strlit00;
118 m01[0] = m00;
119 m01[1] = m00;
120 m01[2] = m00;
121 cc2 = gctype.is(cc0, cc0 + 3, m01);
122 VERIFY( cc2 == strlit00 + 3);
123 VERIFY( m01[0] != m00 );
124 VERIFY( m01[1] != m00 );
125 VERIFY( m01[2] != m00 );
126 VERIFY( gctype.is(m01[0], cc0[0]) );
127 VERIFY( gctype.is(m01[1], cc0[1]) );
128 VERIFY( gctype.is(m01[2], cc0[2]) );
129
130 cc0 = strlit01;
131 cc1 = gctype.is(cc0, cc0 + 13, m02);
132 VERIFY( cc1 == strlit01 + 13);
133 VERIFY( m02[6] != m00 );
134 VERIFY( m02[7] != m00 );
135 VERIFY( m02[8] != m00 );
136 VERIFY( m02[8] != m02[6] );
137 VERIFY( m02[6] != m02[7] );
138 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alnum) );
139 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::upper) );
140 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alpha) );
141 VERIFY( static_cast<bool>(m02[7] & std::ctype_base::punct) );
142 VERIFY( static_cast<bool>(m02[8] & std::ctype_base::space) );
143 VERIFY( gctype.is(m02[6], cc0[6]) );
144 VERIFY( gctype.is(m02[7], cc0[7]) );
145 VERIFY( gctype.is(m02[8], cc0[8]) );
146
147 // char toupper(char c) const
148 c100 = gctype.toupper(c10);
149 VERIFY( c100 == c00 );
150
151 // char tolower(char c) const
152 c100 = gctype.tolower(c00);
153 VERIFY( c100 == c10 );
154
155 // char toupper(char* low, const char* hi) const
156 std::char_traits<char>::copy(c_array, strlit02, len + 1);
157 gctype.toupper(c_array, c_array + len);
158 VERIFY( !std::char_traits<char>::compare(c_array, strlit01, len - 1) );
159
160 // char tolower(char* low, const char* hi) const
161 std::char_traits<char>::copy(c_array, strlit01, len + 1);
162 gctype.tolower(c_array, c_array + len);
163 VERIFY( !std::char_traits<char>::compare(c_array, strlit02, len - 1) );
164
165
166 #ifdef DEBUG_ASSERT
167 assert(test);
168 #endif
169 }
170
171 int main() {
172 test01();
173 return 0;
174 }