xref: /trunk/main/sal/qa/rtl/locale/rtl_locale.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // autogenerated file with codegen.pl
32 
33 #include <testshl/simpleheader.hxx>
34 #include <rtl/locale.hxx>
35 #include <osl/thread.h>
36 
37 namespace rtl_locale
38 {
39     // default locale for test purpose
40     void setDefaultLocale()
41     {
42         rtl::OLocale::setDefault(rtl::OUString::createFromAscii("de"), rtl::OUString::createFromAscii("DE"), /* rtl::OUString() */ rtl::OUString::createFromAscii("hochdeutsch") );
43     }
44 
45 class getDefault : public CppUnit::TestFixture
46 {
47 public:
48     // initialise your test code values here.
49     void setUp()
50     {
51     }
52 
53     void tearDown()
54     {
55     }
56 
57     // insert your test code here.
58     void getDefault_000()
59     {
60         // this is demonstration code
61         // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
62 
63         // due to the fact, we set the default locale at first, this test is no longer possible
64    	// ::rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
65         // CPPUNIT_ASSERT_MESSAGE("locale must be null", aLocale.getData() == NULL);
66 
67     }
68 
69     void getDefault_001()
70     {
71         // rtl::OLocale::setDefault(rtl::OUString::createFromAscii("de"), rtl::OUString::createFromAscii("DE"), rtl::OUString());
72    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
73         CPPUNIT_ASSERT_MESSAGE("locale must not null", aLocale.getData() != NULL);
74     }
75 
76     // Change the following lines only, if you add, remove or rename
77     // member functions of the current class,
78     // because these macros are need by auto register mechanism.
79 
80     CPPUNIT_TEST_SUITE(getDefault);
81     CPPUNIT_TEST(getDefault_000);
82     CPPUNIT_TEST(getDefault_001);
83     CPPUNIT_TEST_SUITE_END();
84 }; // class getDefault
85 
86 
87 class setDefault : public CppUnit::TestFixture
88 {
89 public:
90     // initialise your test code values here.
91     void setUp()
92     {
93     }
94 
95     void tearDown()
96     {
97         setDefaultLocale();
98     }
99 
100     // insert your test code here.
101     void setDefault_001()
102     {
103         rtl::OLocale::setDefault(rtl::OUString::createFromAscii("en"), rtl::OUString::createFromAscii("US"), rtl::OUString());
104    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
105         CPPUNIT_ASSERT_MESSAGE("locale must not null", aLocale.getData() != NULL);
106 
107         // be sure to not GPF
108     }
109 
110     // Change the following lines only, if you add, remove or rename
111     // member functions of the current class,
112     // because these macros are need by auto register mechanism.
113 
114     CPPUNIT_TEST_SUITE(setDefault);
115     CPPUNIT_TEST(setDefault_001);
116     CPPUNIT_TEST_SUITE_END();
117 }; // class setDefault
118 
119 
120 class getLanguage : public CppUnit::TestFixture
121 {
122 public:
123     // initialise your test code values here.
124     void setUp()
125     {
126     }
127 
128     void tearDown()
129     {
130     }
131 
132     // insert your test code here.
133     void getLanguage_001()
134     {
135    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
136         rtl::OUString suLanguage = aLocale.getLanguage();
137         t_print("Language: %s\n", rtl::OUStringToOString(suLanguage, osl_getThreadTextEncoding()).getStr());
138         CPPUNIT_ASSERT_MESSAGE("locale language must be 'de'", suLanguage.equals(rtl::OUString::createFromAscii("de")));
139     }
140     void getLanguage_002()
141     {
142    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
143         rtl::OUString suLanguage = rtl_locale_getLanguage(aLocale.getData());
144         t_print("Language: %s\n", rtl::OUStringToOString(suLanguage, osl_getThreadTextEncoding()).getStr());
145         CPPUNIT_ASSERT_MESSAGE("locale language must be 'de'", suLanguage.equals(rtl::OUString::createFromAscii("de")));
146     }
147 
148     // Change the following lines only, if you add, remove or rename
149     // member functions of the current class,
150     // because these macros are need by auto register mechanism.
151 
152     CPPUNIT_TEST_SUITE(getLanguage);
153     CPPUNIT_TEST(getLanguage_001);
154     CPPUNIT_TEST(getLanguage_002);
155     CPPUNIT_TEST_SUITE_END();
156 }; // class getLanguage
157 
158 
159 class getCountry : public CppUnit::TestFixture
160 {
161 public:
162     // initialise your test code values here.
163     void setUp()
164     {
165     }
166 
167     void tearDown()
168     {
169     }
170 
171     // insert your test code here.
172     void getCountry_001()
173     {
174    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
175         rtl::OUString suCountry = aLocale.getCountry();
176         t_print("Country: %s\n", rtl::OUStringToOString(suCountry, osl_getThreadTextEncoding()).getStr());
177         CPPUNIT_ASSERT_MESSAGE("locale country must be 'DE'", suCountry.equals(rtl::OUString::createFromAscii("DE")));
178     }
179     void getCountry_002()
180     {
181    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
182         rtl::OUString suCountry = rtl_locale_getCountry(aLocale.getData());
183         t_print("Country: %s\n", rtl::OUStringToOString(suCountry, osl_getThreadTextEncoding()).getStr());
184         CPPUNIT_ASSERT_MESSAGE("locale country must be 'DE'", suCountry.equals(rtl::OUString::createFromAscii("DE")));
185     }
186 
187     // Change the following lines only, if you add, remove or rename
188     // member functions of the current class,
189     // because these macros are need by auto register mechanism.
190 
191     CPPUNIT_TEST_SUITE(getCountry);
192     CPPUNIT_TEST(getCountry_001);
193     CPPUNIT_TEST(getCountry_002);
194     CPPUNIT_TEST_SUITE_END();
195 }; // class getCountry
196 
197 
198 class getVariant : public CppUnit::TestFixture
199 {
200 public:
201     // initialise your test code values here.
202     void setUp()
203     {
204     }
205 
206     void tearDown()
207     {
208     }
209 
210     // insert your test code here.
211     void getVariant_001()
212     {
213    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
214         rtl::OUString suVariant = aLocale.getVariant();
215         t_print("Variant: %s\n", rtl::OUStringToOString(suVariant, osl_getThreadTextEncoding()).getStr());
216         CPPUNIT_ASSERT_MESSAGE("locale variant must be 'hochdeutsch'", suVariant.equals(rtl::OUString::createFromAscii("hochdeutsch")));
217     }
218     void getVariant_002()
219     {
220    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
221         rtl::OUString suVariant = rtl_locale_getVariant(aLocale.getData());
222         t_print("Variant: %s\n", rtl::OUStringToOString(suVariant, osl_getThreadTextEncoding()).getStr());
223         CPPUNIT_ASSERT_MESSAGE("locale variant must be 'hochdeutsch'", suVariant.equals(rtl::OUString::createFromAscii("hochdeutsch")));
224     }
225 
226     // Change the following lines only, if you add, remove or rename
227     // member functions of the current class,
228     // because these macros are need by auto register mechanism.
229 
230     CPPUNIT_TEST_SUITE(getVariant);
231     CPPUNIT_TEST(getVariant_001);
232     CPPUNIT_TEST(getVariant_002);
233     CPPUNIT_TEST_SUITE_END();
234 }; // class getVariant
235 
236 
237 class hashCode : public CppUnit::TestFixture
238 {
239 public:
240     // initialise your test code values here.
241     void setUp()
242     {
243     }
244 
245     void tearDown()
246     {
247     }
248 
249     // insert your test code here.
250     void hashCode_001()
251     {
252    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
253         sal_Int32 nHashCode = aLocale.hashCode();
254         t_print("Hashcode: %d\n", nHashCode);
255         CPPUNIT_ASSERT_MESSAGE("locale hashcode must be 3831", nHashCode != 0);
256     }
257     void hashCode_002()
258     {
259    	rtl::OLocale aLocale = ::rtl::OLocale::getDefault();
260         sal_Int32 nHashCode = rtl_locale_hashCode(aLocale.getData());
261         t_print("Hashcode: %d\n", nHashCode);
262         CPPUNIT_ASSERT_MESSAGE("locale hashcode must be 3831", nHashCode != 0);
263     }
264 
265     // Change the following lines only, if you add, remove or rename
266     // member functions of the current class,
267     // because these macros are need by auto register mechanism.
268 
269     CPPUNIT_TEST_SUITE(hashCode);
270     CPPUNIT_TEST(hashCode_001);
271     CPPUNIT_TEST(hashCode_002);
272     CPPUNIT_TEST_SUITE_END();
273 }; // class hashCode
274 
275 
276 class equals : public CppUnit::TestFixture
277 {
278 public:
279     // initialise your test code values here.
280     void setUp()
281     {
282     }
283 
284     void tearDown()
285     {
286     }
287 
288     // insert your test code here.
289     void equals_001()
290     {
291         rtl::OLocale aLocale1 = rtl::OLocale::registerLocale(rtl::OUString::createFromAscii("en"), rtl::OUString::createFromAscii("US"), rtl::OUString());
292         rtl::OLocale aLocale2 = rtl::OLocale::registerLocale(rtl::OUString::createFromAscii("en"), rtl::OUString::createFromAscii("US"));
293 
294         sal_Bool bLocaleAreEqual = sal_False;
295         bLocaleAreEqual = (aLocale1 == aLocale2);
296 
297         CPPUNIT_ASSERT_MESSAGE("check operator ==()", bLocaleAreEqual == sal_True);
298     }
299 
300     void equals_002()
301     {
302         rtl::OLocale aLocale1 = rtl::OLocale::registerLocale(rtl::OUString::createFromAscii("en"), rtl::OUString::createFromAscii("US"), rtl::OUString());
303         rtl::OLocale aLocale2 = rtl::OLocale::registerLocale(rtl::OUString::createFromAscii("en"), rtl::OUString::createFromAscii("US"));
304 
305         sal_Int32 nEqual = rtl_locale_equals(aLocale1.getData(), aLocale2.getData());
306         t_print("rtl_locale_equals() result: %d\n", nEqual);
307         CPPUNIT_ASSERT(nEqual != 0);
308     }
309 
310     // Change the following lines only, if you add, remove or rename
311     // member functions of the current class,
312     // because these macros are need by auto register mechanism.
313 
314     CPPUNIT_TEST_SUITE(equals);
315     CPPUNIT_TEST(equals_001);
316     CPPUNIT_TEST(equals_002);
317     CPPUNIT_TEST_SUITE_END();
318 }; // class equals
319 
320 // -----------------------------------------------------------------------------
321 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::getDefault, "rtl_locale");
322 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::setDefault, "rtl_locale");
323 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::getLanguage, "rtl_locale");
324 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::getCountry, "rtl_locale");
325 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::getVariant, "rtl_locale");
326 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::hashCode, "rtl_locale");
327 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_locale::equals, "rtl_locale");
328 } // namespace rtl_locale
329 
330 
331 // -----------------------------------------------------------------------------
332 
333 // this macro creates an empty function, which will called by the RegisterAllFunctions()
334 // to let the user the possibility to also register some functions by hand.
335 // NOADDITIONAL;
336 
337 void RegisterAdditionalFunctions(FktRegFuncPtr)
338 {
339     // start message
340     t_print("Initializing ...\n" );
341     rtl_locale::setDefaultLocale();
342     t_print("Initialization Done.\n" );
343 }
344