]>
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
7 // Copyright: (c) 2008 Brian Vanderburg II
8 // 2008 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
28 #include "wx/xlocale.h"
30 // --------------------------------------------------------------------------
32 // --------------------------------------------------------------------------
34 class XLocaleTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( XLocaleTestCase
);
41 CPPUNIT_TEST( TestCtor
);
42 CPPUNIT_TEST( TestCtypeFunctions
);
43 CPPUNIT_TEST( TestStdlibFunctions
);
44 CPPUNIT_TEST_SUITE_END();
47 void TestCtypeFunctions();
48 void TestStdlibFunctions();
50 void TestCtypeFunctionsWith(const wxXLocale
& loc
);
51 void TestStdlibFunctionsWith(const wxXLocale
& loc
);
53 DECLARE_NO_COPY_CLASS(XLocaleTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( XLocaleTestCase
);
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XLocaleTestCase
, "XLocaleTestCase" );
63 // test the different wxXLocale ctors
64 void XLocaleTestCase::TestCtor()
66 CPPUNIT_ASSERT( !wxXLocale().IsOk() );
67 CPPUNIT_ASSERT( wxCLocale
.IsOk() );
68 CPPUNIT_ASSERT( wxXLocale("C").IsOk() );
69 CPPUNIT_ASSERT( !wxXLocale("bloordyblop").IsOk() );
71 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
72 return; // you should have french support installed to continue this test!
74 #ifdef wxHAS_XLOCALE_SUPPORT
75 CPPUNIT_ASSERT( wxXLocale(wxLANGUAGE_FRENCH
).IsOk() );
77 CPPUNIT_ASSERT( wxXLocale("french").IsOk() );
79 CPPUNIT_ASSERT( wxXLocale("fr_FR").IsOk() );
84 // test the ctype functions with the given locale
85 void XLocaleTestCase::TestCtypeFunctionsWith(const wxXLocale
& loc
)
87 // NOTE: here go the checks which must pass under _any_ locale "loc";
88 // checks for specific locales are in TestCtypeFunctions()
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
) );
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
) );
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
) );
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
) );
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
) );
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
) );
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
) );
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
) );
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
) );
181 // test the stdlib functions with the given locale
182 void XLocaleTestCase::TestStdlibFunctionsWith(const wxXLocale
& loc
)
184 // NOTE: here go the checks which must pass under _any_ locale "loc";
185 // checks for specific locales are in TestStdlibFunctions()
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
) );
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
) );
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
) );
212 // TODO: test for "failure" behaviour of the functions above
215 void XLocaleTestCase::TestCtypeFunctions()
217 TestCtypeFunctionsWith(wxCLocale
);
219 #ifdef wxHAS_XLOCALE_SUPPORT
223 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
224 return; // you should have french support installed to continue this test!
226 wxXLocale
locFR(wxLANGUAGE_FRENCH
);
227 CPPUNIT_ASSERT( locFR
.IsOk() ); // doesn't make sense to continue otherwise
229 TestCtypeFunctionsWith(locFR
);
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
) );
242 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN
))
243 return; // you should have italian support installed to continue this test!
245 wxXLocale
locIT(wxLANGUAGE_ITALIAN
);
246 CPPUNIT_ASSERT( locIT
.IsOk() ); // doesn't make sense to continue otherwise
248 TestCtypeFunctionsWith(locIT
);
250 CPPUNIT_ASSERT( wxIsalpha_l(wxT('\xe1'), locIT
) );
251 CPPUNIT_ASSERT( wxIslower_l(wxT('\xe1'), locIT
) );
255 void XLocaleTestCase::TestStdlibFunctions()
257 TestStdlibFunctionsWith(wxCLocale
);
265 // strtod checks specific for C locale
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
) );
273 #ifdef wxHAS_XLOCALE_SUPPORT
277 if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH
))
278 return; // you should have french support installed to continue this test!
280 wxXLocale
locFR(wxLANGUAGE_FRENCH
);
281 CPPUNIT_ASSERT( locFR
.IsOk() ); // doesn't make sense to continue otherwise
283 TestCtypeFunctionsWith(locFR
);
285 // comma as decimal point:
286 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL
, locFR
) );
288 // space as thousands separator is not recognized by wxStrtod_l():
289 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1 234,5"), NULL
, locFR
) );
294 if (!wxLocale::IsAvailable(wxLANGUAGE_ITALIAN
))
295 return; // you should have italian support installed to continue this test!
297 wxXLocale
locIT(wxLANGUAGE_ITALIAN
);
298 CPPUNIT_ASSERT( locIT
.IsOk() ); // doesn't make sense to continue otherwise
300 TestStdlibFunctionsWith(locIT
);
302 // comma as decimal point:
303 CPPUNIT_ASSERT_EQUAL( 1.234, wxStrtod_l(wxT("1,234"), NULL
, locIT
) );
305 // dot as thousands separator is not recognized by wxStrtod_l():
306 CPPUNIT_ASSERT( 1234.5 != wxStrtod_l(wxT("1.234,5"), NULL
, locIT
) );
310 #endif // wxUSE_XLOCALE