]> git.saurik.com Git - wxWidgets.git/blob - tests/xlocale/xlocale.cpp
Suppress warnings about double to int truncation in wxHTML code.
[wxWidgets.git] / tests / xlocale / xlocale.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xlocale/xlocale.cpp
3 // Purpose: wxXLocale & related unit test
4 // Author: Brian Vanderburg II, Vadim Zeitlin
5 // Created: 2008-01-16
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Brian Vanderburg II
8 // 2008 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 #include "testprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #if wxUSE_XLOCALE
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif // WX_PRECOMP
27
28 #include "wx/xlocale.h"
29
30 // --------------------------------------------------------------------------
31 // test class
32 // --------------------------------------------------------------------------
33
34 class XLocaleTestCase : public CppUnit::TestCase
35 {
36 public:
37 XLocaleTestCase() { }
38
39 private:
40 CPPUNIT_TEST_SUITE( XLocaleTestCase );
41 CPPUNIT_TEST( TestCtor );
42 CPPUNIT_TEST( TestCtypeFunctions );
43 CPPUNIT_TEST( TestStdlibFunctions );
44 CPPUNIT_TEST_SUITE_END();
45
46 void TestCtor();
47 void TestCtypeFunctions();
48 void TestStdlibFunctions();
49
50 void TestCtypeFunctionsWith(const wxXLocale& loc);
51 void TestStdlibFunctionsWith(const wxXLocale& loc);
52
53 DECLARE_NO_COPY_CLASS(XLocaleTestCase)
54 };
55
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( XLocaleTestCase );
58
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XLocaleTestCase, "XLocaleTestCase" );
61
62
63 // test the different wxXLocale ctors
64 void XLocaleTestCase::TestCtor()
65 {
66 CPPUNIT_ASSERT( !wxXLocale().IsOk() );
67 CPPUNIT_ASSERT( wxCLocale.IsOk() );
68 CPPUNIT_ASSERT( wxXLocale("C").IsOk() );
69 CPPUNIT_ASSERT( !wxXLocale("bloordyblop").IsOk() );
70
71 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH))
72 return; // you should have french support installed to continue this test!
73
74 #ifdef wxHAS_XLOCALE_SUPPORT
75 CPPUNIT_ASSERT( wxXLocale(wxLANGUAGE_FRENCH).IsOk() );
76 #ifdef __WXMSW__
77 CPPUNIT_ASSERT( wxXLocale("french").IsOk() );
78 #else
79 CPPUNIT_ASSERT( wxXLocale("fr_FR").IsOk() );
80 #endif
81 #endif
82 }
83
84 // test the ctype functions with the given locale
85 void XLocaleTestCase::TestCtypeFunctionsWith(const wxXLocale& loc)
86 {
87 // NOTE: here go the checks which must pass under _any_ locale "loc";
88 // checks for specific locales are in TestCtypeFunctions()
89
90
91 // isalnum
92 CPPUNIT_ASSERT( wxIsalnum_l('0', loc) );
93 CPPUNIT_ASSERT( wxIsalnum_l('9', loc) );
94 CPPUNIT_ASSERT( wxIsalnum_l('A', loc) );
95 CPPUNIT_ASSERT( wxIsalnum_l('Z', loc) );
96 CPPUNIT_ASSERT( wxIsalnum_l('a', loc) );
97 CPPUNIT_ASSERT( wxIsalnum_l('z', loc) );
98 CPPUNIT_ASSERT( !wxIsalnum_l('*', loc) );
99 CPPUNIT_ASSERT( !wxIsalnum_l('@', loc) );
100 CPPUNIT_ASSERT( !wxIsalnum_l('+', loc) );
101
102 // isalpha
103 CPPUNIT_ASSERT( !wxIsalpha_l('0', loc) );
104 CPPUNIT_ASSERT( !wxIsalpha_l('9', loc) );
105 CPPUNIT_ASSERT( wxIsalpha_l('A', loc) );
106 CPPUNIT_ASSERT( wxIsalpha_l('Z', loc) );
107 CPPUNIT_ASSERT( wxIsalpha_l('a', loc) );
108 CPPUNIT_ASSERT( wxIsalpha_l('z', loc) );
109 CPPUNIT_ASSERT( !wxIsalpha_l('*', loc) );
110 CPPUNIT_ASSERT( !wxIsalpha_l('@', loc) );
111 CPPUNIT_ASSERT( !wxIsalpha_l('+', loc) );
112
113 // TODO: iscntrl
114
115 // isdigit
116 CPPUNIT_ASSERT( wxIsdigit_l('0', loc) );
117 CPPUNIT_ASSERT( wxIsdigit_l('9', loc) );
118 CPPUNIT_ASSERT( !wxIsdigit_l('A', loc) );
119 CPPUNIT_ASSERT( !wxIsdigit_l('Z', loc) );
120 CPPUNIT_ASSERT( !wxIsdigit_l('a', loc) );
121 CPPUNIT_ASSERT( !wxIsdigit_l('z', loc) );
122
123 // TODO: isgraph
124
125 // islower
126 CPPUNIT_ASSERT( !wxIslower_l('A', loc) );
127 CPPUNIT_ASSERT( !wxIslower_l('Z', loc) );
128 CPPUNIT_ASSERT( wxIslower_l('a', loc) );
129 CPPUNIT_ASSERT( wxIslower_l('z', loc) );
130 CPPUNIT_ASSERT( !wxIslower_l('0', loc) );
131 CPPUNIT_ASSERT( !wxIslower_l('9', loc) );
132
133
134 // TODO: isprint
135 // TODO: ispunct
136
137 // isspace
138 CPPUNIT_ASSERT( wxIsspace_l(' ', loc) );
139 CPPUNIT_ASSERT( wxIsspace_l('\t', loc) );
140 CPPUNIT_ASSERT( wxIsspace_l('\r', loc) );
141 CPPUNIT_ASSERT( wxIsspace_l('\n', loc) );
142 CPPUNIT_ASSERT( !wxIsspace_l('0', loc) );
143 CPPUNIT_ASSERT( !wxIsspace_l('a', loc) );
144 CPPUNIT_ASSERT( !wxIsspace_l('A', loc) );
145
146 // isupper
147 CPPUNIT_ASSERT( !wxIsupper_l('0', loc) );
148 CPPUNIT_ASSERT( !wxIsupper_l('9', loc) );
149 CPPUNIT_ASSERT( wxIsupper_l('A', loc) );
150 CPPUNIT_ASSERT( wxIsupper_l('Z', loc) );
151 CPPUNIT_ASSERT( !wxIsupper_l('a', loc) );
152 CPPUNIT_ASSERT( !wxIsupper_l('z', loc) );
153
154 // isxdigit
155 CPPUNIT_ASSERT( wxIsxdigit_l('0', loc) );
156 CPPUNIT_ASSERT( wxIsxdigit_l('9', loc) );
157 CPPUNIT_ASSERT( wxIsxdigit_l('A', loc) );
158 CPPUNIT_ASSERT( wxIsxdigit_l('F', loc) );
159 CPPUNIT_ASSERT( !wxIsxdigit_l('Z', loc) );
160 CPPUNIT_ASSERT( wxIsxdigit_l('a', loc) );
161 CPPUNIT_ASSERT( wxIsxdigit_l('f', loc) );
162 CPPUNIT_ASSERT( !wxIsxdigit_l('z', loc) );
163
164 // tolower
165 CPPUNIT_ASSERT_EQUAL( 'a', (char)wxTolower_l('A', loc) );
166 CPPUNIT_ASSERT_EQUAL( 'a', (char)wxTolower_l('a', loc) );
167 CPPUNIT_ASSERT_EQUAL( 'z', (char)wxTolower_l('Z', loc) );
168 CPPUNIT_ASSERT_EQUAL( 'z', (char)wxTolower_l('z', loc) );
169 CPPUNIT_ASSERT_EQUAL( '0', (char)wxTolower_l('0', loc) );
170 CPPUNIT_ASSERT_EQUAL( '9', (char)wxTolower_l('9', loc) );
171
172 // toupper
173 CPPUNIT_ASSERT_EQUAL( 'A', (char)wxToupper_l('A', loc) );
174 CPPUNIT_ASSERT_EQUAL( 'A', (char)wxToupper_l('a', loc) );
175 CPPUNIT_ASSERT_EQUAL( 'Z', (char)wxToupper_l('Z', loc) );
176 CPPUNIT_ASSERT_EQUAL( 'Z', (char)wxToupper_l('z', loc) );
177 CPPUNIT_ASSERT_EQUAL( '0', (char)wxToupper_l('0', loc) );
178 CPPUNIT_ASSERT_EQUAL( '9', (char)wxToupper_l('9', loc) );
179 }
180
181 // test the stdlib functions with the given locale
182 void XLocaleTestCase::TestStdlibFunctionsWith(const wxXLocale& loc)
183 {
184 // NOTE: here go the checks which must pass under _any_ locale "loc";
185 // checks for specific locales are in TestStdlibFunctions()
186
187 #if wxUSE_UNICODE
188 wchar_t* endptr;
189 #else
190 char* endptr;
191 #endif
192
193 // strtod (don't use decimal separator as it's locale-specific)
194 CPPUNIT_ASSERT_EQUAL( 0.0, wxStrtod_l(wxT("0"), NULL, loc) );
195 CPPUNIT_ASSERT_EQUAL( 1234.0, wxStrtod_l(wxT("1234"), NULL, loc) );
196
197 // strtol
198 endptr = NULL;
199 CPPUNIT_ASSERT_EQUAL( 100, wxStrtol_l(wxT("100"), NULL, 0, loc) );
200 CPPUNIT_ASSERT_EQUAL( 0xFF, wxStrtol_l(wxT("0xFF"), NULL, 0, loc) );
201 CPPUNIT_ASSERT_EQUAL( 2001, wxStrtol_l(wxT("2001 60c0c0 -1101110100110100100000 0x6fffff"), &endptr, 10, loc) );
202 CPPUNIT_ASSERT_EQUAL( 0x60c0c0, wxStrtol_l(endptr, &endptr, 16, loc) );
203 CPPUNIT_ASSERT_EQUAL( -0x374D20, wxStrtol_l(endptr, &endptr, 2, loc) );
204 CPPUNIT_ASSERT_EQUAL( 0x6fffff, wxStrtol_l(endptr, NULL, 0, loc) );
205
206 // strtoul
207 // NOTE: 3147483647 and 0xEE6B2800 are greater than LONG_MAX (on 32bit machines) but
208 // smaller than ULONG_MAX
209 CPPUNIT_ASSERT_EQUAL( 3147483647ul, wxStrtoul_l(wxT("3147483647"), NULL, 0, loc) );
210 CPPUNIT_ASSERT_EQUAL( 0xEE6B2800ul, wxStrtoul_l(wxT("0xEE6B2800"), NULL, 0, loc) );
211
212 // TODO: test for "failure" behaviour of the functions above
213 }
214
215 void XLocaleTestCase::TestCtypeFunctions()
216 {
217 TestCtypeFunctionsWith(wxCLocale);
218
219 #ifdef wxHAS_XLOCALE_SUPPORT
220
221 // french
222
223 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH))
224 return; // you should have french support installed to continue this test!
225
226 wxXLocale locFR(wxLANGUAGE_FRENCH);
227 CPPUNIT_ASSERT( locFR.IsOk() ); // doesn't make sense to continue otherwise
228
229 TestCtypeFunctionsWith(locFR);
230
231 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe9'), locFR) );
232 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe9'), locFR) );
233 CPPUNIT_ASSERT( !wxIslower_l(wxT('\xc9'), locFR) );
234 CPPUNIT_ASSERT( wxIsupper_l(wxT('\xc9'), locFR) );
235 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe7'), locFR) );
236 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe7'), locFR) );
237 CPPUNIT_ASSERT( wxIsupper_l(wxT('\xc7'), locFR) );
238
239
240 // italian
241
242 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN))
243 return; // you should have italian support installed to continue this test!
244
245 wxXLocale locIT(wxLANGUAGE_ITALIAN);
246 CPPUNIT_ASSERT( locIT.IsOk() ); // doesn't make sense to continue otherwise
247
248 TestCtypeFunctionsWith(locIT);
249
250 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe1'), locIT) );
251 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe1'), locIT) );
252 #endif
253 }
254
255 void XLocaleTestCase::TestStdlibFunctions()
256 {
257 TestStdlibFunctionsWith(wxCLocale);
258
259 #if wxUSE_UNICODE
260 wchar_t* endptr;
261 #else
262 char* endptr;
263 #endif
264
265 // strtod checks specific for C locale
266 endptr = NULL;
267 CPPUNIT_ASSERT_EQUAL( 0.0, wxStrtod_l(wxT("0.000"), NULL, wxCLocale) );
268 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1.234"), NULL, wxCLocale) );
269 CPPUNIT_ASSERT_EQUAL( -1.234E-5, wxStrtod_l(wxT("-1.234E-5"), NULL, wxCLocale) );
270 CPPUNIT_ASSERT_EQUAL( 365.24, wxStrtod_l(wxT("365.24 29.53"), &endptr, wxCLocale) );
271 CPPUNIT_ASSERT_EQUAL( 29.53, wxStrtod_l(endptr, NULL, wxCLocale) );
272
273 #ifdef wxHAS_XLOCALE_SUPPORT
274
275 // french
276
277 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH))
278 return; // you should have french support installed to continue this test!
279
280 wxXLocale locFR(wxLANGUAGE_FRENCH);
281 CPPUNIT_ASSERT( locFR.IsOk() ); // doesn't make sense to continue otherwise
282
283 TestCtypeFunctionsWith(locFR);
284
285 // comma as decimal point:
286 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL, locFR) );
287
288 // space as thousands separator is not recognized by wxStrtod_l():
289 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1 234,5"), NULL, locFR) );
290
291
292 // italian
293
294 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN))
295 return; // you should have italian support installed to continue this test!
296
297 wxXLocale locIT(wxLANGUAGE_ITALIAN);
298 CPPUNIT_ASSERT( locIT.IsOk() ); // doesn't make sense to continue otherwise
299
300 TestStdlibFunctionsWith(locIT);
301
302 // comma as decimal point:
303 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL, locIT) );
304
305 // dot as thousands separator is not recognized by wxStrtod_l():
306 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1.234,5"), NULL, locIT) );
307 #endif
308 }
309
310 #endif // wxUSE_XLOCALE