]>
git.saurik.com Git - wxWidgets.git/blob - tests/formatconverter/formatconvertertest.cpp
56b52d2af2be3fde055f34849862763a0bb1b7aa
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/formatconverter/formatconverter.cpp
3 // Purpose: Test wxFormatConverter
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows 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(wxT("%hs"), "test") == wxT("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(wxT("%d"), 255) == wxT("255"));
114 CPPUNIT_ASSERT(wxString::Format(wxT("%05d"), 255) == wxT("00255"));
115 CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), 255) == wxT(" 255"));
116 CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), -255) == wxT(" -255"));
117 CPPUNIT_ASSERT(wxString::Format(wxT("%-5d"), -255) == wxT("-255 "));
118 CPPUNIT_ASSERT(wxString::Format(wxT("%+5d"), 255) == wxT(" +255"));
119 CPPUNIT_ASSERT(wxString::Format(wxT("%*d"), 5, 255) == wxT(" 255"));
122 void FormatConverterTestCase::format_hd()
124 doTest("hd", "hd", "hd", "hd", "hd");
126 CPPUNIT_ASSERT(wxString::Format(wxT("%hd"), s
) == wxT("32767"));
129 void FormatConverterTestCase::format_ld()
131 doTest("ld", "ld", "ld", "ld", "ld");
132 long l
= 2147483647L;
133 CPPUNIT_ASSERT(wxString::Format(wxT("%ld"), l
) == wxT("2147483647"));
136 void FormatConverterTestCase::format_s()
138 doTest("s", "ls", "s", "ls", "s");
139 CPPUNIT_ASSERT(wxString::Format(wxT("%s!"), wxT("test")) == wxT("test!"));
140 CPPUNIT_ASSERT(wxString::Format(wxT("%6s!"), wxT("test")) == wxT(" test!"));
141 CPPUNIT_ASSERT(wxString::Format(wxT("%-6s!"), wxT("test")) == wxT("test !"));
142 CPPUNIT_ASSERT(wxString::Format(wxT("%.6s!"), wxT("test")) == wxT("test!"));
143 CPPUNIT_ASSERT(wxString::Format(wxT("%6.4s!"), wxT("testing")) == wxT(" test!"));
146 void FormatConverterTestCase::format_hs()
148 doTest("hs", "hs", "s", "ls", "s");
149 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hs!")), "test") == wxT("test!"));
150 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6hs!")), "test") == wxT(" test!"));
151 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-6hs!")), "test") == wxT("test !"));
152 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%.6hs!")), "test") == wxT("test!"));
153 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6.4hs!")), "testing") == wxT(" test!"));
156 void FormatConverterTestCase::format_ls()
158 doTest("ls", "ls", "s", "ls", "s");
159 CPPUNIT_ASSERT(wxString::Format(wxT("%ls!"), L
"test") == wxT("test!"));
160 CPPUNIT_ASSERT(wxString::Format(wxT("%6ls!"), L
"test") == wxT(" test!"));
161 CPPUNIT_ASSERT(wxString::Format(wxT("%-6ls!"), L
"test") == wxT("test !"));
162 CPPUNIT_ASSERT(wxString::Format(wxT("%.6ls!"), L
"test") == wxT("test!"));
163 CPPUNIT_ASSERT(wxString::Format(wxT("%6.4ls!"), L
"testing") == wxT(" test!"));
166 void FormatConverterTestCase::format_c()
168 doTest("c", "lc", "lc", "lc", "c");
169 CPPUNIT_ASSERT(wxString::Format(wxT("%c"), wxT('x')) == wxT("x"));
170 CPPUNIT_ASSERT(wxString::Format(wxT("%2c"), wxT('x')) == wxT(" x"));
171 CPPUNIT_ASSERT(wxString::Format(wxT("%-2c"), wxT('x')) == wxT("x "));
174 void FormatConverterTestCase::format_hc()
176 doTest("hc", "hc", "lc", "lc", "c");
177 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hc")), 'x') == wxT("x"));
178 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%2hc")), 'x') == wxT(" x"));
179 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-2hc")), 'x') == wxT("x "));
182 void FormatConverterTestCase::format_lc()
184 doTest("lc", "lc", "lc", "lc", "c");
185 CPPUNIT_ASSERT(wxString::Format(wxT("%lc"), L
'x') == wxT("x"));
186 CPPUNIT_ASSERT(wxString::Format(wxT("%2lc"), L
'x') == wxT(" x"));
187 CPPUNIT_ASSERT(wxString::Format(wxT("%-2lc"), L
'x') == wxT("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 { wxT(""), wxT("*"), wxT("10"), wxT("-*"), wxT("-10"), NULL
};
258 static const wxChar
*precision
[] =
259 { wxT(""), wxT(".*"), wxT(".10"), NULL
};
260 static const wxChar
*empty
[] =
263 // no precision for %c or %C
264 const wxChar
**precs
= wxTolower(input
[wxStrlen(input
)-1]) == wxT('c') ?
267 wxString
fmt(wxT("%"));
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
= wxT("input: '") + input
+
298 wxT("', result (scanf): '") + result
+
299 wxT("', expected: '") + expectedScanf
+ wxT("'");
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
= wxT("input: '") + input
+
307 wxT("', result (UTF-8): '") + result
+
308 wxT("', expected: '") + expectedUtf8
+ wxT("'");
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
= wxT("input: '") + input
+
322 wxT("', result (wchar_t): '") + result
+
323 wxT("', expected: '") + expectedWchar
+ wxT("'");
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 its own registry so that these tests can be run alone
333 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase
,
334 "FormatConverterTestCase");