1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508 | #include <muleunit/test.h>
#include <common/Format.h>
#include <limits>
#include <Types.h>
using namespace muleunit;
// Note: These tests have been written in accordance with the
// capabilities of printf found in printf(3).
// Note to the above: Except where otherwise stated.
#define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
#define MIN(x) std::numeric_limits<x>::min()
#define MAX(x) std::numeric_limits<x>::max()
DECLARE(Format)<--- There is an unknown macro here somewhere. Configuration is required. If DECLARE is a macro then please configure it.
// Less is valid for the string type, so we need a cut
// down test for that.
void testFormat(const wxString& str, const wxChar* value) {
for (int p = -1; p < 5; ++p) {
wxString format;
format += wxT("%");
if (p == -1) {
format += wxT(".");
} else {
format += wxString::Format(wxT(".%d"), p);
}
format += str;
wxString reference = wxString::Format(format, value);
wxString actual = CFormat(format) % value;
ASSERT_EQUALS_M(reference, actual, format + wxT(": '") + reference + wxT("' != '") + actual + wxT("'"));
}
}
END_DECLARE;
TEST(Format, ConstructorAndGetString)
{
{
// Null should be valid
CFormat fmt1(NULL);
ASSERT_EQUALS(wxT(""), fmt1.GetString());
}
{
// Empty string should be valid
CFormat fmt2(wxT(""));
ASSERT_EQUALS(wxT(""), fmt2.GetString());
}
{
// Simple string with no format fields
CFormat fmt3(wxT("a b c"));
ASSERT_EQUALS(wxT("a b c"), fmt3.GetString());
}
{
// Simple string with one format field
CFormat fmt4(wxT("a %i c"));
ASSERT_EQUALS(wxT("a %i c"), fmt4.GetString());
}
}
TEST(Format, SetStringAndGetString)
{
CFormat format(NULL);
ASSERT_EQUALS(wxT(""), format.GetString());
// Empty string should be valid
format = CFormat(wxT(""));
ASSERT_EQUALS(wxT(""), format.GetString());
// Simple string with no format fields
format = CFormat(wxT("a b c"));
ASSERT_EQUALS(wxT("a b c"), format.GetString());
// Simple string with one format field
format = CFormat(wxT("a %i c"));
ASSERT_EQUALS(wxT("a %i c"), format.GetString());
}
//! Implementation for the Standard type test
#define STANDARD_TEST(cformat, wxformat, value) \
{ \
wxString reference = wxString::Format(wxString(wxT("%")) + wxformat, value); \
wxString actual = CFormat(wxString(wxT("%")) + cformat) % value; \
ASSERT_EQUALS_M(reference, actual, wxString(wxT("%")) << wxformat \
<< wxT(" vs. %") << cformat << wxT(": '") + reference + wxT("' != '") + actual + wxT("'")); \
}
//! Test the two boundaries and a middle value of the specified format
#define STANDARD_TYPE_TESTS(cformat, wxformat, type) \
STANDARD_TEST(cformat, wxformat, MIN(type)); \
STANDARD_TEST(cformat, wxformat, (type)(MAX(type) / 2)); \
STANDARD_TEST(cformat, wxformat, MAX(type)); \
// In wx >= 2.9 wxChar represents a Unicode code point, thus its maximum value
// is 1114111 (0x10ffff).
TEST(Format, InjectwxChar)
{
STANDARD_TEST(wxT("c"), wxT("c"), MIN(wxChar));
STANDARD_TEST(wxT("c"), wxT("c"), (wxChar)(std::min<wxChar>(MAX(wxChar), 0x10ffff) / 2));
STANDARD_TEST(wxT("c"), wxT("c"), (std::min<wxChar>(MAX(wxChar), 0x10ffff)));
}
//! All length specifiers are supported and should yield the same result
const wxChar* int_lengths[] =
{
wxT("h"),
wxT(""),
wxT("l"),
wxT("ll"),
NULL
};
//! All signed types are supported, and should yield the same result
const wxChar* sint_types[] =
{
wxT("d"),
wxT("i"),
NULL
};
//! All unsigned types are supported, and should yield the same result
const wxChar* uint_types[] =
{
wxT("u"),
wxT("o"),
wxT("x"),
wxT("X"),
NULL
};
TEST(Format, InjectInteger)
{
const wxChar** sint_entry = sint_types;
while (*sint_entry) {
const wxChar** len_entry = int_lengths;
while (*len_entry) {
wxString entry = wxString() << *len_entry << *sint_entry;
STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *sint_entry, signed short);
STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *sint_entry, signed int);
STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *sint_entry, signed long);
STANDARD_TYPE_TESTS(entry, wxString() << WXLONGLONGFMTSPEC << *sint_entry, signed long long);
++len_entry;
}
++sint_entry;
}
const wxChar** uint_entry = uint_types;
while (*uint_entry) {
const wxChar** len_entry = int_lengths;
while (*len_entry) {
wxString entry = wxString() << *len_entry << *uint_entry;
STANDARD_TYPE_TESTS(entry, wxString() << wxT("h") << *uint_entry, unsigned short);
STANDARD_TYPE_TESTS(entry, wxString() << wxT("") << *uint_entry, unsigned int);
STANDARD_TYPE_TESTS(entry, wxString() << wxT("l") << *uint_entry, unsigned long);
STANDARD_TYPE_TESTS(entry, wxString() << WXLONGLONGFMTSPEC << *uint_entry, unsigned long long);
++len_entry;
}
++uint_entry;
}
}
TEST(Format, InjectFloatAndDouble)
{
STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), float);
STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), float);
STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), float);
STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), float);
STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), float);
STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), float);
STANDARD_TYPE_TESTS(wxT("e"), wxT("e"), double);
STANDARD_TYPE_TESTS(wxT("E"), wxT("E"), double);
STANDARD_TYPE_TESTS(wxT("f"), wxT("f"), double);
STANDARD_TYPE_TESTS(wxT("F"), wxT("F"), double);
STANDARD_TYPE_TESTS(wxT("g"), wxT("g"), double);
STANDARD_TYPE_TESTS(wxT("G"), wxT("G"), double);
}
TEST(Format, InjectString)
{
testFormat(wxT("s"), wxT(""));
testFormat(wxT("s"), wxT("abc"));
}
TEST(Format, InjectNULLString)
{
for (int p = -1; p < 5; ++p) {
wxString format = wxT("%");
if (p == -1) {
format += wxT(".");
} else {
format += wxString::Format(wxT(".%d"), p);
}
format += wxT("s");
// cppcheck-suppress zerodiv
wxString actual = CFormat(format) % (const wxChar*)NULL;
ASSERT_TRUE_M(actual.IsEmpty(), wxT("Expected empty string, got '") + actual + wxT("'"));
}
}
TEST(Format, MultipleFields)
{
{
CFormat fmt1(wxT("%d _ %u _ %i"));
fmt1 % -1 % 2u % -4;
ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt1.GetString());
}
{
CFormat fmt2(wxT("%d _ %u _ %i"));
fmt2 % -1;
fmt2 % 2u;
fmt2 % -4;
ASSERT_EQUALS(wxT("-1 _ 2 _ -4"), fmt2.GetString());
}
{
// Test grouped fields
CFormat fmt3(wxT("%d%u%i"));
fmt3 % -1 % 2u % -4;
ASSERT_EQUALS(wxT("-12-4"), fmt3.GetString());
}
}
TEST(Format, EscapedPercentageSign)
{
{
CFormat fmt1(wxT("%%"));
ASSERT_EQUALS(wxT("%"), fmt1.GetString());
}
{
CFormat fmt2(wxT("-- %% --"));
ASSERT_EQUALS(wxT("-- % --"), fmt2.GetString());
}
{
CFormat fmt3(wxT("%d _ %% _ %i"));
fmt3 % 1 % 2;
ASSERT_EQUALS(wxT("1 _ % _ 2"), fmt3.GetString());
}
{
CFormat fmt4(wxT("%% _ %% _ %%"));
ASSERT_EQUALS(wxT("% _ % _ %"), fmt4.GetString());
}
}
///////////////////////////////////////////////////////////
// The following checks for invalid operations
TEST(Format, MalformedFields)
{
#ifdef __WXDEBUG__
{
// Incomplete format string
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%")));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %")));
// Non-existing type
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q")));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q")));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT(" -- %q -- ")));
// Partially valid format strings
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%i%q")));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%q%i")));
}
#endif
{
CAssertOff null;
ASSERT_EQUALS(wxT("%"), CFormat(wxT("%")));
ASSERT_EQUALS(wxT(" -- %"), CFormat(wxT(" -- %")));
// Non-existing type
ASSERT_EQUALS(wxT("%q"), CFormat(wxT("%q")) % 1);
ASSERT_EQUALS(wxT(" -- %q"), CFormat(wxT(" -- %q")) % 1.0f);
ASSERT_EQUALS(wxT(" -- %q -- "), CFormat(wxT(" -- %q -- ")) % wxT("1"));
// Partially valid format strings
ASSERT_EQUALS(wxT("1%q"), CFormat(wxT("%i%q")) % 1);
ASSERT_EQUALS(wxT("%q1"), CFormat(wxT("%q%i")) % 1);
// Wrong and right arguments
ASSERT_EQUALS(wxT("%i -- 17"), CFormat(wxT("%i -- %i")) % 1.0 % 17);
}
}
TEST(Format, NotReady)
{
CFormat fmt(wxT("-- %s - %d"));
ASSERT_EQUALS(wxT("-- %s - %d"), fmt.GetString());
fmt % wxT("foo");
ASSERT_EQUALS(wxT("-- foo - %d"), fmt.GetString());
fmt % 42;
ASSERT_EQUALS(wxT("-- foo - 42"), fmt.GetString());
}
TEST(Format, WrongTypes)
{
#ifdef __WXDEBUG__
{
// Entirely wrong types
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%c")) % wxT("1"));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%f")) % wxT("1"));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % 1.0f);
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%d")) % wxT("1"));
}
#endif
{
CAssertOff null;
ASSERT_EQUALS(wxT("-- %d -- 42 --"), CFormat(wxT("-- %d -- %u --")) % 1.0f % 42);
}
}
TEST(Format, NotSupported)
{
#ifdef __WXDEBUG__
{
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*d")) % 1);
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%*s")) % wxT(""));
ASSERT_RAISES(CAssertFailureException, CFormat(wxT("%n")) % wxT(""));
}
#endif
{
CAssertOff null;
ASSERT_EQUALS(wxT("%*d"), CFormat(wxT("%*d")) % 1);
ASSERT_EQUALS(wxT("%*s"), CFormat(wxT("%*s")) % wxT(""));
ASSERT_EQUALS(wxT("%n"), CFormat(wxT("%n")) % wxT(""));
}
}
TEST(Format, Overfeeding)
{
CFormat fmt(wxT("%d - %d"));
fmt % 1 % 2;
ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
fmt % 1;
ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
fmt % 1.0;
ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
fmt % wxT("x");
ASSERT_EQUALS(wxT("1 - 2"), fmt.GetString());
}
TEST(Format, 64bValues)
{
{
CFormat fmt(wxT("%lli - %lli"));
fmt % MIN(sint64) % MAX(sint64);
ASSERT_EQUALS(wxT("-9223372036854775808 - 9223372036854775807"), fmt.GetString());
}
{
CFormat fmt(wxT("%llu - %llu"));
fmt % MIN(uint64) % MAX(uint64);
ASSERT_EQUALS(wxT("0 - 18446744073709551615"), fmt.GetString());
}
}
class CTestPrintable
{
public:
CTestPrintable(int value)
: m_value(value)
{}
wxString GetPrintableString() const
{
return wxString::Format(wxT("%i"), m_value);
}
private:
int m_value;
};
template<>
inline CFormat& CFormat::operator%(CTestPrintable value)
{
return this->operator%<const wxString&>(value.GetPrintableString());
}
TEST(Format, UserDefined)
{
{
CFormat fmt(wxT("%s"));
fmt % CTestPrintable(10);
ASSERT_EQUALS(wxT("10"), fmt.GetString());
}
{
CFormat fmt(wxT("%s"));
fmt % CTestPrintable(-10);
ASSERT_EQUALS(wxT("-10"), fmt.GetString());
}
}
TEST(Format, ReorderedArguments)
{
// Swapping two arguments of the same type
{
CFormat fmt(wxT("%2$i,%1$i"));
fmt % 1 % 2;
ASSERT_EQUALS(wxT("2,1"), fmt.GetString());
}
// Swapping arguments of different type
{
CFormat fmt(wxT("%2$i,%1$c"));
fmt % wxT('a') % 2;
ASSERT_EQUALS(wxT("2,a"), fmt.GetString());
}
// Using the same argument multiple times
{
CFormat fmt(wxT("%1$i,%1$i"));
fmt % 3;
ASSERT_EQUALS(wxT("3,3"), fmt.GetString());
}
// Leaving gaps (printf doesn't allow this!)
{
CFormat fmt(wxT("%1$i,%3$i"));
fmt % 1 % 2 % 3;
ASSERT_EQUALS(wxT("1,3"), fmt.GetString());
}
// Mixing positional and indexed arguments (printf doesn't allow this!)
{
CFormat fmt(wxT("%3$i,%i,%1$i"));
fmt % 1 % 2 % 3;
ASSERT_EQUALS(wxT("3,2,1"), fmt.GetString());
}
}
// Tests for non-standard functionality.
// These are extensions to the standard printf functionality.
TEST(Format, DifferentArguments)
{
// Tests for default conversion with the 's' conversion type
ASSERT_EQUALS(wxT("a"), CFormat(wxT("%s")) % wxT('a'));
ASSERT_EQUALS(wxT("1"), CFormat(wxT("%s")) % 1u);
ASSERT_EQUALS(wxT("-1"), CFormat(wxT("%s")) % -1);
ASSERT_EQUALS(wxString::Format(wxT("%g"), 1.2), CFormat(wxT("%s")) % 1.2);
// Test for changing the conversion type based on the argument type
ASSERT_EQUALS(wxT("-1"), CFormat(wxT("%u")) % -1);
// Tests for accepting mismatching argument type
ASSERT_EQUALS(wxT("C"), CFormat(wxT("%c")) % 67);
ASSERT_EQUALS(wxT("69"), CFormat(wxT("%i")) % wxT('E'));
ASSERT_EQUALS(wxString::Format(wxT("%.e"), 1.0), CFormat(wxT("%.e")) % 1u);
}
|