c59ab329ebd670983c7098dd71f8dbcca63fd871
[platform/upstream/gcc.git] / libstdc++-v3 / testsuite / 22_locale / collate_members_wchar_t.cc
1 // 2001-08-17 Benjamin Kosnik  <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002 Free Software Foundation
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 // 22.2.4.1.1 collate members
22
23 #include <locale>
24 #include <testsuite_hooks.h>
25
26 // XXX This may not work for non-glibc locale models.
27 // { dg-do run { xfail *-*-* } }
28 #ifdef _GLIBCPP_USE_WCHAR_T
29 void test01()
30 {
31   using namespace std;
32   typedef std::collate<wchar_t>::string_type string_type;
33
34   bool test = true;
35
36   // basic construction
37   locale loc_c = locale::classic();
38   locale loc_us("en_US");
39   locale loc_fr("fr_FR");
40   locale loc_de("de_DE");
41   VERIFY( loc_c != loc_de );
42   VERIFY( loc_us != loc_fr );
43   VERIFY( loc_us != loc_de );
44   VERIFY( loc_de != loc_fr );
45
46   // cache the collate facets
47   const collate<wchar_t>& coll_c = use_facet<collate<wchar_t> >(loc_c); 
48   const collate<wchar_t>& coll_us = use_facet<collate<wchar_t> >(loc_us); 
49   const collate<wchar_t>& coll_fr = use_facet<collate<wchar_t> >(loc_fr); 
50   const collate<wchar_t>& coll_de = use_facet<collate<wchar_t> >(loc_de); 
51
52   // int compare(const charT*, const charT*, const charT*, const charT*) const
53   // long hash(const charT*, const charT*) cosnt
54   // string_type transform(const charT*, const charT*) const
55
56   // Check "C" locale.
57   const wchar_t* strlit1 = L"monkey picked tikuanyin oolong";
58   const wchar_t* strlit2 = L"imperial tea court green oolong";
59
60   int i1;
61   int size1 = char_traits<wchar_t>::length(strlit1) - 1;
62   i1 = coll_c.compare(strlit1, strlit1 + size1, strlit1, strlit1 + 7);
63   VERIFY ( i1 == 1 );
64   i1 = coll_c.compare(strlit1, strlit1 + 7, strlit1, strlit1 + size1);
65   VERIFY ( i1 == -1 );
66   i1 = coll_c.compare(strlit1, strlit1 + 7, strlit1, strlit1 + 7);
67   VERIFY ( i1 == 0 );
68
69   int i2;
70   int size2 = char_traits<wchar_t>::length(strlit2) - 1;
71   i2 = coll_c.compare(strlit2, strlit2 + size2, strlit2, strlit2 + 13);
72   VERIFY ( i2 == 1 );
73   i2 = coll_c.compare(strlit2, strlit2 + 13, strlit2, strlit2 + size2);
74   VERIFY ( i2 == -1 );
75   i2 = coll_c.compare(strlit2, strlit2 + size2, strlit2, strlit2 + size2);
76   VERIFY ( i2 == 0 );
77
78   long l1;
79   long l2;
80   l1 = coll_c.hash(strlit1, strlit1 + size1);
81   l2 = coll_c.hash(strlit1, strlit1 + size1 - 1);
82   VERIFY ( l1 != l2 );
83   l1 = coll_c.hash(strlit1, strlit1 + size1);
84   l2 = coll_c.hash(strlit2, strlit2 + size2);
85   VERIFY ( l1 != l2 );
86
87   wstring str1 = coll_c.transform(strlit1, strlit1 + size1);
88   wstring str2 = coll_c.transform(strlit2, strlit2 + size2);
89   i1 = str1.compare(str2);
90   i2 = coll_c.compare(strlit1, strlit1 + size1, strlit2, strlit2 + size2);
91   VERIFY ( i2 == 1 );
92   VERIFY ( i1 * i2 > 0 );
93
94   // Check German "de_DE" locale.
95   const wchar_t* strlit3 = L"Äuglein Augment"; // "C" == "Augment Äuglein"
96   const wchar_t* strlit4 = L"Base baß Baß Bast"; // "C" == "Base baß Baß Bast"
97
98   int size3 = char_traits<wchar_t>::length(strlit3) - 1;
99   i1 = coll_de.compare(strlit3, strlit3 + size3, strlit3, strlit3 + 7);
100   VERIFY ( i1 == 1 );
101   i1 = coll_de.compare(strlit3, strlit3 + 7, strlit3, strlit3 + size1);
102   VERIFY ( i1 == -1 );
103   i1 = coll_de.compare(strlit3, strlit3 + 7, strlit3, strlit3 + 7);
104   VERIFY ( i1 == 0 );
105
106   i1 = coll_de.compare(strlit3, strlit3 + 6, strlit3 + 8, strlit3 + 14);
107   VERIFY ( i1 == -1 );
108
109   int size4 = char_traits<wchar_t>::length(strlit4) - 1;
110   i2 = coll_de.compare(strlit4, strlit4 + size4, strlit4, strlit4 + 13);
111   VERIFY ( i2 == 1 );
112   i2 = coll_de.compare(strlit4, strlit4 + 13, strlit4, strlit4 + size4);
113   VERIFY ( i2 == -1 );
114   i2 = coll_de.compare(strlit4, strlit4 + size4, strlit4, strlit4 + size4);
115   VERIFY ( i2 == 0 );
116
117   l1 = coll_de.hash(strlit3, strlit3 + size3);
118   l2 = coll_de.hash(strlit3, strlit3 + size3 - 1);
119   VERIFY ( l1 != l2 );
120   l1 = coll_de.hash(strlit3, strlit3 + size3);
121   l2 = coll_de.hash(strlit4, strlit4 + size4);
122   VERIFY ( l1 != l2 );
123
124   wstring str3 = coll_de.transform(strlit3, strlit3 + size3);
125   wstring str4 = coll_de.transform(strlit4, strlit4 + size4);
126   i1 = str3.compare(str4);
127   i2 = coll_de.compare(strlit3, strlit3 + size3, strlit4, strlit4 + size4);
128   VERIFY ( i2 == -1 );
129   VERIFY ( i1 * i2 > 0 );
130 }
131
132 // libstdc++/5280
133 void test02()
134 {
135 #ifdef _GLIBCPP_HAVE_SETENV 
136   // Set the global locale to non-"C".
137   std::locale loc_de("de_DE");
138   std::locale::global(loc_de);
139
140   // Set LANG environment variable to de_DE.
141   const char* oldLANG = getenv("LANG");
142   if (!setenv("LANG", "de_DE", 1))
143     {
144       test01();
145       setenv("LANG", oldLANG ? oldLANG : "", 1);
146     }
147 #endif
148 }
149
150 void test03()
151 {
152   bool test = true;
153   std::wstring str1(L"fffff");
154   std::wstring str2(L"ffffffffffff");
155
156   const std::locale cloc = std::locale::classic();
157   const std::collate<wchar_t> &col = std::use_facet<std::collate<wchar_t> >(cloc);
158
159   long l1 = col.hash(str1.c_str(), str1.c_str() + str1.size());
160   long l2 = col.hash(str2.c_str(), str2.c_str() + str2.size());
161   VERIFY( l1 != l2 );
162 }
163
164 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
165 void test04()
166 {
167   bool test = true;
168
169   const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
170   if (tentLANG != NULL)
171     {
172       std::string preLANG = tentLANG;
173       test01();
174       test03();
175       std::string postLANG = std::setlocale(LC_ALL, NULL);
176       VERIFY( preLANG == postLANG );
177     }
178 }
179 #endif
180
181 int main()
182 {
183 #if _GLIBCPP_USE_WCHAR_T
184   test01();
185   test02();
186   test03();
187   test04();
188 #endif
189   return 0;
190 }