don't run WrongFormatStrings test with system printf, it doesn't work
[wxWidgets.git] / tests / strings / vsnprintf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/vsnprintf.cpp
3 // Purpose: wxVsnprintf unit test
4 // Author: Francesco Montorsi
5 // (part of this file was taken from CMP.c of TRIO package
6 // written by Bjorn Reese and Daniel Stenberg)
7 // Created: 2006-04-01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 #include "testprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #include "wx/wxchar.h"
25 #endif // WX_PRECOMP
26
27
28 // NOTE: for more info about the specification of wxVsnprintf() behaviour you can
29 // refer to the following page of the GNU libc manual:
30 // http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
31
32
33
34 // ----------------------------------------------------------------------------
35 // global utilities for testing
36 // ----------------------------------------------------------------------------
37
38 #define MAX_TEST_LEN 1024
39
40 // temporary buffers
41 static wxChar buf[MAX_TEST_LEN];
42 int r;
43
44 // these macros makes it possible to write all tests without repeating a lot
45 // of times the wxT() macro
46 // NOTE: you should use expected strings with these macros which do not exceed
47 // MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
48
49 #define ASSERT_STR_EQUAL( a, b ) \
50 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
51
52 #define CMP5(expected, x, y, z, w) \
53 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
54 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
55 ASSERT_STR_EQUAL( wxT(expected), buf );
56
57 #define CMP4(expected, x, y, z) \
58 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
59 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
60 ASSERT_STR_EQUAL( wxT(expected), buf );
61
62 #define CMP3(expected, x, y) \
63 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
64 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
65 ASSERT_STR_EQUAL( wxT(expected), buf );
66
67 #define CMP2(expected, x) \
68 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
69 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
70 ASSERT_STR_EQUAL( wxT(expected), buf );
71
72 // NOTE: this macro is used also with too-small buffers (see Miscellaneous())
73 // test function, thus the return value can be > size and thus we
74 // cannot check if r == (int)wxStrlen(buf)
75 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
76 r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
77 CPPUNIT_ASSERT( r > 0 ); \
78 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
79 wxString(buffer) )
80
81
82
83 // ----------------------------------------------------------------------------
84 // test class
85 // ----------------------------------------------------------------------------
86
87 class VsnprintfTestCase : public CppUnit::TestCase
88 {
89 public:
90 VsnprintfTestCase();
91
92 private:
93 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
94 CPPUNIT_TEST( C );
95 CPPUNIT_TEST( D );
96 CPPUNIT_TEST( X );
97 CPPUNIT_TEST( O );
98 CPPUNIT_TEST( P );
99 CPPUNIT_TEST( N );
100 CPPUNIT_TEST( E );
101 CPPUNIT_TEST( F );
102 CPPUNIT_TEST( G );
103 CPPUNIT_TEST( S );
104 CPPUNIT_TEST( Asterisk );
105 CPPUNIT_TEST( Percent );
106 #ifdef wxLongLong_t
107 CPPUNIT_TEST( LongLong );
108 #endif
109
110 CPPUNIT_TEST( BigToSmallBuffer );
111 #if wxUSE_WXVSNPRINTF
112 CPPUNIT_TEST( WrongFormatStrings );
113 #endif // wxUSE_WXVSNPRINTF
114 CPPUNIT_TEST( Miscellaneous );
115 CPPUNIT_TEST_SUITE_END();
116
117 void C();
118 void D();
119 void X();
120 void O();
121 void P();
122 void N();
123 void E();
124 void F();
125 void G();
126 void S();
127 void Asterisk();
128 void Percent();
129 #ifdef wxLongLong_t
130 void LongLong();
131 #endif
132 void Unicode();
133
134 void BigToSmallBuffer();
135 #if wxUSE_WXVSNPRINTF
136 void WrongFormatStrings();
137 #endif // wxUSE_WXVSNPRINTF
138 void Miscellaneous();
139 void Misc(wxChar *buffer, int size);
140
141 // compares the expectedString and the result of wxVsnprintf() char by char
142 // for all its lenght (not only for first expectedLen chars) and also
143 // checks the return value
144 void DoMisc(int expectedLen, const wxString& expectedString,
145 size_t max, const wxChar *format, ...);
146
147 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
148 };
149
150 // register in the unnamed registry so that these tests are run by default
151 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
152
153 // also include in it's own registry so that these tests can be run alone
154 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
155
156 VsnprintfTestCase::VsnprintfTestCase()
157 {
158 }
159
160 void VsnprintfTestCase::C()
161 {
162 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
163
164 // NOTE:
165 // the NULL characters _can_ be passed to %c to e.g. create strings
166 // with embedded NULs (because strings are not always supposed to be
167 // NUL-terminated).
168
169 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
170 }
171
172 void VsnprintfTestCase::D()
173 {
174 CMP3("+123456", "%+d", 123456);
175 CMP3("-123456", "%d", -123456);
176 CMP3(" 123456", "% d", 123456);
177 CMP3(" 123456", "%10d", 123456);
178 CMP3("0000123456", "%010d", 123456);
179 CMP3("-123456 ", "%-10d", -123456);
180 }
181
182 void VsnprintfTestCase::X()
183 {
184 CMP3("ABCD", "%X", 0xABCD);
185 CMP3("0XABCD", "%#X", 0xABCD);
186 CMP3("0xabcd", "%#x", 0xABCD);
187 }
188
189 void VsnprintfTestCase::O()
190 {
191 CMP3("1234567", "%o", 01234567);
192 CMP3("01234567", "%#o", 01234567);
193 }
194
195 void VsnprintfTestCase::P()
196 {
197 // WARNING: printing of pointers is not fully standard.
198 // GNU prints them as %#x except for NULL pointers which are
199 // printed as '(nil)'.
200 // MSVC always print them as %8X on 32 bit systems and as %16X
201 // on 64 bit systems
202 #ifdef __VISUALC__
203 #if SIZEOF_VOID_P == 4
204 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
205 CMP3("00000000", "%p", (void*)NULL);
206 #elif SIZEOF_VOID_P == 8
207 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
208 CMP3("0000000000000000", "%p", (void*)NULL);
209 #endif
210 #elif defined(__GNUG__)
211 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
212 CMP3("(nil)", "%p", (void*)NULL);
213 #endif
214 }
215
216 void VsnprintfTestCase::N()
217 {
218 int nchar;
219
220 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
221 CPPUNIT_ASSERT_EQUAL( 7, nchar );
222 }
223
224 void VsnprintfTestCase::E()
225 {
226 // NB: there are no standards about the minimum exponent width
227 // (and the width of the %e conversion specifier refers to the
228 // mantissa, not to the exponent).
229 // Since newer MSVC versions use 3 digits as minimum exponent
230 // width while GNU libc uses 2 digits as minimum width, here we
231 // workaround this problem using for the exponent values with at
232 // least three digits.
233 // Some examples:
234 // printf("%e",2.342E+02);
235 // -> under MSVC7.1 prints: 2.342000e+002
236 // -> under GNU libc 2.4 prints: 2.342000e+02
237 CMP3("2.342000e+112", "%e",2.342E+112);
238 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
239 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
240 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
241
242 CMP3("-0.02342", "%G",-2.342E-02);
243 CMP3("3.1415E-116", "%G",3.1415e-116);
244 CMP3("0003.141500e+103", "%016e", 3141.5e100);
245 CMP3(" 3.141500e+103", "%16e", 3141.5e100);
246 CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
247 CMP3("3.142e+103", "%010.3e", 3141.5e100);
248 }
249
250 void VsnprintfTestCase::F()
251 {
252 CMP3("3.300000", "%5f", 3.3);
253 CMP3("3.000000", "%5f", 3.0);
254 CMP3("0.000100", "%5f", .999999E-4);
255 CMP3("0.000990", "%5f", .99E-3);
256 CMP3("3333.000000", "%5f", 3333.0);
257 }
258
259 void VsnprintfTestCase::G()
260 {
261 // NOTE: the same about E() testcase applies here...
262
263 CMP3(" 3.3", "%5g", 3.3);
264 CMP3(" 3", "%5g", 3.0);
265 CMP3("9.99999e-115", "%5g", .999999E-114);
266 CMP3("0.00099", "%5g", .99E-3);
267 CMP3(" 3333", "%5g", 3333.0);
268 CMP3(" 0.01", "%5g", 0.01);
269
270 CMP3(" 3", "%5.g", 3.3);
271 CMP3(" 3", "%5.g", 3.0);
272 CMP3("1e-114", "%5.g", .999999E-114);
273 CMP3("0.0001", "%5.g", 1.0E-4);
274 CMP3("0.001", "%5.g", .99E-3);
275 CMP3("3e+103", "%5.g", 3333.0E100);
276 CMP3(" 0.01", "%5.g", 0.01);
277
278 CMP3(" 3.3", "%5.2g", 3.3);
279 CMP3(" 3", "%5.2g", 3.0);
280 CMP3("1e-114", "%5.2g", .999999E-114);
281 CMP3("0.00099", "%5.2g", .99E-3);
282 CMP3("3.3e+103", "%5.2g", 3333.0E100);
283 CMP3(" 0.01", "%5.2g", 0.01);
284 }
285
286 void VsnprintfTestCase::S()
287 {
288 CMP3(" abc", "%5s", wxT("abc"));
289 CMP3(" a", "%5s", wxT("a"));
290 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
291 CMP3("abc ", "%-5s", wxT("abc"));
292 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
293
294 CMP3("abcde", "%.5s", wxT("abcdefghi"));
295
296 // do the same tests but with Unicode characters:
297 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
298 #define ALPHA "\x3B1"
299 #define BETA "\x3B2"
300 #define GAMMA "\x3B3"
301 #define DELTA "\x3B4"
302 #define EPSILON "\x3B5"
303 #define ZETA "\x3B6"
304 #define ETA "\x3B7"
305 #define THETA "\x3B8"
306 #define IOTA "\x3B9"
307
308 #define ABC ALPHA BETA GAMMA
309 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
310 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
311
312 CMP3(" " ABC, "%5s", wxT(ABC));
313 CMP3(" " ALPHA, "%5s", wxT(ALPHA));
314 CMP3(ABCDEFGHI, "%5s", wxT(ABCDEFGHI));
315 CMP3(ABC " ", "%-5s", wxT(ABC));
316 CMP3(ABCDEFGHI, "%-5s", wxT(ABCDEFGHI));
317
318 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
319 #endif
320
321 // test a string which have the NULL character after "ab";
322 // obviously it should be handled exactly like just as "ab"
323 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
324 }
325
326 void VsnprintfTestCase::Asterisk()
327 {
328 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
329 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
330 CMP5("0.1", "%*.*f", 3, 1, 0.123);
331
332 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
333 }
334
335 void VsnprintfTestCase::Percent()
336 {
337 // some tests without any argument passed through ...
338 CMP2("%", "%%");
339 CMP2("%%%", "%%%%%%");
340
341 CMP3("% abc", "%%%5s", wxT("abc"));
342 CMP3("% abc%", "%%%5s%%", wxT("abc"));
343
344 // do not test odd number of '%' symbols as different implementations
345 // of snprintf() give different outputs as this situation is not considered
346 // by any standard (in fact, GCC will also warn you about a spurious % if
347 // you write %%% as argument of some *printf function !)
348 // Compare(wxT("%"), wxT("%%%"));
349 }
350
351 #ifdef wxLongLong_t
352 void VsnprintfTestCase::LongLong()
353 {
354 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
355 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
356
357 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
358
359 #ifdef __WXMSW__
360 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
361 CMP3("123456789abcdef", "%I64x", (wxLongLong_t)0x123456789abcdef);
362 #endif
363 }
364 #endif
365
366 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
367 {
368 // Remember that wx*printf could be mapped either to system
369 // implementation or to wx implementation.
370 // In the first case, when the output buffer is too small, the returned
371 // value can be the number of characters required for the output buffer
372 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
373 // just a negative number, usually -1; (this is how e.g. MSVC's
374 // *printf() behaves). Luckily, in all implementations, when the
375 // output buffer is too small, it's nonetheless filled up to its max size.
376 //
377 // Note that in the second case (i.e. when we're using our own implementation),
378 // wxVsnprintf() will return the number of characters written in the standard
379 // output or
380 // -1 if there was an error in the format string
381 // maxSize+1 if the output buffer is too small
382
383 // test without positionals
384 CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
385 "%i %li - test - %d %.3f",
386 123, (long int)444444444, 555, -0.666);
387
388 #if wxUSE_PRINTF_POS_PARAMS
389 // test with positional
390 CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
391 "%4$.3f %1$i - test - %2$li %3$d",
392 123, (long int)444444444, 555, -0.666);
393 #endif
394
395 // test unicode/ansi conversion specifiers
396 // NB: this line will output two warnings like these, on GCC:
397 // warning: use of 'h' length modifier with 's' type character (i.e.
398 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
399 // be ignored as here we explicitely want to test the wxSnprintf()
400 // behaviour in such case
401
402 CMPTOSIZE(buffer, size,
403 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
404 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
405 L"unicode!!", L'W', "ansi!!", 'w');
406 }
407
408
409 // this test is only for our own implementation, the system implementation
410 // doesn't always give errors for invalid format strings (e.g. glibc doesn't)
411 // and as it's not required too (the behaviour is "undefined" according to the
412 // spec), there is really no sense in testing for it (and the first 2 formats
413 // in this test are not invalid at all in fact)
414 #if wxUSE_WXVSNPRINTF
415
416 void VsnprintfTestCase::WrongFormatStrings()
417 {
418 // test how wxVsnprintf() behaves with wrong format string:
419
420 // NB: the next 2 tests currently return an error but they shouldn't,
421 // according to POSIX reusing the parameters is allowed
422
423 // two positionals with the same index:
424 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
425 CPPUNIT_ASSERT(r != -1);
426
427 // three positionals with the same index mixed with other pos args:
428 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
429 CPPUNIT_ASSERT(r != -1);
430
431 // a missing positional arg: this should result in an error but not all
432 // implementations detect it (e.g. glibc doesn't)
433 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3);
434 CPPUNIT_ASSERT_EQUAL(-1, r);
435
436 // positional and non-positionals in the same format string:
437 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
438 CPPUNIT_ASSERT_EQUAL(-1, r);
439 }
440
441 #endif // wxUSE_WXVSNPRINTF
442
443 void VsnprintfTestCase::BigToSmallBuffer()
444 {
445 wxChar buf[1024], buf2[16], buf3[4], buf4;
446
447 Misc(buf, 1024);
448 Misc(buf2, 16);
449 Misc(buf3, 4);
450 Misc(&buf4, 1);
451 }
452
453 void VsnprintfTestCase::DoMisc(
454 int expectedLen,
455 const wxString& expectedString,
456 size_t max,
457 const wxChar *format, ...)
458 {
459 const size_t BUFSIZE = MAX_TEST_LEN - 1;
460 size_t i;
461 static int count = 0;
462
463 wxASSERT(max <= BUFSIZE);
464
465 for (i = 0; i < BUFSIZE; i++)
466 buf[i] = '*';
467 buf[BUFSIZE] = 0;
468
469 va_list ap;
470 va_start(ap, format);
471
472 int n = wxVsnprintf(buf, max, format, ap);
473
474 va_end(ap);
475
476 // Prepare messages so that it is possible to see from the error which
477 // test was running.
478 wxString errStr, overflowStr;
479 errStr << _T("No.: ") << ++count << _T(", expected: ") << expectedLen
480 << _T(" '") << expectedString << _T("', result: ");
481 overflowStr << errStr << _T("buffer overflow");
482 errStr << n << _T(" '") << buf << _T("'");
483
484 // turn them into std::strings
485 std::string errMsg(errStr.mb_str());
486 std::string overflowMsg(overflowStr.mb_str());
487
488 CPPUNIT_ASSERT_MESSAGE(errMsg,
489 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
490
491 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
492
493 for (i = max; i < BUFSIZE; i++)
494 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
495 }
496
497 void VsnprintfTestCase::Miscellaneous()
498 {
499 // expectedLen, expectedString, max, format, ...
500 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
501 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
502 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
503 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
504
505 DoMisc(6, wxT("123456"), 8, wxT("123456"));
506 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
507 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
508
509 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
510 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
511 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
512 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
513
514 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
515 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
516 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
517
518 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
519 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
520 }