]>
git.saurik.com Git - wxWidgets.git/blob - tests/xlocale/xlocale.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xlocale/xlocale.cpp
3 // Purpose: wxXLocale & related unit test
4 // Author: Brian Vanderburg II, Vadim Zeitlin
6 // Copyright: (c) 2008 Brian Vanderburg II
7 // 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
27 #include "wx/xlocale.h"
29 // --------------------------------------------------------------------------
31 // --------------------------------------------------------------------------
33 class XLocaleTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( XLocaleTestCase
);
40 CPPUNIT_TEST( TestCtor
);
41 CPPUNIT_TEST( PreserveLocale
);
42 CPPUNIT_TEST( TestCtypeFunctions
);
43 CPPUNIT_TEST( TestStdlibFunctions
);
44 CPPUNIT_TEST_SUITE_END();
47 void PreserveLocale();
48 void TestCtypeFunctions();
49 void TestStdlibFunctions();
51 void TestCtypeFunctionsWith(const wxXLocale
& loc
);
52 void TestStdlibFunctionsWith(const wxXLocale
& loc
);
54 DECLARE_NO_COPY_CLASS(XLocaleTestCase
)
57 // register in the unnamed registry so that these tests are run by default
58 CPPUNIT_TEST_SUITE_REGISTRATION( XLocaleTestCase
);
60 // also include in its own registry so that these tests can be run alone
61 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XLocaleTestCase
, "XLocaleTestCase" );
64 // test the different wxXLocale ctors
65 void XLocaleTestCase::TestCtor()
67 CPPUNIT_ASSERT( !wxXLocale().IsOk() );
68 CPPUNIT_ASSERT( wxCLocale
.IsOk() );
69 CPPUNIT_ASSERT( wxXLocale("C").IsOk() );
70 CPPUNIT_ASSERT( !wxXLocale("bloordyblop").IsOk() );
72 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
73 return; // you should have french support installed to continue this test!
75 #ifdef wxHAS_XLOCALE_SUPPORT
76 CPPUNIT_ASSERT( wxXLocale(wxLANGUAGE_FRENCH
).IsOk() );
78 CPPUNIT_ASSERT( wxXLocale("french").IsOk() );
80 CPPUNIT_ASSERT( wxXLocale("fr_FR").IsOk() );
85 void XLocaleTestCase::PreserveLocale()
87 // Test that using locale functions doesn't change the global C locale.
88 const wxString
origLocale(setlocale(LC_ALL
, NULL
));
90 wxStrtod_l(wxT("1.234"), NULL
, wxCLocale
);
92 CPPUNIT_ASSERT_EQUAL( origLocale
, setlocale(LC_ALL
, NULL
) );
95 // test the ctype functions with the given locale
96 void XLocaleTestCase::TestCtypeFunctionsWith(const wxXLocale
& loc
)
98 // NOTE: here go the checks which must pass under _any_ locale "loc";
99 // checks for specific locales are in TestCtypeFunctions()
103 CPPUNIT_ASSERT( wxIsalnum_l('0', loc
) );
104 CPPUNIT_ASSERT( wxIsalnum_l('9', loc
) );
105 CPPUNIT_ASSERT( wxIsalnum_l('A', loc
) );
106 CPPUNIT_ASSERT( wxIsalnum_l('Z', loc
) );
107 CPPUNIT_ASSERT( wxIsalnum_l('a', loc
) );
108 CPPUNIT_ASSERT( wxIsalnum_l('z', loc
) );
109 CPPUNIT_ASSERT( !wxIsalnum_l('*', loc
) );
110 CPPUNIT_ASSERT( !wxIsalnum_l('@', loc
) );
111 CPPUNIT_ASSERT( !wxIsalnum_l('+', loc
) );
114 CPPUNIT_ASSERT( !wxIsalpha_l('0', loc
) );
115 CPPUNIT_ASSERT( !wxIsalpha_l('9', loc
) );
116 CPPUNIT_ASSERT( wxIsalpha_l('A', loc
) );
117 CPPUNIT_ASSERT( wxIsalpha_l('Z', loc
) );
118 CPPUNIT_ASSERT( wxIsalpha_l('a', loc
) );
119 CPPUNIT_ASSERT( wxIsalpha_l('z', loc
) );
120 CPPUNIT_ASSERT( !wxIsalpha_l('*', loc
) );
121 CPPUNIT_ASSERT( !wxIsalpha_l('@', loc
) );
122 CPPUNIT_ASSERT( !wxIsalpha_l('+', loc
) );
127 CPPUNIT_ASSERT( wxIsdigit_l('0', loc
) );
128 CPPUNIT_ASSERT( wxIsdigit_l('9', loc
) );
129 CPPUNIT_ASSERT( !wxIsdigit_l('A', loc
) );
130 CPPUNIT_ASSERT( !wxIsdigit_l('Z', loc
) );
131 CPPUNIT_ASSERT( !wxIsdigit_l('a', loc
) );
132 CPPUNIT_ASSERT( !wxIsdigit_l('z', loc
) );
137 CPPUNIT_ASSERT( !wxIslower_l('A', loc
) );
138 CPPUNIT_ASSERT( !wxIslower_l('Z', loc
) );
139 CPPUNIT_ASSERT( wxIslower_l('a', loc
) );
140 CPPUNIT_ASSERT( wxIslower_l('z', loc
) );
141 CPPUNIT_ASSERT( !wxIslower_l('0', loc
) );
142 CPPUNIT_ASSERT( !wxIslower_l('9', loc
) );
149 CPPUNIT_ASSERT( wxIsspace_l(' ', loc
) );
150 CPPUNIT_ASSERT( wxIsspace_l('\t', loc
) );
151 CPPUNIT_ASSERT( wxIsspace_l('\r', loc
) );
152 CPPUNIT_ASSERT( wxIsspace_l('\n', loc
) );
153 CPPUNIT_ASSERT( !wxIsspace_l('0', loc
) );
154 CPPUNIT_ASSERT( !wxIsspace_l('a', loc
) );
155 CPPUNIT_ASSERT( !wxIsspace_l('A', loc
) );
158 CPPUNIT_ASSERT( !wxIsupper_l('0', loc
) );
159 CPPUNIT_ASSERT( !wxIsupper_l('9', loc
) );
160 CPPUNIT_ASSERT( wxIsupper_l('A', loc
) );
161 CPPUNIT_ASSERT( wxIsupper_l('Z', loc
) );
162 CPPUNIT_ASSERT( !wxIsupper_l('a', loc
) );
163 CPPUNIT_ASSERT( !wxIsupper_l('z', loc
) );
166 CPPUNIT_ASSERT( wxIsxdigit_l('0', loc
) );
167 CPPUNIT_ASSERT( wxIsxdigit_l('9', loc
) );
168 CPPUNIT_ASSERT( wxIsxdigit_l('A', loc
) );
169 CPPUNIT_ASSERT( wxIsxdigit_l('F', loc
) );
170 CPPUNIT_ASSERT( !wxIsxdigit_l('Z', loc
) );
171 CPPUNIT_ASSERT( wxIsxdigit_l('a', loc
) );
172 CPPUNIT_ASSERT( wxIsxdigit_l('f', loc
) );
173 CPPUNIT_ASSERT( !wxIsxdigit_l('z', loc
) );
176 CPPUNIT_ASSERT_EQUAL( 'a', (char)wxTolower_l('A', loc
) );
177 CPPUNIT_ASSERT_EQUAL( 'a', (char)wxTolower_l('a', loc
) );
178 CPPUNIT_ASSERT_EQUAL( 'z', (char)wxTolower_l('Z', loc
) );
179 CPPUNIT_ASSERT_EQUAL( 'z', (char)wxTolower_l('z', loc
) );
180 CPPUNIT_ASSERT_EQUAL( '0', (char)wxTolower_l('0', loc
) );
181 CPPUNIT_ASSERT_EQUAL( '9', (char)wxTolower_l('9', loc
) );
184 CPPUNIT_ASSERT_EQUAL( 'A', (char)wxToupper_l('A', loc
) );
185 CPPUNIT_ASSERT_EQUAL( 'A', (char)wxToupper_l('a', loc
) );
186 CPPUNIT_ASSERT_EQUAL( 'Z', (char)wxToupper_l('Z', loc
) );
187 CPPUNIT_ASSERT_EQUAL( 'Z', (char)wxToupper_l('z', loc
) );
188 CPPUNIT_ASSERT_EQUAL( '0', (char)wxToupper_l('0', loc
) );
189 CPPUNIT_ASSERT_EQUAL( '9', (char)wxToupper_l('9', loc
) );
192 // test the stdlib functions with the given locale
193 void XLocaleTestCase::TestStdlibFunctionsWith(const wxXLocale
& loc
)
195 // NOTE: here go the checks which must pass under _any_ locale "loc";
196 // checks for specific locales are in TestStdlibFunctions()
204 // strtod (don't use decimal separator as it's locale-specific)
205 CPPUNIT_ASSERT_EQUAL( 0.0, wxStrtod_l(wxT("0"), NULL
, loc
) );
206 CPPUNIT_ASSERT_EQUAL( 1234.0, wxStrtod_l(wxT("1234"), NULL
, loc
) );
210 CPPUNIT_ASSERT_EQUAL( 100, wxStrtol_l(wxT("100"), NULL
, 0, loc
) );
211 CPPUNIT_ASSERT_EQUAL( 0xFF, wxStrtol_l(wxT("0xFF"), NULL
, 0, loc
) );
212 CPPUNIT_ASSERT_EQUAL( 2001, wxStrtol_l(wxT("2001 60c0c0 -1101110100110100100000 0x6fffff"), &endptr
, 10, loc
) );
213 CPPUNIT_ASSERT_EQUAL( 0x60c0c0, wxStrtol_l(endptr
, &endptr
, 16, loc
) );
214 CPPUNIT_ASSERT_EQUAL( -0x374D20, wxStrtol_l(endptr
, &endptr
, 2, loc
) );
215 CPPUNIT_ASSERT_EQUAL( 0x6fffff, wxStrtol_l(endptr
, NULL
, 0, loc
) );
218 // NOTE: 3147483647 and 0xEE6B2800 are greater than LONG_MAX (on 32bit machines) but
219 // smaller than ULONG_MAX
220 CPPUNIT_ASSERT_EQUAL( 3147483647ul, wxStrtoul_l(wxT("3147483647"), NULL
, 0, loc
) );
221 CPPUNIT_ASSERT_EQUAL( 0xEE6B2800ul
, wxStrtoul_l(wxT("0xEE6B2800"), NULL
, 0, loc
) );
223 // TODO: test for "failure" behaviour of the functions above
226 void XLocaleTestCase::TestCtypeFunctions()
228 TestCtypeFunctionsWith(wxCLocale
);
230 #ifdef wxHAS_XLOCALE_SUPPORT
234 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
235 return; // you should have french support installed to continue this test!
237 wxXLocale
locFR(wxLANGUAGE_FRENCH
);
238 CPPUNIT_ASSERT( locFR
.IsOk() ); // doesn't make sense to continue otherwise
240 TestCtypeFunctionsWith(locFR
);
242 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe9'), locFR
) );
243 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe9'), locFR
) );
244 CPPUNIT_ASSERT( !wxIslower_l(wxT('\xc9'), locFR
) );
245 CPPUNIT_ASSERT( wxIsupper_l(wxT('\xc9'), locFR
) );
246 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe7'), locFR
) );
247 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe7'), locFR
) );
248 CPPUNIT_ASSERT( wxIsupper_l(wxT('\xc7'), locFR
) );
253 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN
))
254 return; // you should have italian support installed to continue this test!
256 wxXLocale
locIT(wxLANGUAGE_ITALIAN
);
257 CPPUNIT_ASSERT( locIT
.IsOk() ); // doesn't make sense to continue otherwise
259 TestCtypeFunctionsWith(locIT
);
261 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe1'), locIT
) );
262 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe1'), locIT
) );
266 void XLocaleTestCase::TestStdlibFunctions()
268 TestStdlibFunctionsWith(wxCLocale
);
276 // strtod checks specific for C locale
278 CPPUNIT_ASSERT_EQUAL( 0.0, wxStrtod_l(wxT("0.000"), NULL
, wxCLocale
) );
279 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1.234"), NULL
, wxCLocale
) );
280 CPPUNIT_ASSERT_EQUAL( -1.234E-5, wxStrtod_l(wxT("-1.234E-5"), NULL
, wxCLocale
) );
281 CPPUNIT_ASSERT_EQUAL( 365.24, wxStrtod_l(wxT("365.24 29.53"), &endptr
, wxCLocale
) );
282 CPPUNIT_ASSERT_EQUAL( 29.53, wxStrtod_l(endptr
, NULL
, wxCLocale
) );
284 #ifdef wxHAS_XLOCALE_SUPPORT
288 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
289 return; // you should have french support installed to continue this test!
291 wxXLocale
locFR(wxLANGUAGE_FRENCH
);
292 CPPUNIT_ASSERT( locFR
.IsOk() ); // doesn't make sense to continue otherwise
294 TestCtypeFunctionsWith(locFR
);
296 // comma as decimal point:
297 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL
, locFR
) );
299 // space as thousands separator is not recognized by wxStrtod_l():
300 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1 234,5"), NULL
, locFR
) );
305 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN
))
306 return; // you should have italian support installed to continue this test!
308 wxXLocale
locIT(wxLANGUAGE_ITALIAN
);
309 CPPUNIT_ASSERT( locIT
.IsOk() ); // doesn't make sense to continue otherwise
311 TestStdlibFunctionsWith(locIT
);
313 // comma as decimal point:
314 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL
, locIT
) );
316 // dot as thousands separator is not recognized by wxStrtod_l():
317 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1.234,5"), NULL
, locIT
) );
321 #endif // wxUSE_XLOCALE