Upstream tarball 20080304
[amule.git] / unittests / tests / FormatTest.cpp
blobd863e68d1b4dd3a3e623d9990ee8a09dd50c3de3
1 #include <muleunit/test.h>
2 #include <common/Format.h>
3 #include <limits.h>
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 ASSERT_RAISES(CAssertFailureException, fmt4.GetString());
72 CAssertOff null;
73 ASSERT_EQUALS(wxT("a %i c"), fmt4.GetString());
78 TEST(Format, SetStringAndGetString)
80 CFormat format(NULL);
81 ASSERT_TRUE(format.IsReady());
82 ASSERT_EQUALS(wxT(""), format.GetString());
84 // Empty string should be valid
85 format = CFormat(wxT(""));
86 ASSERT_TRUE(format.IsReady());
87 ASSERT_EQUALS(wxT(""), format.GetString());
89 // Simple string with no format fields
90 format = CFormat(wxT("a b c"));
91 ASSERT_TRUE(format.IsReady());
92 ASSERT_EQUALS(wxT("a b c"), format.GetString());
94 // Simple string with one format field
95 format = CFormat(wxT("a %i c"));
96 ASSERT_FALSE(format.IsReady());
97 ASSERT_RAISES(CAssertFailureException, format.GetString());
99 CAssertOff null;
100 ASSERT_EQUALS(wxT("a %i c"), format.GetString());
105 //! Implementation for the Standard type test
106 #define STANDARD_TEST(cformat, wxformat, value) \
108 wxString reference = wxString::Format(wxString(wxT("%")) + wxformat, value); \
109 wxString actual = CFormat(wxString(wxT("%")) + cformat) % value; \
110 ASSERT_EQUALS_M(reference, actual, wxString(wxT("%")) << wxformat \
111 << wxT(" vs. %") << cformat << wxT(": '") + reference + wxT("' != '") + actual + wxT("'")); \
116 //! Test the two boundries and a middle value of the specificed format
117 #define STANDARD_TYPE_TESTS(cformat, wxformat, type) \
118 STANDARD_TEST(cformat, wxformat, MIN(type)); \
119 STANDARD_TEST(cformat, wxformat, (type)(MAX(type) / 2)); \
120 STANDARD_TEST(cformat, wxformat, MAX(type)); \
122 TEST(Format, InjectwxChar)
124 STANDARD_TYPE_TESTS(wxT("c"), wxT("c"), wxChar);
128 //! All length specifiers are supported and should yield the same result
129 const wxChar* int_lengths[] =
131 wxT("h"),
132 wxT(""),
133 wxT("l"),
134 wxT("ll"),
135 NULL
138 //! All signed types are supported, and should yield the same result
139 const wxChar* sint_types[] =
141 wxT("d"),
142 wxT("i"),
143 NULL
147 //! All unsigned types are supported, and should yield the same result
148 const wxChar* uint_types[] =
150 wxT("u"),
151 wxT("o"),
152 wxT("x"),
153 wxT("X"),
154 NULL
158 TEST(Format, InjectInteger)
160 const wxChar** sint_entry = sint_types;
162 while (*sint_entry) {
163 const wxChar** len_entry = int_lengths;
165 while (*len_entry) {
166 wxString entry = wxString() << *len_entry << *sint_entry;
168 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *sint_entry, signed short);
169 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *sint_entry, signed int);
170 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *sint_entry, signed long);
171 STANDARD_TYPE_TESTS(entry, wxString() << wxLongLongFmtSpec << *sint_entry, signed long long);
173 ++len_entry;
176 ++sint_entry;
180 const wxChar** uint_entry = uint_types;
181 while (*uint_entry) {
182 const wxChar** len_entry = int_lengths;
184 while (*len_entry) {
185 wxString entry = wxString() << *len_entry << *uint_entry;
187 STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *uint_entry, unsigned short);
188 STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *uint_entry, unsigned int);
189 STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *uint_entry, unsigned long);
190 STANDARD_TYPE_TESTS(entry, wxString() << wxLongLongFmtSpec << *uint_entry, unsigned long long);
192 ++len_entry;
195 ++uint_entry;
200 TEST(Format, InjectFloatAndDouble)
202 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), float);
203 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), float);
204 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), float);
205 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), float);
206 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), float);
207 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), float);
209 STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), double);
210 STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), double);
211 STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), double);
212 STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), double);
213 STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), double);
214 STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), double);
218 TEST(Format, InjectString)
220 testFormat(wxT("s"), wxT(""));
221 testFormat(wxT("s"), wxT("abc"));
225 TEST(Format, InjectNULLString)
227 for (int p = -1; p < 5; ++p) {
228 wxString format = wxT("%");
230 if (p == -1) {
231 format += wxT(".");
232 } else {
233 format += wxString::Format(wxT(".%d"), p);
236 format += wxT("s");
238 wxString actual = CFormat(format) % (const wxChar*)NULL;
240 ASSERT_TRUE_M(actual.IsEmpty(), wxT("Expected empty string, got '") + actual + wxT("'"));
245 TEST(Format, MultipleFields)
248 CFormat fmt1(wxT("%d _ %u _ %i"));
249 fmt1 % -1 % 2u % -4;
250 ASSERT_TRUE(fmt1.IsReady());
251 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1.GetString());
255 CFormat fmt2(wxT("%d _ %u _ %i"));
256 ASSERT_FALSE(fmt2.IsReady());
257 fmt2 % -1;
258 ASSERT_FALSE(fmt2.IsReady());
259 fmt2 % 2u;
260 ASSERT_FALSE(fmt2.IsReady());
261 fmt2 % -4;
262 ASSERT_TRUE(fmt2.IsReady());
263 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2.GetString());
267 // Test grouped fields
268 CFormat fmt3(wxT("%d%u%i"));
269 fmt3 % -1 % 2u % -4;
270 ASSERT_EQUALS(wxT("-12-4"), fmt3.GetString());
275 TEST(Format, EscapedPercentageSign)
278 CFormat fmt1(wxT("%%"));
279 ASSERT_EQUALS(wxT("%"), fmt1.GetString());
283 CFormat fmt2(wxT("-- %% --"));
284 ASSERT_EQUALS(wxT("-- % --"), fmt2.GetString());
288 CFormat fmt3(wxT("%d _ %% _ %i"));
289 fmt3 % 1 % 2;
290 ASSERT_TRUE(fmt3.IsReady());
291 ASSERT_EQUALS(wxT("1 _ % _ 2"), fmt3.GetString());
295 CFormat fmt4(wxT("%% _ %% _ %%"));
296 ASSERT_EQUALS(wxT("% _ % _ %"), fmt4.GetString());
301 ///////////////////////////////////////////////////////////
302 // The following checks for invalid operations
304 TEST(Format, MalformedFields)
307 // Incomplete format string
308 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%")));
309 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %")));
311 // Non-existing type
312 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q")) % 1);
313 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q")) % 1.0f );
314 ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q -- ")) % wxT("1"));
316 // Invalid string length
317 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%.qs")) % wxT(""));
318 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%.-10s")) % wxT(""));
322 CAssertOff null;
324 ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
325 ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
327 // Non-existing type
328 ASSERT_EQUALS(wxT("%q"), CFormat(wxT("%q")) % 1);
329 ASSERT_EQUALS(wxT(" -- %q"), CFormat(wxT(" -- %q")) % 1.0f );
330 ASSERT_EQUALS(wxT(" -- %q -- "), CFormat(wxT(" -- %q -- ")) % wxT("1"));
332 // Invalid string length
333 ASSERT_EQUALS(wxT("%.qs"), CFormat(wxT("%.qs")) % wxT(""));
334 ASSERT_EQUALS(wxT("%.-10s"), CFormat(wxT("%.-10s")) % wxT(""));
336 // Wrong and right arguments
337 ASSERT_EQUALS(wxT("%s -- 17"), CFormat(wxT("%s -- %i")) % 1 % 17);
342 TEST(Format, NotReady)
344 CFormat fmt(wxT("-- %s - %d"));
345 ASSERT_FALSE(fmt.IsReady());
346 ASSERT_RAISES(CAssertFailureException, fmt.GetString());
349 CAssertOff null;
350 ASSERT_EQUALS(wxT("-- %s - %d"), fmt);
353 fmt % wxT("foo");
355 ASSERT_FALSE(fmt.IsReady());
356 ASSERT_RAISES(CAssertFailureException, fmt.GetString());
359 CAssertOff null;
360 ASSERT_EQUALS(wxT("-- foo - %d"), fmt);
363 fmt % 42;
365 ASSERT_TRUE(fmt.IsReady());
366 ASSERT_EQUALS(wxT("-- foo - 42"), fmt.GetString());
370 TEST(Format, WrongTypes)
373 // Entirely wrong types:
374 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % 1);
375 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % 1.0f);
376 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%s")) % wxT('1'));
378 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % 1);
379 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT('1'));
380 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT("1"));
382 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % 1.0f);
383 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % wxT("1"));
387 CAssertOff null;
389 ASSERT_EQUALS(wxT("-- %s -- 42 --"), CFormat(wxT("-- %s -- %u --")) % 1 % 42);
390 ASSERT_EQUALS(wxT("-- %f -- 42 --"), CFormat(wxT("-- %f -- %u --")) % 1 % 42);
391 ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f % 42);
396 TEST(Format, NotSupported)
399 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*d")) % 1);
400 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*s")) % wxT(""));
401 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%p")) % wxT(""));
402 ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%n")) % wxT(""));
406 CAssertOff null;
408 ASSERT_EQUALS(wxT("%*d"), CFormat(wxT("%*d")) % 1);
409 ASSERT_EQUALS(wxT("%*s"), CFormat(wxT("%*s")) % wxT(""));
410 ASSERT_EQUALS(wxT("%p"), CFormat(wxT("%p")) % wxT(""));
411 ASSERT_EQUALS(wxT("%n"), CFormat(wxT("%n")) % wxT(""));
416 TEST(Format, Overfeeding)
418 CFormat fmt(wxT("%d - %d"));
419 ASSERT_FALSE(fmt.IsReady());
420 fmt % 1 % 2;
421 ASSERT_TRUE(fmt.IsReady());
422 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
424 ASSERT_RAISES(CAssertFailureException, fmt % 1);
425 ASSERT_TRUE(fmt.IsReady());
426 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
428 ASSERT_RAISES(CAssertFailureException, fmt % 1.0f);
429 ASSERT_TRUE(fmt.IsReady());
430 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
432 ASSERT_RAISES(CAssertFailureException, fmt % wxT("1"));
433 ASSERT_TRUE(fmt.IsReady());
434 ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
438 TEST(Format, 64bValues)
441 CFormat fmt(wxT("%lli - %lli"));
442 fmt % MIN(sint64) % MAX(sint64);
443 ASSERT_EQUALS(wxT("-9223372036854775808 - 9223372036854775807"), fmt.GetString());
447 CFormat fmt(wxT("%llu - %llu"));
448 fmt % MIN(uint64) % MAX(uint64);
449 ASSERT_EQUALS(wxT("0 - 18446744073709551615"), fmt.GetString());
454 class CTestPrintable : public CPrintable
456 public:
457 CTestPrintable(int value)
458 : m_value(value)
462 virtual wxString GetPrintableString() const
464 return wxString::Format(wxT("%i"), m_value);
467 private:
468 int m_value;
471 TEST(Format, Printable)
474 CFormat fmt(wxT("%s"));
475 fmt % CTestPrintable(10);
476 ASSERT_EQUALS(wxT("10"), fmt.GetString());
480 CFormat fmt(wxT("%s"));
481 fmt % CTestPrintable(-10);
482 ASSERT_EQUALS(wxT("-10"), fmt.GetString());