]> git.saurik.com Git - wxWidgets.git/blob - tests/formatconverter/formatconvertertest.cpp
4ad9180a0bbda03a792c412d6b59cf163f432629
[wxWidgets.git] / tests / formatconverter / formatconvertertest.cpp
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 ///////////////////////////////////////////////////////////////////////////////
8
9 //
10 // Notes:
11 //
12 // The conversions wxFormatConverter currently does are as follows:
13 //
14 // %s, %lS -> %ls
15 // %S, %hS -> %s
16 // %c, %lC -> %lc
17 // %C, %hC -> %c
18 //
19 // %hs and %hc stay the same.
20 //
21 // %S and %C aren't actually in the ISO C or C++ standards, but they can be
22 // avoided when writing portable code.
23 //
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.
26 //
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
29 // not legal.
30 //
31 // I've put in some checks, such as this which will flag up any platforms
32 // where this is not the case:
33 //
34 // CPPUNIT_ASSERT(wxString::Format(wxT("%hs"), "test") == wxT("test"));
35 //
36
37 // For compilers that support precompilation, includes "wx/wx.h".
38 #include "testprec.h"
39
40 #ifdef __BORLANDC__
41 #pragma hdrstop
42 #endif
43
44 // for all others, include the necessary headers
45 #ifndef WX_PRECOMP
46 #include "wx/wx.h"
47 #endif
48
49
50 using CppUnit::TestCase;
51 using std::string;
52
53 ///////////////////////////////////////////////////////////////////////////////
54 // The test case
55 //
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
58 // will do.
59
60 class FormatConverterTestCase : public TestCase
61 {
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();
80
81 void format_d();
82 void format_hd();
83 void format_ld();
84 void format_s();
85 void format_hs();
86 void format_ls();
87 void format_c();
88 void format_hc();
89 void format_lc();
90
91 void format_S();
92 void format_hS();
93 void format_lS();
94 void format_C();
95 void format_hC();
96 void format_lC();
97 void testLonger();
98
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);
107 };
108
109 void FormatConverterTestCase::format_d()
110 {
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"));
119 }
120
121 void FormatConverterTestCase::format_hd()
122 {
123 doTest("hd", "hd", "hd", "hd", "hd");
124 short s = 32767;
125 CPPUNIT_ASSERT(wxString::Format(wxT("%hd"), s) == wxT("32767"));
126 }
127
128 void FormatConverterTestCase::format_ld()
129 {
130 doTest("ld", "ld", "ld", "ld", "ld");
131 long l = 2147483647L;
132 CPPUNIT_ASSERT(wxString::Format(wxT("%ld"), l) == wxT("2147483647"));
133 }
134
135 void FormatConverterTestCase::format_s()
136 {
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!"));
143 }
144
145 void FormatConverterTestCase::format_hs()
146 {
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!"));
153 }
154
155 void FormatConverterTestCase::format_ls()
156 {
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!"));
163 }
164
165 void FormatConverterTestCase::format_c()
166 {
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 "));
171 }
172
173 void FormatConverterTestCase::format_hc()
174 {
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 "));
179 }
180
181 void FormatConverterTestCase::format_lc()
182 {
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 "));
187 }
188
189
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"); }
196
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"); }
203
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.
207 //
208 void FormatConverterTestCase::testLonger()
209 {
210 struct {
211 const char *input;
212 const char *expectedScanf;
213 const char *expectedWcharUnix;
214 const char *expectedWcharWindows;
215 const char *expectedUtf8;
216 } formats[] = {
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" }
226 };
227 size_t i, j;
228
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);
237
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);
245 }
246 }
247 }
248
249 void FormatConverterTestCase::doTest(const char *input,
250 const char *expectedScanf,
251 const char *expectedUtf8,
252 const char *expectedWcharUnix,
253 const char *expectedWcharWindows)
254 {
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[] =
260 { wxT(""), NULL };
261
262 // no precision for %c or %C
263 const wxChar **precs = wxTolower(input[wxStrlen(input)-1]) == wxT('c') ?
264 empty : precision;
265
266 wxString fmt(wxT("%"));
267
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);
276 }
277
278 void FormatConverterTestCase::check(const wxString& input,
279 const wxString& expectedScanf,
280 const wxString& expectedUtf8,
281 const wxString& expectedWcharUnix,
282 const wxString& expectedWcharWindows)
283 {
284 // all of them are unused in some build configurations
285 wxUnusedVar(expectedScanf);
286 wxUnusedVar(expectedUtf8);
287 wxUnusedVar(expectedWcharUnix);
288 wxUnusedVar(expectedWcharWindows);
289
290 wxString result, msg;
291
292 #ifndef __WINDOWS__
293 // on windows, wxScanf() string needs no modifications
294 result = wxScanfConvertFormatW(input.wc_str());
295
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__
301
302 #if wxUSE_UNICODE_UTF8
303 result = (const char*)wxFormatString(input);
304
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
310
311 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
312 result = (const wchar_t*)wxFormatString(input);
313
314 #ifdef __WINDOWS__
315 wxString expectedWchar(expectedWcharWindows);
316 #else
317 wxString expectedWchar(expectedWcharUnix);
318 #endif
319
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
325 }
326
327
328 // register in the unnamed registry so that these tests are run by default
329 CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase);
330
331 // also include in its own registry so that these tests can be run alone
332 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase,
333 "FormatConverterTestCase");
334