Still fixing the unittests...
[amule.git] / unittests / tests / FormatTest.cpp
blob0fbfac8712199afd8dd952bf6a52f7348d8891fb
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).
11 // Note to the above: Except where otherwise stated.
13 #define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
14 #define MIN(x) std::numeric_limits<x>::min()
15 #define MAX(x) std::numeric_limits<x>::max()
18 DECLARE(Format)
19 // Less is valid for the string type, so we need a cut
20 // down test for that.
21 void testFormat(const wxString& str, const wxChar* value) {
22 for (int p = -1; p < 5; ++p) {
23 wxString format;
25 format += wxT("%");
27 if (p == -1) {
28 format += wxT(".");
29 } else {
30 format += wxString::Format(wxT(".%d"), p);
33 format += str;
35 wxString reference = wxString::Format(format, value);
36 wxString actual = CFormat(format) % value;
38 ASSERT_EQUALS_M(reference, actual, format + wxT(": '") + reference + wxT("' != '") + actual + wxT("'"));
41 END_DECLARE;
44 TEST(Format, ConstructorAndGetString)
47 // Null should be valid
48 CFormat fmt1(NULL);
49 ASSERT_EQUALS(wxT(""), fmt1.GetString());
53 // Empty string should be valid
54 CFormat fmt2(wxT(""));
55 ASSERT_EQUALS(wxT(""), fmt2.GetString());
59 // Simple string with no format fields
60 CFormat fmt3(wxT("a b c"));
61 ASSERT_EQUALS(wxT("a b c"), fmt3.GetString());
65 // Simple string with one format field
66 CFormat fmt4(wxT("a %i c"));
67 ASSERT_EQUALS(wxT("a %i c"), fmt4.GetString());
72 TEST(Format, SetStringAndGetString)
74 CFormat format(NULL);
75 ASSERT_EQUALS(wxT(""), format.GetString());
77 // Empty string should be valid
78 format = CFormat(wxT(""));
79 ASSERT_EQUALS(wxT(""), format.GetString());
81 // Simple string with no format fields
82 format = CFormat(wxT("a b c"));
83 ASSERT_EQUALS(wxT("a b c"), format.GetString());
85 // Simple string with one format field
86 format = CFormat(wxT("a %i c"));
87 ASSERT_EQUALS(wxT("a %i c"), format.GetString());
92 //! Implementation for the Standard type test
93 #define STANDARD_TEST(cformat, wxformat, value) \
94 { \
95 wxString reference = wxString::Format(wxString(wxT("%")) + wxformat, value); \
96 wxString actual = CFormat(wxString(wxT("%")) + cformat) % value; \
97 ASSERT_EQUALS_M(reference, actual, wxString(wxT("%")) << wxformat \
98 << wxT(" vs. %") << cformat << wxT(": '") + reference + wxT("' != '") + actual + wxT("'")); \
103 //! Test the two boundaries and a middle value of the specificed format
104 #define STANDARD_TYPE_TESTS(cformat, wxformat, type) \
105 STANDARD_TEST(cformat, wxformat, MIN(type)); \
106 STANDARD_TEST(cformat, wxformat, (type)(MAX(type) / 2)); \
107 STANDARD_TEST(cformat, wxformat, MAX(type)); \
109 // In wx >= 2.9 wxChar represents a Unicode code point, thus its maximum value
110 // is 1114111 (0x10ffff).
111 TEST(Format, InjectwxChar)
113 STANDARD_TEST(wxT("c"), wxT("c"), MIN(wxChar));
114 STANDARD_TEST(wxT("c"), wxT("c"), (wxChar)(std::min<wxChar>(MAX(wxChar), 0x10ffff) / 2));
115 STANDARD_TEST(wxT("c"), wxT("c"), (std::min<wxChar>(MAX(wxChar), 0x10ffff)));
119 //! All length specifiers are supported and should yield the same result
120 const wxChar* int_lengths[] =
122 wxT("h"),
123 wxT(""),
124 wxT("l"),
125 wxT("ll"),
126 NULL
129 //! All signed types are supported, and should yield the same result
130 const wxChar* sint_types[] =
132 wxT("d"),
133 wxT("i"),
134 NULL
138 //! All unsigned types are supported, and should yield the same result
139 const wxChar* uint_types[] =
141 wxT("u"),
142 wxT("o"),
143 wxT("x"),
144 wxT("X"),
145 NULL
149 TEST(Format, InjectInteger)
151 const wxChar** sint_entry = sint_types;
153 while (*sint_entry) {
154 const wxChar** len_entry = int_lengths;
156 while (*len_entry) {
157 wxString entry = wxString() << *len_entry << *sint_entry;
159 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *sint_entry, signed short);
160 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *sint_entry, signed int);
161 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *sint_entry, signed long);
162 STANDARD_TYPE_TESTS(entry, wxString() << WXLONGLONGFMTSPEC << *sint_entry, signed long long);
164 ++len_entry;
167 ++sint_entry;
171 const wxChar** uint_entry = uint_types;
172 while (*uint_entry) {
173 const wxChar** len_entry = int_lengths;
175 while (*len_entry) {
176 wxString entry = wxString() << *len_entry << *uint_entry;
178 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *uint_entry, unsigned short);
179 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *uint_entry, unsigned int);
180 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *uint_entry, unsigned long);
181 STANDARD_TYPE_TESTS(entry, wxString() << WXLONGLONGFMTSPEC << *uint_entry, unsigned long long);
183 ++len_entry;
186 ++uint_entry;
191 TEST(Format, InjectFloatAndDouble)
193 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), float);
194 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), float);
195 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), float);
196 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), float);
197 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), float);
198 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), float);
200 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), double);
201 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), double);
202 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), double);
203 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), double);
204 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), double);
205 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), double);
209 TEST(Format, InjectString)
211 testFormat(wxT("s"), wxT(""));
212 testFormat(wxT("s"), wxT("abc"));
216 TEST(Format, InjectNULLString)
218 for (int p = -1; p < 5; ++p) {
219 wxString format = wxT("%");
221 if (p == -1) {
222 format += wxT(".");
223 } else {
224 format += wxString::Format(wxT(".%d"), p);
227 format += wxT("s");
229 // cppcheck-suppress zerodiv
230 wxString actual = CFormat(format) % (const wxChar*)NULL;
232 ASSERT_TRUE_M(actual.IsEmpty(), wxT("Expected empty string, got '") + actual + wxT("'"));
237 TEST(Format, MultipleFields)
240 CFormat fmt1(wxT("%d _ %u _ %i"));
241 fmt1 % -1 % 2u % -4;
242 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1.GetString());
246 CFormat fmt2(wxT("%d _ %u _ %i"));
247 fmt2 % -1;
248 fmt2 % 2u;
249 fmt2 % -4;
250 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2.GetString());
254 // Test grouped fields
255 CFormat fmt3(wxT("%d%u%i"));
256 fmt3 % -1 % 2u % -4;
257 ASSERT_EQUALS(wxT("-12-4"), fmt3.GetString());
262 TEST(Format, EscapedPercentageSign)
265 CFormat fmt1(wxT("%%"));
266 ASSERT_EQUALS(wxT("%"), fmt1.GetString());
270 CFormat fmt2(wxT("-- %% --"));
271 ASSERT_EQUALS(wxT("-- % --"), fmt2.GetString());
275 CFormat fmt3(wxT("%d _ %% _ %i"));
276 fmt3 % 1 % 2;
277 ASSERT_EQUALS(wxT("1 _ % _ 2"), fmt3.GetString());
281 CFormat fmt4(wxT("%% _ %% _ %%"));
282 ASSERT_EQUALS(wxT("% _ % _ %"), fmt4.GetString());
287 ///////////////////////////////////////////////////////////
288 // The following checks for invalid operations
290 TEST(Format, MalformedFields)
292 #ifdef __WXDEBUG__
294 // Incomplete format string
295 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%")));
296 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %")));
298 // Non-existing type
299 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q")));
300 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q")));
301 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q -- ")));
303 // Partially valid format strings
304 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%i%q")));
305 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q%i")));
307 #endif
310 CAssertOff null;
312 ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
313 ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
315 // Non-existing type
316 ASSERT_EQUALS(wxT("%q"), CFormat(wxT("%q")) % 1);
317 ASSERT_EQUALS(wxT(" -- %q"), CFormat(wxT(" -- %q")) % 1.0f);
318 ASSERT_EQUALS(wxT(" -- %q -- "), CFormat(wxT(" -- %q -- ")) % wxT("1"));
320 // Partially valid format strings
321 ASSERT_EQUALS(wxT("1%q"), CFormat(wxT("%i%q")) % 1);
322 ASSERT_EQUALS(wxT("%q1"), CFormat(wxT("%q%i")) % 1);
324 // Wrong and right arguments
325 ASSERT_EQUALS(wxT("%i -- 17"), CFormat(wxT("%i -- %i")) % 1.0 % 17);
330 TEST(Format, NotReady)
332 CFormat fmt(wxT("-- %s - %d"));
333 ASSERT_EQUALS(wxT("-- %s - %d"), fmt.GetString());
335 fmt % wxT("foo");
336 ASSERT_EQUALS(wxT("-- foo - %d"), fmt.GetString());
338 fmt % 42;
339 ASSERT_EQUALS(wxT("-- foo - 42"), fmt.GetString());
343 TEST(Format, WrongTypes)
345 #ifdef __WXDEBUG__
347 // Entirely wrong types
348 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%c")) % wxT("1"));
349 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT("1"));
350 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % 1.0f);
351 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % wxT("1"));
353 #endif
356 CAssertOff null;
358 ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f % 42);
363 TEST(Format, NotSupported)
365 #ifdef __WXDEBUG__
367 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*d")) % 1);
368 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*s")) % wxT(""));
369 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%n")) % wxT(""));
371 #endif
374 CAssertOff null;
376 ASSERT_EQUALS(wxT("%*d"), CFormat(wxT("%*d")) % 1);
377 ASSERT_EQUALS(wxT("%*s"), CFormat(wxT("%*s")) % wxT(""));
378 ASSERT_EQUALS(wxT("%n"), CFormat(wxT("%n")) % wxT(""));
383 TEST(Format, Overfeeding)
385 CFormat fmt(wxT("%d - %d"));
386 fmt % 1 % 2;
387 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
389 fmt % 1;
390 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
392 fmt % 1.0;
393 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
395 fmt % wxT("x");
396 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
400 TEST(Format, 64bValues)
403 CFormat fmt(wxT("%lli - %lli"));
404 fmt % MIN(sint64) % MAX(sint64);
405 ASSERT_EQUALS(wxT("-9223372036854775808 - 9223372036854775807"), fmt.GetString());
409 CFormat fmt(wxT("%llu - %llu"));
410 fmt % MIN(uint64) % MAX(uint64);
411 ASSERT_EQUALS(wxT("0 - 18446744073709551615"), fmt.GetString());
416 class CTestPrintable
418 public:
419 CTestPrintable(int value)
420 : m_value(value)
423 wxString GetPrintableString() const
425 return wxString::Format(wxT("%i"), m_value);
428 private:
429 int m_value;
432 template<>
433 inline CFormat& CFormat::operator%(CTestPrintable value)
435 return this->operator%<const wxString&>(value.GetPrintableString());
438 TEST(Format, UserDefined)
441 CFormat fmt(wxT("%s"));
442 fmt % CTestPrintable(10);
443 ASSERT_EQUALS(wxT("10"), fmt.GetString());
447 CFormat fmt(wxT("%s"));
448 fmt % CTestPrintable(-10);
449 ASSERT_EQUALS(wxT("-10"), fmt.GetString());
453 TEST(Format, ReorderedArguments)
455 // Swapping two arguments of the same type
457 CFormat fmt(wxT("%2$i,%1$i"));
458 fmt % 1 % 2;
459 ASSERT_EQUALS(wxT("2,1"), fmt.GetString());
462 // Swapping arguments of different type
464 CFormat fmt(wxT("%2$i,%1$c"));
465 fmt % wxT('a') % 2;
466 ASSERT_EQUALS(wxT("2,a"), fmt.GetString());
469 // Using the same argument multiple times
471 CFormat fmt(wxT("%1$i,%1$i"));
472 fmt % 3;
473 ASSERT_EQUALS(wxT("3,3"), fmt.GetString());
476 // Leaving gaps (printf doesn't allow this!)
478 CFormat fmt(wxT("%1$i,%3$i"));
479 fmt % 1 % 2 % 3;
480 ASSERT_EQUALS(wxT("1,3"), fmt.GetString());
483 // Mixing positional and indexed arguments (printf doesn't allow this!)
485 CFormat fmt(wxT("%3$i,%i,%1$i"));
486 fmt % 1 % 2 % 3;
487 ASSERT_EQUALS(wxT("3,2,1"), fmt.GetString());
491 // Tests for non-standard functionality.
492 // These are extensions to the standard printf functionality.
493 TEST(Format, DifferentArguments)
495 // Tests for default conversion with the 's' conversion type
496 ASSERT_EQUALS(wxT("a"), CFormat(wxT("%s")) % wxT('a'));
497 ASSERT_EQUALS(wxT("1"), CFormat(wxT("%s")) % 1u);
498 ASSERT_EQUALS(wxT("-1"), CFormat(wxT("%s")) % -1);
499 ASSERT_EQUALS(wxString::Format(wxT("%g"), 1.2), CFormat(wxT("%s")) % 1.2);
501 // Test for changing the conversion type based on the argument type
502 ASSERT_EQUALS(wxT("-1"), CFormat(wxT("%u")) % -1);
504 // Tests for accepting mismatching argument type
505 ASSERT_EQUALS(wxT("C"), CFormat(wxT("%c")) % 67);
506 ASSERT_EQUALS(wxT("69"), CFormat(wxT("%i")) % wxT('E'));
507 ASSERT_EQUALS(wxString::Format(wxT("%.e"), 1.0), CFormat(wxT("%.e")) % 1u);