1 #include <muleunit/test.h>
2 #include <common/Format.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()
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
) {
30 format
+= wxString::Format(wxT(".%d"), p
);
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("'"));
44 TEST(Format
, ConstructorAndGetString
)
47 // Null should be valid
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
)
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) \
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
[] =
129 //! All signed types are supported, and should yield the same result
130 const wxChar
* sint_types
[] =
138 //! All unsigned types are supported, and should yield the same result
139 const wxChar
* uint_types
[] =
149 TEST(Format
, InjectInteger
)
151 const wxChar
** sint_entry
= sint_types
;
153 while (*sint_entry
) {
154 const wxChar
** len_entry
= int_lengths
;
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);
171 const wxChar
** uint_entry
= uint_types
;
172 while (*uint_entry
) {
173 const wxChar
** len_entry
= int_lengths
;
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);
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("%");
224 format
+= wxString::Format(wxT(".%d"), p
);
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"));
242 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1
.GetString());
246 CFormat
fmt2(wxT("%d _ %u _ %i"));
250 ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2
.GetString());
254 // Test grouped fields
255 CFormat
fmt3(wxT("%d%u%i"));
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"));
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
)
294 // Incomplete format string
295 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%")));
296 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT(" -- %")));
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")));
312 ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
313 ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
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());
336 ASSERT_EQUALS(wxT("-- foo - %d"), fmt
.GetString());
339 ASSERT_EQUALS(wxT("-- foo - 42"), fmt
.GetString());
343 TEST(Format
, WrongTypes
)
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"));
358 ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f
% 42);
363 TEST(Format
, NotSupported
)
367 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%*d")) % 1);
368 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%*s")) % wxT(""));
369 ASSERT_RAISES(CAssertFailureException
, CFormat(wxT("%n")) % wxT(""));
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"));
387 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
390 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
393 ASSERT_EQUALS(wxT("1 - 2"), fmt
.GetString());
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());
419 CTestPrintable(int value
)
423 wxString
GetPrintableString() const
425 return wxString::Format(wxT("%i"), m_value
);
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"));
459 ASSERT_EQUALS(wxT("2,1"), fmt
.GetString());
462 // Swapping arguments of different type
464 CFormat
fmt(wxT("%2$i,%1$c"));
466 ASSERT_EQUALS(wxT("2,a"), fmt
.GetString());
469 // Using the same argument multiple times
471 CFormat
fmt(wxT("%1$i,%1$i"));
473 ASSERT_EQUALS(wxT("3,3"), fmt
.GetString());
476 // Leaving gaps (printf doesn't allow this!)
478 CFormat
fmt(wxT("%1$i,%3$i"));
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"));
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);