]>
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".
39 #include "wx/wxprec.h"
45 // for all others, include the necessary headers
50 #include "wx/cppunit.h"
52 // wxFormatConverter can only be tested in a Unicode non-Windows debug build
54 #if defined(wxNEED_PRINTF_CONVERSION) && defined(__WXDEBUG__)
56 extern wxString
wxConvertFormat(const wxChar
*format
);
59 using CppUnit::TestCase
;
62 ///////////////////////////////////////////////////////////////////////////////
65 // wxFormatConverter only changes %s, %c, %S and %C, all others are treated
66 // equally, therefore it is enough to choose just one other for testing, %d
69 class FormatConverterTestCase
: public TestCase
71 CPPUNIT_TEST_SUITE(FormatConverterTestCase
);
72 CPPUNIT_TEST(format_d
);
73 CPPUNIT_TEST(format_hd
);
74 CPPUNIT_TEST(format_ld
);
75 CPPUNIT_TEST(format_s
);
76 CPPUNIT_TEST(format_hs
);
77 CPPUNIT_TEST(format_ls
);
78 CPPUNIT_TEST(format_c
);
79 CPPUNIT_TEST(format_hc
);
80 CPPUNIT_TEST(format_lc
);
82 CPPUNIT_TEST(format_S
);
83 CPPUNIT_TEST(format_hS
);
84 CPPUNIT_TEST(format_lS
);
85 CPPUNIT_TEST(format_C
);
86 CPPUNIT_TEST(format_hC
);
87 CPPUNIT_TEST(format_lC
);
88 CPPUNIT_TEST(testLonger
);
90 CPPUNIT_TEST_SUITE_END();
111 void doTest(const wxChar
*input
, const wxChar
*expected
);
112 void check(const wxString
& input
, const wxString
& expected
);
116 void FormatConverterTestCase::format_d()
119 doTest(_T("d"), _T("d"));
121 CPPUNIT_ASSERT(wxString::Format(_T("%d"), 255) == _T("255"));
122 CPPUNIT_ASSERT(wxString::Format(_T("%05d"), 255) == _T("00255"));
123 CPPUNIT_ASSERT(wxString::Format(_T("% 5d"), 255) == _T(" 255"));
124 CPPUNIT_ASSERT(wxString::Format(_T("% 5d"), -255) == _T(" -255"));
125 CPPUNIT_ASSERT(wxString::Format(_T("%-5d"), -255) == _T("-255 "));
126 CPPUNIT_ASSERT(wxString::Format(_T("%+5d"), 255) == _T(" +255"));
127 CPPUNIT_ASSERT(wxString::Format(_T("%*d"), 5, 255) == _T(" 255"));
130 void FormatConverterTestCase::format_hd()
133 doTest(_T("hd"), _T("hd"));
136 CPPUNIT_ASSERT(wxString::Format(_T("%hd"), s
) == _T("32767"));
139 void FormatConverterTestCase::format_ld()
142 doTest(_T("ld"), _T("ld"));
144 long l
= 2147483647L;
145 CPPUNIT_ASSERT(wxString::Format(_T("%ld"), l
) == _T("2147483647"));
148 void FormatConverterTestCase::format_s()
151 doTest(_T("s"), _T("ls"));
153 CPPUNIT_ASSERT(wxString::Format(_T("%s!"), _T("test")) == _T("test!"));
154 CPPUNIT_ASSERT(wxString::Format(_T("%6s!"), _T("test")) == _T(" test!"));
155 CPPUNIT_ASSERT(wxString::Format(_T("%-6s!"), _T("test")) == _T("test !"));
156 CPPUNIT_ASSERT(wxString::Format(_T("%.6s!"), _T("test")) == _T("test!"));
157 CPPUNIT_ASSERT(wxString::Format(_T("%6.4s!"), _T("testing")) == _T(" test!"));
160 void FormatConverterTestCase::format_hs()
163 doTest(_T("hs"), _T("hs"));
165 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%hs!")), "test") == _T("test!"));
166 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%6hs!")), "test") == _T(" test!"));
167 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%-6hs!")), "test") == _T("test !"));
168 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%.6hs!")), "test") == _T("test!"));
169 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%6.4hs!")), "testing") == _T(" test!"));
172 void FormatConverterTestCase::format_ls()
175 doTest(_T("ls"), _T("ls"));
177 CPPUNIT_ASSERT(wxString::Format(_T("%ls!"), L
"test") == _T("test!"));
178 CPPUNIT_ASSERT(wxString::Format(_T("%6ls!"), L
"test") == _T(" test!"));
179 CPPUNIT_ASSERT(wxString::Format(_T("%-6ls!"), L
"test") == _T("test !"));
180 CPPUNIT_ASSERT(wxString::Format(_T("%.6ls!"), L
"test") == _T("test!"));
181 CPPUNIT_ASSERT(wxString::Format(_T("%6.4ls!"), L
"testing") == _T(" test!"));
184 void FormatConverterTestCase::format_c()
187 doTest(_T("c"), _T("lc"));
189 CPPUNIT_ASSERT(wxString::Format(_T("%c"), _T('x')) == _T("x"));
190 CPPUNIT_ASSERT(wxString::Format(_T("%2c"), _T('x')) == _T(" x"));
191 CPPUNIT_ASSERT(wxString::Format(_T("%-2c"), _T('x')) == _T("x "));
194 void FormatConverterTestCase::format_hc()
197 doTest(_T("hc"), _T("hc"));
199 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%hc")), 'x') == _T("x"));
200 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%2hc")), 'x') == _T(" x"));
201 CPPUNIT_ASSERT(wxString::Format(wxString(_T("%-2hc")), 'x') == _T("x "));
204 void FormatConverterTestCase::format_lc()
207 doTest(_T("lc"), _T("lc"));
209 CPPUNIT_ASSERT(wxString::Format(_T("%lc"), L
'x') == _T("x"));
210 CPPUNIT_ASSERT(wxString::Format(_T("%2lc"), L
'x') == _T(" x"));
211 CPPUNIT_ASSERT(wxString::Format(_T("%-2lc"), L
'x') == _T("x "));
216 void FormatConverterTestCase::format_S() { doTest(_T("S"), _T("s")); }
217 void FormatConverterTestCase::format_hS() { doTest(_T("hS"), _T("s")); }
218 void FormatConverterTestCase::format_lS() { doTest(_T("lS"), _T("ls")); }
220 void FormatConverterTestCase::format_C() { doTest(_T("C"), _T("c")); }
221 void FormatConverterTestCase::format_hC() { doTest(_T("hC"), _T("c")); }
222 void FormatConverterTestCase::format_lC() { doTest(_T("lC"), _T("lc")); }
224 // It's possible that although a format converts correctly alone, it leaves
225 // the converter in a bad state that will affect subsequent formats, so
226 // check with a selection of longer patterns.
228 void FormatConverterTestCase::testLonger()
232 const wxChar
*expected
;
234 { _T("%d"), _T("%d"), },
235 { _T("%*hd"), _T("%*hd") },
236 { _T("%.4ld"), _T("%.4ld") },
237 { _T("%-.*s"), _T("%-.*ls") },
238 { _T("%.*hs"), _T("%.*hs"), },
239 { _T("%-.9ls"), _T("%-.9ls") },
240 { _T("%-*c"), _T("%-*lc") },
241 { _T("%3hc"), _T("%3hc") },
242 { _T("%-5lc"), _T("%-5lc") }
246 // exclude patterns that don't translate correctly alone from the test
247 for (i
= 0; i
< WXSIZEOF(formats
); i
++)
248 if (wxConvertFormat(formats
[i
].input
) != formats
[i
].expected
)
249 formats
[i
].input
= NULL
;
251 // test all possible pairs of the above patterns
252 for (i
= 0; i
< WXSIZEOF(formats
); i
++) {
253 if (formats
[i
].input
) {
254 wxString
input(formats
[i
].input
);
255 wxString
expected(formats
[i
].expected
);
257 for (j
= 0; j
< WXSIZEOF(formats
); j
++)
258 if (formats
[j
].input
)
259 check(input
+ formats
[j
].input
,
260 expected
+ formats
[j
].expected
);
265 void FormatConverterTestCase::doTest(const wxChar
*input
,
266 const wxChar
*expected
)
268 static const wxChar
*flag_width
[] =
269 { _T(""), _T("*"), _T("10"), _T("-*"), _T("-10"), NULL
};
270 static const wxChar
*precision
[] =
271 { _T(""), _T(".*"), _T(".10"), NULL
};
272 static const wxChar
*empty
[] =
275 // no precision for %c or %C
276 const wxChar
**precs
= wxTolower(input
[wxStrlen(input
)-1]) == _T('c') ?
279 wxString
fmt(_T("%"));
281 // try the test for a variety of combinations of flag, width and precision
282 for (const wxChar
**prec
= precs
; *prec
; prec
++)
283 for (const wxChar
**width
= flag_width
; *width
; width
++)
284 check(fmt
+ *width
+ *prec
+ input
,
285 fmt
+ *width
+ *prec
+ expected
);
288 void FormatConverterTestCase::check(const wxString
& input
,
289 const wxString
& expected
)
291 wxString result
= wxConvertFormat(input
);
292 wxString msg
= _T("input: '") + input
+
293 _T("', result: '") + result
+
294 _T("', expected: '") + expected
+ _T("'");
295 CPPUNIT_ASSERT_MESSAGE(string(msg
.mb_str()), result
== expected
);
300 // register in the unnamed registry so that these tests are run by default
301 CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase
);
303 // also include in it's own registry so that these tests can be run alone
304 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase
,
305 "FormatConverterTestCase");