]>
git.saurik.com Git - wxWidgets.git/blob - tests/formatconverter/formatconvertertest.cpp
4ad9180a0bbda03a792c412d6b59cf163f432629
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/formatconverter/formatconverter.cpp
3 // Purpose: Test wxFormatConverter
4 // Author: Mike Wetherell
5 // Copyright: (c) 2004 Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
12 // The conversions wxFormatConverter currently does are as follows:
19 // %hs and %hc stay the same.
21 // %S and %C aren't actually in the ISO C or C++ standards, but they can be
22 // avoided when writing portable code.
24 // Nor are %hs or %hc in the standards, which means wxWidgets currently doesn't
25 // have a specifier for 'char' types that is ok for all builds and platforms.
27 // The effect of using %hs/%hc is undefined, though RTLs are quite likely
28 // to just ignore the 'h', so maybe it works as required even though it's
31 // I've put in some checks, such as this which will flag up any platforms
32 // where this is not the case:
34 // CPPUNIT_ASSERT(wxString::Format(wxT("%hs"), "test") == wxT("test"));
37 // For compilers that support precompilation, includes "wx/wx.h".
44 // for all others, include the necessary headers
50 using CppUnit::TestCase
;
53 ///////////////////////////////////////////////////////////////////////////////
56 // wxFormatConverter only changes %s, %c, %S and %C, all others are treated
57 // equally, therefore it is enough to choose just one other for testing, %d
60 class FormatConverterTestCase
: public TestCase
62 CPPUNIT_TEST_SUITE(FormatConverterTestCase
);
63 CPPUNIT_TEST(format_d
);
64 CPPUNIT_TEST(format_hd
);
65 CPPUNIT_TEST(format_ld
);
66 CPPUNIT_TEST(format_s
);
67 CPPUNIT_TEST(format_hs
);
68 CPPUNIT_TEST(format_ls
);
69 CPPUNIT_TEST(format_c
);
70 CPPUNIT_TEST(format_hc
);
71 CPPUNIT_TEST(format_lc
);
72 CPPUNIT_TEST(format_S
);
73 CPPUNIT_TEST(format_hS
);
74 CPPUNIT_TEST(format_lS
);
75 CPPUNIT_TEST(format_C
);
76 CPPUNIT_TEST(format_hC
);
77 CPPUNIT_TEST(format_lC
);
78 CPPUNIT_TEST(testLonger
);
79 CPPUNIT_TEST_SUITE_END();
99 void doTest(const char *input
, const char *expectedScanf
,
100 const char *expectedUtf8
,
101 const char *expectedWcharUnix
,
102 const char *expectedWcharWindows
);
103 void check(const wxString
& input
, const wxString
& expectedScanf
,
104 const wxString
& expectedUtf8
,
105 const wxString
& expectedWcharUnix
,
106 const wxString
& expectedWcharWindows
);
109 void FormatConverterTestCase::format_d()
111 doTest("d", "d", "d", "d", "d");
112 CPPUNIT_ASSERT(wxString::Format(wxT("%d"), 255) == wxT("255"));
113 CPPUNIT_ASSERT(wxString::Format(wxT("%05d"), 255) == wxT("00255"));
114 CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), 255) == wxT(" 255"));
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("%*d"), 5, 255) == wxT(" 255"));
121 void FormatConverterTestCase::format_hd()
123 doTest("hd", "hd", "hd", "hd", "hd");
125 CPPUNIT_ASSERT(wxString::Format(wxT("%hd"), s
) == wxT("32767"));
128 void FormatConverterTestCase::format_ld()
130 doTest("ld", "ld", "ld", "ld", "ld");
131 long l
= 2147483647L;
132 CPPUNIT_ASSERT(wxString::Format(wxT("%ld"), l
) == wxT("2147483647"));
135 void FormatConverterTestCase::format_s()
137 doTest("s", "ls", "s", "ls", "s");
138 CPPUNIT_ASSERT(wxString::Format(wxT("%s!"), wxT("test")) == wxT("test!"));
139 CPPUNIT_ASSERT(wxString::Format(wxT("%6s!"), 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("%6.4s!"), wxT("testing")) == wxT(" test!"));
145 void FormatConverterTestCase::format_hs()
147 doTest("hs", "hs", "s", "ls", "s");
148 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hs!")), "test") == wxT("test!"));
149 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6hs!")), "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("%6.4hs!")), "testing") == wxT(" test!"));
155 void FormatConverterTestCase::format_ls()
157 doTest("ls", "ls", "s", "ls", "s");
158 CPPUNIT_ASSERT(wxString::Format(wxT("%ls!"), L
"test") == wxT("test!"));
159 CPPUNIT_ASSERT(wxString::Format(wxT("%6ls!"), 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("%6.4ls!"), L
"testing") == wxT(" test!"));
165 void FormatConverterTestCase::format_c()
167 doTest("c", "lc", "lc", "lc", "c");
168 CPPUNIT_ASSERT(wxString::Format(wxT("%c"), wxT('x')) == wxT("x"));
169 CPPUNIT_ASSERT(wxString::Format(wxT("%2c"), wxT('x')) == wxT(" x"));
170 CPPUNIT_ASSERT(wxString::Format(wxT("%-2c"), wxT('x')) == wxT("x "));
173 void FormatConverterTestCase::format_hc()
175 doTest("hc", "hc", "lc", "lc", "c");
176 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hc")), 'x') == wxT("x"));
177 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%2hc")), 'x') == wxT(" x"));
178 CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-2hc")), 'x') == wxT("x "));
181 void FormatConverterTestCase::format_lc()
183 doTest("lc", "lc", "lc", "lc", "c");
184 CPPUNIT_ASSERT(wxString::Format(wxT("%lc"), L
'x') == wxT("x"));
185 CPPUNIT_ASSERT(wxString::Format(wxT("%2lc"), L
'x') == wxT(" x"));
186 CPPUNIT_ASSERT(wxString::Format(wxT("%-2lc"), L
'x') == wxT("x "));
190 void FormatConverterTestCase::format_S()
191 { doTest("S", "s", "s", "ls", "s"); }
192 void FormatConverterTestCase::format_hS()
193 { doTest("hS", "s", "s", "ls", "s"); }
194 void FormatConverterTestCase::format_lS()
195 { doTest("lS", "ls", "s", "ls", "s"); }
197 void FormatConverterTestCase::format_C()
198 { doTest("C", "c", "lc", "lc", "c"); }
199 void FormatConverterTestCase::format_hC()
200 { doTest("hC", "c", "lc", "lc", "c"); }
201 void FormatConverterTestCase::format_lC()
202 { doTest("lC", "lc", "lc", "lc", "c"); }
204 // It's possible that although a format converts correctly alone, it leaves
205 // the converter in a bad state that will affect subsequent formats, so
206 // check with a selection of longer patterns.
208 void FormatConverterTestCase::testLonger()
212 const char *expectedScanf
;
213 const char *expectedWcharUnix
;
214 const char *expectedWcharWindows
;
215 const char *expectedUtf8
;
217 { "%d", "%d", "%d", "%d", "%d" },
218 { "%*hd", "%*hd", "%*hd", "%*hd", "%*hd" },
219 { "%.4ld", "%.4ld", "%.4ld", "%.4ld", "%.4ld" },
220 { "%-.*s", "%-.*ls", "%-.*ls", "%-.*s", "%-.*s" },
221 { "%.*hs", "%.*hs", "%.*ls", "%.*s", "%.*s" },
222 { "%-.9ls", "%-.9ls", "%-.9ls", "%-.9s", "%-.9s" },
223 { "%-*c", "%-*lc", "%-*lc", "%-*c", "%-*lc" },
224 { "%3hc", "%3hc", "%3lc", "%3c", "%3lc" },
225 { "%-5lc", "%-5lc", "%-5lc", "%-5c", "%-5lc" }
229 // test all possible pairs of the above patterns
230 for (i
= 0; i
< WXSIZEOF(formats
); i
++) {
231 if (formats
[i
].input
) {
232 wxString
input(formats
[i
].input
);
233 wxString
expectedScanf(formats
[i
].expectedScanf
);
234 wxString
expectedUtf8(formats
[i
].expectedUtf8
);
235 wxString
expectedWcharUnix(formats
[i
].expectedWcharUnix
);
236 wxString
expectedWcharWindows(formats
[i
].expectedWcharWindows
);
238 for (j
= 0; j
< WXSIZEOF(formats
); j
++)
239 if (formats
[j
].input
)
240 check(input
+ formats
[j
].input
,
241 expectedScanf
+ formats
[j
].expectedScanf
,
242 expectedUtf8
+ formats
[j
].expectedUtf8
,
243 expectedWcharUnix
+ formats
[j
].expectedWcharUnix
,
244 expectedWcharWindows
+ formats
[j
].expectedWcharWindows
);
249 void FormatConverterTestCase::doTest(const char *input
,
250 const char *expectedScanf
,
251 const char *expectedUtf8
,
252 const char *expectedWcharUnix
,
253 const char *expectedWcharWindows
)
255 static const wxChar
*flag_width
[] =
256 { wxT(""), wxT("*"), wxT("10"), wxT("-*"), wxT("-10"), NULL
};
257 static const wxChar
*precision
[] =
258 { wxT(""), wxT(".*"), wxT(".10"), NULL
};
259 static const wxChar
*empty
[] =
262 // no precision for %c or %C
263 const wxChar
**precs
= wxTolower(input
[wxStrlen(input
)-1]) == wxT('c') ?
266 wxString
fmt(wxT("%"));
268 // try the test for a variety of combinations of flag, width and precision
269 for (const wxChar
**prec
= precs
; *prec
; prec
++)
270 for (const wxChar
**width
= flag_width
; *width
; width
++)
271 check(fmt
+ *width
+ *prec
+ input
,
272 fmt
+ *width
+ *prec
+ expectedScanf
,
273 fmt
+ *width
+ *prec
+ expectedUtf8
,
274 fmt
+ *width
+ *prec
+ expectedWcharUnix
,
275 fmt
+ *width
+ *prec
+ expectedWcharWindows
);
278 void FormatConverterTestCase::check(const wxString
& input
,
279 const wxString
& expectedScanf
,
280 const wxString
& expectedUtf8
,
281 const wxString
& expectedWcharUnix
,
282 const wxString
& expectedWcharWindows
)
284 // all of them are unused in some build configurations
285 wxUnusedVar(expectedScanf
);
286 wxUnusedVar(expectedUtf8
);
287 wxUnusedVar(expectedWcharUnix
);
288 wxUnusedVar(expectedWcharWindows
);
290 wxString result
, msg
;
293 // on windows, wxScanf() string needs no modifications
294 result
= wxScanfConvertFormatW(input
.wc_str());
296 msg
= wxT("input: '") + input
+
297 wxT("', result (scanf): '") + result
+
298 wxT("', expected: '") + expectedScanf
+ wxT("'");
299 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedScanf
);
300 #endif // !__WINDOWS__
302 #if wxUSE_UNICODE_UTF8
303 result
= (const char*)wxFormatString(input
);
305 msg
= wxT("input: '") + input
+
306 wxT("', result (UTF-8): '") + result
+
307 wxT("', expected: '") + expectedUtf8
+ wxT("'");
308 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedUtf8
);
309 #endif // wxUSE_UNICODE_UTF8
311 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
312 result
= (const wchar_t*)wxFormatString(input
);
315 wxString
expectedWchar(expectedWcharWindows
);
317 wxString
expectedWchar(expectedWcharUnix
);
320 msg
= wxT("input: '") + input
+
321 wxT("', result (wchar_t): '") + result
+
322 wxT("', expected: '") + expectedWchar
+ wxT("'");
323 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expectedWchar
);
324 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
328 // register in the unnamed registry so that these tests are run by default
329 CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase
);
331 // also include in its own registry so that these tests can be run alone
332 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase
,
333 "FormatConverterTestCase");