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)
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
24 #include "wx/wxchar.h"
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
34 // ----------------------------------------------------------------------------
35 // global utilities for testing
36 // ----------------------------------------------------------------------------
38 #define MAX_TEST_LEN 1024
41 static wxChar buf
[MAX_TEST_LEN
];
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)
49 #define ASSERT_STR_EQUAL( a, b ) \
50 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
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 );
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 );
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 );
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 );
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), \
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class VsnprintfTestCase
: public CppUnit
::TestCase
93 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
104 CPPUNIT_TEST( Asterisk
);
105 CPPUNIT_TEST( Percent
);
107 CPPUNIT_TEST( LongLong
);
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();
134 void BigToSmallBuffer();
135 #if wxUSE_WXVSNPRINTF
136 void WrongFormatStrings();
137 #endif // wxUSE_WXVSNPRINTF
138 void Miscellaneous();
139 void Misc(wxChar
*buffer
, int size
);
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
, ...);
147 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
150 // register in the unnamed registry so that these tests are run by default
151 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
153 // also include in it's own registry so that these tests can be run alone
154 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
156 VsnprintfTestCase
::VsnprintfTestCase()
160 void VsnprintfTestCase
::C()
162 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
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
169 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
172 void VsnprintfTestCase
::D()
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);
182 void VsnprintfTestCase
::X()
184 CMP3("ABCD", "%X", 0xABCD);
185 CMP3("0XABCD", "%#X", 0xABCD);
186 CMP3("0xabcd", "%#x", 0xABCD);
189 void VsnprintfTestCase
::O()
191 CMP3("1234567", "%o", 01234567);
192 CMP3("01234567", "%#o", 01234567);
195 void VsnprintfTestCase
::P()
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
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
);
210 #elif defined(__GNUG__)
211 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
212 CMP3("(nil)", "%p", (void*)NULL
);
216 void VsnprintfTestCase
::N()
220 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
221 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
224 void VsnprintfTestCase
::E()
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.
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);
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
);
250 void VsnprintfTestCase
::F()
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);
259 void VsnprintfTestCase
::G()
261 // NOTE: the same about E() testcase applies here...
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);
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);
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);
286 void VsnprintfTestCase
::S()
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"));
294 CMP3("abcde", "%.5s", wxT("abcdefghi"));
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"
300 #define GAMMA "\x3B3"
301 #define DELTA "\x3B4"
302 #define EPSILON "\x3B5"
305 #define THETA "\x3B8"
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
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
));
318 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
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"));
326 void VsnprintfTestCase
::Asterisk()
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);
332 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
335 void VsnprintfTestCase
::Percent()
337 // some tests without any argument passed through ...
339 CMP2("%%%", "%%%%%%");
341 CMP3("% abc", "%%%5s", wxT("abc"));
342 CMP3("% abc%", "%%%5s%%", wxT("abc"));
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("%%%"));
352 void VsnprintfTestCase
::LongLong()
354 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
355 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
357 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
360 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
361 CMP3("123456789abcdef", "%I64x", (wxLongLong_t
)0x123456789abcdef);
366 void VsnprintfTestCase
::Misc(wxChar
*buffer
, int size
)
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.
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
380 // -1 if there was an error in the format string
381 // maxSize+1 if the output buffer is too small
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);
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);
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
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');
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
416 void VsnprintfTestCase
::WrongFormatStrings()
418 // test how wxVsnprintf() behaves with wrong format string:
420 // NB: the next 2 tests currently return an error but they shouldn't,
421 // according to POSIX reusing the parameters is allowed
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);
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);
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
);
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
);
441 #endif // wxUSE_WXVSNPRINTF
443 void VsnprintfTestCase
::BigToSmallBuffer()
445 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;
453 void VsnprintfTestCase
::DoMisc(
455 const wxString
& expectedString
,
457 const wxChar
*format
, ...)
459 const size_t BUFSIZE
= MAX_TEST_LEN
- 1;
461 static int count
= 0;
463 wxASSERT(max
<= BUFSIZE
);
465 for (i
= 0; i
< BUFSIZE
; i
++)
470 va_start(ap
, format
);
472 int n
= wxVsnprintf(buf
, max
, format
, ap
);
476 // Prepare messages so that it is possible to see from the error which
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("'");
484 // turn them into std::strings
485 std
::string
errMsg(errStr
.mb_str());
486 std
::string
overflowMsg(overflowStr
.mb_str());
488 CPPUNIT_ASSERT_MESSAGE(errMsg
,
489 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
491 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
493 for (i
= max
; i
< BUFSIZE
; i
++)
494 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
497 void VsnprintfTestCase
::Miscellaneous()
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);
505 DoMisc(6, wxT("123456"), 8, wxT("123456"));
506 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
507 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
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);
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"));
518 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
519 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);