]>
git.saurik.com Git - wxWidgets.git/blob - tests/formatconverter/formatconvertertest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/formatconverter/formatconverter.cpp
3 // Purpose: Test wxFormatConverter
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
13 // The conversions wxFormatConverter currently does are as follows:
20 // %hs and %hc stay the same.
22 // %S and %C aren't actually in the ISO C or C++ standards, but they can be
23 // avoided when writing portable code.
25 // Nor are %hs or %hc in the standards, which means wxWidgets currently doesn't
26 // have a specifier for 'char' types that is ok for all builds and platforms.
28 // The effect of using %hs/%hc is undefined, though RTLs are quite likely
29 // to just ignore the 'h', so maybe it works as required even though it's
32 // I've put in some checks, such as this which will flag up any platforms
33 // where this is not the case:
35 // CPPUNIT_ASSERT(wxString::Format(_T("%hs"), "test") == _T("test"));
38 // For compilers that support precompilation, includes "wx/wx.h".
45 // for all others, include the necessary headers
51 using CppUnit::TestCase
;
54 ///////////////////////////////////////////////////////////////////////////////
57 // wxFormatConverter only changes %s, %c, %S and %C, all others are treated
58 // equally, therefore it is enough to choose just one other for testing, %d
61 class FormatConverterTestCase
: public TestCase
63 CPPUNIT_TEST_SUITE(FormatConverterTestCase
);
64 CPPUNIT_TEST(format_d
);
65 CPPUNIT_TEST(format_hd
);
66 CPPUNIT_TEST(format_ld
);
67 CPPUNIT_TEST(format_s
);
68 CPPUNIT_TEST(format_hs
);
69 CPPUNIT_TEST(format_ls
);
70 CPPUNIT_TEST(format_c
);
71 CPPUNIT_TEST(format_hc
);
72 CPPUNIT_TEST(format_lc
);
73 CPPUNIT_TEST(format_S
);
74 CPPUNIT_TEST(format_hS
);
75 CPPUNIT_TEST(format_lS
);
76 CPPUNIT_TEST(format_C
);
77 CPPUNIT_TEST(format_hC
);
78 CPPUNIT_TEST(format_lC
);
79 CPPUNIT_TEST(testLonger
);
80 CPPUNIT_TEST_SUITE_END();
100 void doTest(const char *input
, const char *expectedScanf
,
101 const char *expectedUtf8
,
102 const char *expectedWcharUnix
,
103 const char *expectedWcharWindows
);
104 void check(const wxString
& input
, const wxString
& expectedScanf
,
105 const wxString
& expectedUtf8
,
106 const wxString
& expectedWcharUnix
,
107 const wxString
& expectedWcharWindows
);
110 void FormatConverterTestCase::format_d()
112 doTest("d", "d", "d", "d", "d");
113 CPPUNIT_ASSERT(wxString::Format(_T("%d"), 255) == _T("255"));
114 CPPUNIT_ASSERT(wxString::Format(_T("%05d"), 255) == _T("00255"));
115 CPPUNIT_ASSERT(wxString::Format(_T("% 5d"), 255) == _T(" 255"));
116 CPPUNIT_ASSERT(wxString::Format(_T("% 5d"), -255) == _T(" -255"));
117 CPPUNIT_ASSERT(wxString::Format(_T("%-5d"), -255) == _T("-255 "));
118 CPPUNIT_ASSERT(wxString::Format(_T("%+5d"), 255) == _T(" +255"));
119 CPPUNIT_ASSERT(wxString::Format(_T("%*d"), 5, 255) == _T(" 255"));
122 void FormatConverterTestCase::format_hd()
124 doTest("hd", "hd", "hd", "hd", "hd");
126 CPPUNIT_ASSERT(wxString::Format(_T("%hd"), s
) == _T("32767"));
129 void FormatConverterTestCase::format_ld()
131 doTest("ld", "ld", "ld", "ld", "ld");
132 long l
= 2147483647L;
133 CPPUNIT_ASSERT(wxString::Format(_T("%ld"), l
) == _T("2147483647"));
136 void FormatConverterTestCase::format_s()
138 doTest("s", "ls", "s", "ls", "s");
139 CPPUNIT_ASSERT(wxString::Format(_T("%s!"), _T("test")) == _T("test!"));
140 CPPUNIT_ASSERT(wxString::Format(_T("%6s!"), _T("test")) == _T(" test!"));
141 CPPUNIT_ASSERT(wxString::Format(_T("%-6s!"), _T("test")) == _T("test !"));
142 CPPUNIT_ASSERT(wxString::Format(_T("%.6s!"), _T("test")) == _T("test!"));
143 CPPUNIT_ASSERT(wxString::Format(_T("%6.4s!"), _T("testing")) == _T(" test!"));
146 void FormatConverterTestCase::format_hs()
148 doTest("hs", "hs", "s", "ls", "s");
149 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%hs!")), "test") == _T("test!"));
150 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%6hs!")), "test") == _T(" test!"));
151 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%-6hs!")), "test") == _T("test !"));
152 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%.6hs!")), "test") == _T("test!"));
153 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%6.4hs!")), "testing") == _T(" test!"));
156 void FormatConverterTestCase::format_ls()
158 doTest("ls", "ls", "s", "ls", "s");
159 CPPUNIT_ASSERT(wxString::Format(_T("%ls!"), L
"test") == _T("test!"));
160 CPPUNIT_ASSERT(wxString::Format(_T("%6ls!"), L
"test") == _T(" test!"));
161 CPPUNIT_ASSERT(wxString::Format(_T("%-6ls!"), L
"test") == _T("test !"));
162 CPPUNIT_ASSERT(wxString::Format(_T("%.6ls!"), L
"test") == _T("test!"));
163 CPPUNIT_ASSERT(wxString::Format(_T("%6.4ls!"), L
"testing") == _T(" test!"));
166 void FormatConverterTestCase::format_c()
168 doTest("c", "lc", "lc", "lc", "c");
169 CPPUNIT_ASSERT(wxString::Format(_T("%c"), _T('x')) == _T("x"));
170 CPPUNIT_ASSERT(wxString::Format(_T("%2c"), _T('x')) == _T(" x"));
171 CPPUNIT_ASSERT(wxString::Format(_T("%-2c"), _T('x')) == _T("x "));
174 void FormatConverterTestCase::format_hc()
176 doTest("hc", "hc", "lc", "lc", "c");
177 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%hc")), 'x') == _T("x"));
178 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%2hc")), 'x') == _T(" x"));
179 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%-2hc")), 'x') == _T("x "));
182 void FormatConverterTestCase::format_lc()
184 doTest("lc", "lc", "lc", "lc", "c");
185 CPPUNIT_ASSERT(wxString::Format(_T("%lc"), L
'x') == _T("x"));
186 CPPUNIT_ASSERT(wxString::Format(_T("%2lc"), L
'x') == _T(" x"));
187 CPPUNIT_ASSERT(wxString::Format(_T("%-2lc"), L
'x') == _T("x "));
191 void FormatConverterTestCase::format_S()
192 { doTest("S", "s", "s", "ls", "s"); }
193 void FormatConverterTestCase::format_hS()
194 { doTest("hS", "s", "s", "ls", "s"); }
195 void FormatConverterTestCase::format_lS()
196 { doTest("lS", "ls", "s", "ls", "s"); }
198 void FormatConverterTestCase::format_C()
199 { doTest("C", "c", "lc", "lc", "c"); }
200 void FormatConverterTestCase::format_hC()
201 { doTest("hC", "c", "lc", "lc", "c"); }
202 void FormatConverterTestCase::format_lC()
203 { doTest("lC", "lc", "lc", "lc", "c"); }
205 // It's possible that although a format converts correctly alone, it leaves
206 // the converter in a bad state that will affect subsequent formats, so
207 // check with a selection of longer patterns.
209 void FormatConverterTestCase::testLonger()
213 const char *expectedScanf
;
214 const char *expectedWcharUnix
;
215 const char *expectedWcharWindows
;
216 const char *expectedUtf8
;
218 { "%d", "%d", "%d", "%d", "%d" },
219 { "%*hd", "%*hd", "%*hd", "%*hd", "%*hd" },
220 { "%.4ld", "%.4ld", "%.4ld", "%.4ld", "%.4ld" },
221 { "%-.*s", "%-.*ls", "%-.*ls", "%-.*s", "%-.*s" },
222 { "%.*hs", "%.*hs", "%.*ls", "%.*s", "%.*s" },
223 { "%-.9ls", "%-.9ls", "%-.9ls", "%-.9s", "%-.9s" },
224 { "%-*c", "%-*lc", "%-*lc", "%-*c", "%-*lc" },
225 { "%3hc", "%3hc", "%3lc", "%3c", "%3lc" },
226 { "%-5lc", "%-5lc", "%-5lc", "%-5c", "%-5lc" }
230 // test all possible pairs of the above patterns
231 for (i
= 0; i
< WXSIZEOF(formats
); i
++) {
232 if (formats
[i
].input
) {
233 wxString
input(formats
[i
].input
);
234 wxString
expectedScanf(formats
[i
].expectedScanf
);
235 wxString
expectedUtf8(formats
[i
].expectedUtf8
);
236 wxString
expectedWcharUnix(formats
[i
].expectedWcharUnix
);
237 wxString
expectedWcharWindows(formats
[i
].expectedWcharWindows
);
239 for (j
= 0; j
< WXSIZEOF(formats
); j
++)
240 if (formats
[j
].input
)
241 check(input
+ formats
[j
].input
,
242 expectedScanf
+ formats
[j
].expectedScanf
,
243 expectedUtf8
+ formats
[j
].expectedUtf8
,
244 expectedWcharUnix
+ formats
[j
].expectedWcharUnix
,
245 expectedWcharWindows
+ formats
[j
].expectedWcharWindows
);
250 void FormatConverterTestCase::doTest(const char *input
,
251 const char *expectedScanf
,
252 const char *expectedUtf8
,
253 const char *expectedWcharUnix
,
254 const char *expectedWcharWindows
)
256 static const wxChar
*flag_width
[] =
257 { _T(""), _T("*"), _T("10"), _T("-*"), _T("-10"), NULL
};
258 static const wxChar
*precision
[] =
259 { _T(""), _T(".*"), _T(".10"), NULL
};
260 static const wxChar
*empty
[] =
263 // no precision for %c or %C
264 const wxChar
**precs
= wxTolower(input
[wxStrlen(input
)-1]) == _T('c') ?
267 wxString
fmt(_T("%"));
269 // try the test for a variety of combinations of flag, width and precision
270 for (const wxChar
**prec
= precs
; *prec
; prec
++)
271 for (const wxChar
**width
= flag_width
; *width
; width
++)
272 check(fmt
+ *width
+ *prec
+ input
,
273 fmt
+ *width
+ *prec
+ expectedScanf
,
274 fmt
+ *width
+ *prec
+ expectedUtf8
,
275 fmt
+ *width
+ *prec
+ expectedWcharUnix
,
276 fmt
+ *width
+ *prec
+ expectedWcharWindows
);
279 void FormatConverterTestCase::check(const wxString
& input
,
280 const wxString
& expectedScanf
,
281 const wxString
& expectedUtf8
,
282 const wxString
& expectedWcharUnix
,
283 const wxString
& expectedWcharWindows
)
285 // all of them are unused in some build configurations
286 wxUnusedVar(expectedScanf
);
287 wxUnusedVar(expectedUtf8
);
288 wxUnusedVar(expectedWcharUnix
);
289 wxUnusedVar(expectedWcharWindows
);
291 wxString result
, msg
;
294 // on windows, wxScanf() string needs no modifications
295 result
= wxScanfConvertFormatW(input
.wc_str());
297 msg
= _T("input: '") + input
+
298 _T("', result (scanf): '") + result
+
299 _T("', expected: '") + expectedScanf
+ _T("'");
300 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedScanf
);
301 #endif // !__WINDOWS__
303 #if wxUSE_UNICODE_UTF8
304 result
= (const char*)wxFormatString(input
);
306 msg
= _T("input: '") + input
+
307 _T("', result (UTF-8): '") + result
+
308 _T("', expected: '") + expectedUtf8
+ _T("'");
309 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedUtf8
);
310 #endif // wxUSE_UNICODE_UTF8
312 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
313 result
= (const wchar_t*)wxFormatString(input
);
316 wxString
expectedWchar(expectedWcharWindows
);
318 wxString
expectedWchar(expectedWcharUnix
);
321 msg
= _T("input: '") + input
+
322 _T("', result (wchar_t): '") + result
+
323 _T("', expected: '") + expectedWchar
+ _T("'");
324 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedWchar
);
325 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
329 // register in the unnamed registry so that these tests are run by default
330 CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase
);
332 // also include in it's own registry so that these tests can be run alone
333 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase
,
334 "FormatConverterTestCase");