Upstream tarball 10158
[amule.git] / unittests / tests / FormatTest.cpp
blob2c0965a3e0e47d22842445b42e028f5d798394da
1 #include <muleunit/test.h>
2 #include <common/Format.h>
3 #include <limits>
5 #include <Types.h>
7 using namespace muleunit;
9 // Note: These tests have been written in accordance with the
10 // capabilities of printf found in printf(3).
12 #define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
13 #define MIN(x) std::numeric_limits<x>::min()
14 #define MAX(x) std::numeric_limits<x>::max()
17 DECLARE(Format)
18 // Less is valid for the string type, so we need a cut
19 // down test for that.
20 void testFormat(const wxString& str, const wxChar* value) {
21 for (int p = -1; p < 5; ++p) {
22 wxString format;
24 format += wxT("%");
26 if (p == -1) {
27 format += wxT(".");
28 } else {
29 format += wxString::Format(wxT(".%d"), p);
32 format += str;
34 wxString reference = wxString::Format(format, value);
35 wxString actual = CFormat(format) % value;
37 ASSERT_EQUALS_M(reference, actual, format + wxT(": '") + reference + wxT("' != '") + actual + wxT("'"));
40 END_DECLARE;
43 TEST(Format, ConstructorAndGetString)
46 // Null should be valid
47 CFormat fmt1(NULL);
48 ASSERT_TRUE(fmt1.IsReady());
49 ASSERT_EQUALS(wxT(""), fmt1.GetString());
53 // Empty string should be valid
54 CFormat fmt2(wxT(""));
55 ASSERT_TRUE(fmt2.IsReady());
56 ASSERT_EQUALS(wxT(""), fmt2.GetString());
60 // Simple string with no format fields
61 CFormat fmt3(wxT("a b c"));
62 ASSERT_TRUE(fmt3.IsReady());
63 ASSERT_EQUALS(wxT("a b c"), fmt3.GetString());
67 // Simple string with one format field
68 CFormat fmt4(wxT("a %i c"));
69 ASSERT_FALSE(fmt4.IsReady());
70 #ifdef __WXDEBUG__
71 ASSERT_RAISES(CAssertFailureException, fmt4.GetString());
72 #endif
74 CAssertOff null;
75 ASSERT_EQUALS(wxT("a %i c"), fmt4.GetString());
80 TEST(Format, SetStringAndGetString)
82 CFormat format(NULL);
83 ASSERT_TRUE(format.IsReady());
84 ASSERT_EQUALS(wxT(""), format.GetString());
86 // Empty string should be valid
87 format = CFormat(wxT(""));
88 ASSERT_TRUE(format.IsReady());
89 ASSERT_EQUALS(wxT(""), format.GetString());
91 // Simple string with no format fields
92 format = CFormat(wxT("a b c"));
93 ASSERT_TRUE(format.IsReady());
94 ASSERT_EQUALS(wxT("a b c"), format.GetString());
96 // Simple string with one format field
97 format = CFormat(wxT("a %i c"));
98 ASSERT_FALSE(format.IsReady());
99 #ifdef __WXDEBUG__
100 ASSERT_RAISES(CAssertFailureException, format.GetString());
101 #endif
103 CAssertOff null;
104 ASSERT_EQUALS(wxT("a %i c"), format.GetString());
109 //! Implementation for the Standard type test
110 #define STANDARD_TEST(cformat, wxformat, value) \
112 wxString reference = wxString::Format(wxString(wxT("%")) + wxformat, value); \
113 wxString actual = CFormat(wxString(wxT("%")) + cformat) % value; \
114 ASSERT_EQUALS_M(reference, actual, wxString(wxT("%")) << wxformat \
115 << wxT(" vs. %") << cformat << wxT(": '") + reference + wxT("' != '") + actual + wxT("'")); \
120 //! Test the two boundaries and a middle value of the specificed format
121 #define STANDARD_TYPE_TESTS(cformat, wxformat, type) \
122 STANDARD_TEST(cformat, wxformat, MIN(type)); \
123 STANDARD_TEST(cformat, wxformat, (type)(MAX(type) / 2)); \
124 STANDARD_TEST(cformat, wxformat, MAX(type)); \
126 // In wx >= 2.9 wxChar represents a Unicode code point, thus its maximum value
127 // is 1114111 (0x10ffff).
128 TEST(Format, InjectwxChar)
130 STANDARD_TEST(wxT("c"), wxT("c"), MIN(wxChar));
131 STANDARD_TEST(wxT("c"), wxT("c"), (wxChar)(std::min<wxChar>(MAX(wxChar), 0x10ffff) / 2));
132 STANDARD_TEST(wxT("c"), wxT("c"), (std::min<wxChar>(MAX(wxChar), 0x10ffff)));
136 //! All length specifiers are supported and should yield the same result
137 const wxChar* int_lengths[] =
139 wxT("h"),
140 wxT(""),
141 wxT("l"),
142 wxT("ll"),
143 NULL
146 //! All signed types are supported, and should yield the same result
147 const wxChar* sint_types[] =
149 wxT("d"),
150 wxT("i"),
151 NULL
155 //! All unsigned types are supported, and should yield the same result
156 const wxChar* uint_types[] =
158 wxT("u"),
159 wxT("o"),
160 wxT("x"),
161 wxT("X"),
162 NULL
166 TEST(Format, InjectInteger)
168 const wxChar** sint_entry = sint_types;
170 while (*sint_entry) {
171 const wxChar** len_entry = int_lengths;
173 while (*len_entry) {
174 wxString entry = wxString() << *len_entry << *sint_entry;
176 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *sint_entry, signed short);
177 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *sint_entry, signed int);
178 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *sint_entry, signed long);
179 STANDARD_TYPE_TESTS(entry, wxString() << wxLongLongFmtSpec << *sint_entry, signed long long);
181 ++len_entry;
184 ++sint_entry;
188 const wxChar** uint_entry = uint_types;
189 while (*uint_entry) {
190 const wxChar** len_entry = int_lengths;
192 while (*len_entry) {
193 wxString entry = wxString() << *len_entry << *uint_entry;
195 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *uint_entry, unsigned short);
196 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *uint_entry, unsigned int);
197 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *uint_entry, unsigned long);
198 STANDARD_TYPE_TESTS(entry, wxString() << wxLongLongFmtSpec << *uint_entry, unsigned long long);
200 ++len_entry;
203 ++uint_entry;
208 TEST(Format, InjectFloatAndDouble)
210 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), float);
211 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), float);
212 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), float);
213 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), float);
214 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), float);
215 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), float);
217 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), double);
218 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), double);
219 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), double);
220 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), double);
221 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), double);
222 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), double);
226 TEST(Format, InjectString)
228 testFormat(wxT("s"), wxT(""));
229 testFormat(wxT("s"), wxT("abc"));
233 TEST(Format, InjectNULLString)
235 for (int p = -1; p < 5; ++p) {
236 wxString format = wxT("%");
238 if (p == -1) {
239 format += wxT(".");
240 } else {
241 format += wxString::Format(wxT(".%d"), p);
244 format += wxT("s");
246 wxString actual = CFormat(format) % (const wxChar*)NULL;
248 ASSERT_TRUE_M(actual.IsEmpty(), wxT("Expected empty string, got '") + actual + wxT("'"));
253 TEST(Format, MultipleFields)
256 CFormat fmt1(wxT("%d _ %u _ %i"));
257 fmt1 % -1 % 2u % -4;
258 ASSERT_TRUE(fmt1.IsReady());
259 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1.GetString());
263 CFormat fmt2(wxT("%d _ %u _ %i"));
264 ASSERT_FALSE(fmt2.IsReady());
265 fmt2 % -1;
266 ASSERT_FALSE(fmt2.IsReady());
267 fmt2 % 2u;
268 ASSERT_FALSE(fmt2.IsReady());
269 fmt2 % -4;
270 ASSERT_TRUE(fmt2.IsReady());
271 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2.GetString());
275 // Test grouped fields
276 CFormat fmt3(wxT("%d%u%i"));
277 fmt3 % -1 % 2u % -4;
278 ASSERT_EQUALS(wxT("-12-4"), fmt3.GetString());
283 TEST(Format, EscapedPercentageSign)
286 CFormat fmt1(wxT("%%"));
287 ASSERT_EQUALS(wxT("%"), fmt1.GetString());
291 CFormat fmt2(wxT("-- %% --"));
292 ASSERT_EQUALS(wxT("-- % --"), fmt2.GetString());
296 CFormat fmt3(wxT("%d _ %% _ %i"));
297 fmt3 % 1 % 2;
298 ASSERT_TRUE(fmt3.IsReady());
299 ASSERT_EQUALS(wxT("1 _ % _ 2"), fmt3.GetString());
303 CFormat fmt4(wxT("%% _ %% _ %%"));
304 ASSERT_EQUALS(wxT("% _ % _ %"), fmt4.GetString());
309 ///////////////////////////////////////////////////////////
310 // The following checks for invalid operations
312 TEST(Format, MalformedFields)
314 #ifdef __WXDEBUG__
316 // Incomplete format string
317 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%")));
318 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %")));
320 // Non-existing type
321 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q")) % 1);
322 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q")) % 1.0f );
323 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q -- ")) % wxT("1"));
325 // Invalid string length
326 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%.qs")) % wxT(""));
327 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%.-10s")) % wxT(""));
329 #endif
332 CAssertOff null;
334 ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
335 ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
337 // Non-existing type
338 ASSERT_EQUALS(wxT("%q"), CFormat(wxT("%q")) % 1);
339 ASSERT_EQUALS(wxT(" -- %q"), CFormat(wxT(" -- %q")) % 1.0f );
340 ASSERT_EQUALS(wxT(" -- %q -- "), CFormat(wxT(" -- %q -- ")) % wxT("1"));
342 // Invalid string length
343 ASSERT_EQUALS(wxT("%.qs"), CFormat(wxT("%.qs")) % wxT(""));
344 ASSERT_EQUALS(wxT("%.-10s"), CFormat(wxT("%.-10s")) % wxT(""));
346 // Wrong and right arguments
347 ASSERT_EQUALS(wxT("%s -- 17"), CFormat(wxT("%s -- %i")) % 1 % 17);
352 TEST(Format, NotReady)
354 CFormat fmt(wxT("-- %s - %d"));
355 ASSERT_FALSE(fmt.IsReady());
356 #ifdef __WXDEBUG__
357 ASSERT_RAISES(CAssertFailureException, fmt.GetString());
358 #endif
361 CAssertOff null;
362 ASSERT_EQUALS(wxT("-- %s - %d"), fmt);
365 fmt % wxT("foo");
367 ASSERT_FALSE(fmt.IsReady());
368 #ifdef __WXDEBUG__
369 ASSERT_RAISES(CAssertFailureException, fmt.GetString());
370 #endif
373 CAssertOff null;
374 ASSERT_EQUALS(wxT("-- foo - %d"), fmt);
377 fmt % 42;
379 ASSERT_TRUE(fmt.IsReady());
380 ASSERT_EQUALS(wxT("-- foo - 42"), fmt.GetString());
384 TEST(Format, WrongTypes)
386 #ifdef __WXDEBUG__
388 // Entirely wrong types:
389 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % 1);
390 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % 1.0f);
391 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % wxT('1'));
393 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % 1);
394 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT('1'));
395 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT("1"));
397 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % 1.0f);
398 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % wxT("1"));
400 #endif
403 CAssertOff null;
405 ASSERT_EQUALS(wxT("-- %s -- 42 --"), CFormat(wxT("-- %s -- %u --")) % 1 % 42);
406 ASSERT_EQUALS(wxT("-- %f -- 42 --"), CFormat(wxT("-- %f -- %u --")) % 1 % 42);
407 ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f % 42);
412 TEST(Format, NotSupported)
414 #ifdef __WXDEBUG__
416 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*d")) % 1);
417 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*s")) % wxT(""));
418 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%p")) % wxT(""));
419 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%n")) % wxT(""));
421 #endif
424 CAssertOff null;
426 ASSERT_EQUALS(wxT("%*d"), CFormat(wxT("%*d")) % 1);
427 ASSERT_EQUALS(wxT("%*s"), CFormat(wxT("%*s")) % wxT(""));
428 ASSERT_EQUALS(wxT("%p"), CFormat(wxT("%p")) % wxT(""));
429 ASSERT_EQUALS(wxT("%n"), CFormat(wxT("%n")) % wxT(""));
434 TEST(Format, Overfeeding)
436 CFormat fmt(wxT("%d - %d"));
437 ASSERT_FALSE(fmt.IsReady());
438 fmt % 1 % 2;
439 ASSERT_TRUE(fmt.IsReady());
440 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
442 #ifdef __WXDEBUG__
443 ASSERT_RAISES(CAssertFailureException, fmt % 1);
444 #endif
445 ASSERT_TRUE(fmt.IsReady());
446 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
448 #ifdef __WXDEBUG__
449 ASSERT_RAISES(CAssertFailureException, fmt % 1.0f);
450 #endif
451 ASSERT_TRUE(fmt.IsReady());
452 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
454 #ifdef __WXDEBUG__
455 ASSERT_RAISES(CAssertFailureException, fmt % wxT("1"));
456 #endif
457 ASSERT_TRUE(fmt.IsReady());
458 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
462 TEST(Format, 64bValues)
465 CFormat fmt(wxT("%lli - %lli"));
466 fmt % MIN(sint64) % MAX(sint64);
467 ASSERT_EQUALS(wxT("-9223372036854775808 - 9223372036854775807"), fmt.GetString());
471 CFormat fmt(wxT("%llu - %llu"));
472 fmt % MIN(uint64) % MAX(uint64);
473 ASSERT_EQUALS(wxT("0 - 18446744073709551615"), fmt.GetString());
478 class CTestPrintable : public CPrintable
480 public:
481 CTestPrintable(int value)
482 : m_value(value)
486 virtual wxString GetPrintableString() const
488 return wxString::Format(wxT("%i"), m_value);
491 private:
492 int m_value;
495 TEST(Format, Printable)
498 CFormat fmt(wxT("%s"));
499 fmt % CTestPrintable(10);
500 ASSERT_EQUALS(wxT("10"), fmt.GetString());
504 CFormat fmt(wxT("%s"));
505 fmt % CTestPrintable(-10);
506 ASSERT_EQUALS(wxT("-10"), fmt.GetString());