]>
git.saurik.com Git - wxWidgets.git/blob - 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)
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
28 #include "wx/wxchar.h"
32 // NOTE: for more info about the specification of wxVsnprintf() behaviour you can
33 // refer to the following page of the GNU libc manual:
34 // http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
38 // ----------------------------------------------------------------------------
39 // global utilities for testing
40 // ----------------------------------------------------------------------------
42 #define MAX_TEST_LEN 1024
45 static wxChar buf
[MAX_TEST_LEN
];
48 // these macros makes it possible to write all tests without repeating a lot
49 // of times the wxT() macro
50 // NOTE: you should use expected strings with these macros which do not exceed
51 // MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
53 #define ASSERT_STR_EQUAL( a, b ) \
54 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
56 #define CMP6(expected, fmt, y, z, w, t) \
57 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w, t); \
58 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
59 ASSERT_STR_EQUAL( wxT(expected), buf );
61 #define CMP5(expected, fmt, y, z, w) \
62 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w); \
63 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
64 ASSERT_STR_EQUAL( wxT(expected), buf );
66 #define CMP4(expected, fmt, y, z) \
67 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z); \
68 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
69 ASSERT_STR_EQUAL( wxT(expected), buf );
71 #define CMP3(expected, fmt, y) \
72 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
73 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
74 ASSERT_STR_EQUAL( wxT(expected), buf );
76 #define CMP2(expected, fmt) \
77 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt)); \
78 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
79 ASSERT_STR_EQUAL( wxT(expected), buf );
81 // NOTE: this macro is used also with too-small buffers (see Miscellaneous())
82 // test function, thus the return value can be > size and thus we
83 // cannot check if r == (int)wxStrlen(buf)
84 #define CMPTOSIZE(buffer, size, failuremsg, expected, fmt, x, y, z, w) \
85 r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
86 CPPUNIT_ASSERT( r > 0 ); \
87 CPPUNIT_ASSERT_EQUAL_MESSAGE( \
89 wxString(wxT(expected)).Left(size - 1), \
92 // this is the same as wxSnprintf() but it passes the format string to
93 // wxVsnprintf() without using WX_ATTRIBUTE_PRINTF and thus suppresses the gcc
94 // checks (and resulting warnings) for the format string
96 // use with extreme care and only when you're really sure the warnings must be
100 wxUnsafeSnprintf(T
*buf
, size_t len
, const wxChar
*fmt
, ...)
105 int rc
= wxVsnprintf(buf
, len
, fmt
, args
);
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 class VsnprintfTestCase
: public CppUnit::TestCase
119 VsnprintfTestCase() {}
121 virtual void setUp();
124 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
135 CPPUNIT_TEST( Asterisk
);
136 CPPUNIT_TEST( Percent
);
138 CPPUNIT_TEST( LongLong
);
141 CPPUNIT_TEST( BigToSmallBuffer
);
142 #if wxUSE_WXVSNPRINTF
143 CPPUNIT_TEST( WrongFormatStrings
);
144 #endif // wxUSE_WXVSNPRINTF
145 CPPUNIT_TEST( Miscellaneous
);
146 CPPUNIT_TEST_SUITE_END();
166 void DoBigToSmallBuffer(T
*buffer
, int size
);
167 void BigToSmallBuffer();
169 #if wxUSE_WXVSNPRINTF
170 void WrongFormatStrings();
171 #endif // wxUSE_WXVSNPRINTF
173 // compares the expectedString and the result of wxVsnprintf() char by char
174 // for all its lenght (not only for first expectedLen chars) and also
175 // checks the return value
176 void DoMisc(int expectedLen
, const wxString
& expectedString
,
177 size_t max
, const wxChar
*format
, ...);
178 void Miscellaneous();
180 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
183 // register in the unnamed registry so that these tests are run by default
184 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
186 // also include in it's own registry so that these tests can be run alone
187 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
189 void VsnprintfTestCase::setUp()
191 // this call is required to avoid check failures when running on machines
192 // with a locale where the decimal point is not '.'
193 wxSetlocale(LC_ALL
, "C");
196 void VsnprintfTestCase::C()
198 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
201 // the NULL characters _can_ be passed to %c to e.g. create strings
202 // with embedded NULs (because strings are not always supposed to be
205 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
208 void VsnprintfTestCase::D()
210 CMP3("+123456", "%+d", 123456);
211 CMP3("-123456", "%d", -123456);
212 CMP3(" 123456", "% d", 123456);
213 CMP3(" 123456", "%10d", 123456);
214 CMP3("0000123456", "%010d", 123456);
215 CMP3("-123456 ", "%-10d", -123456);
218 void VsnprintfTestCase::X()
220 CMP3("ABCD", "%X", 0xABCD);
221 CMP3("0XABCD", "%#X", 0xABCD);
222 CMP3("0xabcd", "%#x", 0xABCD);
225 void VsnprintfTestCase::O()
227 CMP3("1234567", "%o", 01234567);
228 CMP3("01234567", "%#o", 01234567);
231 void VsnprintfTestCase::P()
233 // WARNING: printing of pointers is not fully standard.
234 // GNU prints them as %#x except for NULL pointers which are
235 // printed as '(nil)'.
236 // MSVC always print them as %8X on 32 bit systems and as %16X
238 // mingw32 uses MSVC CRT by default so uses the same rules
239 #if defined(__VISUALC__) || (defined(__MINGW32__) && !__USE_MINGW_ANSI_STDIO)
240 #if SIZEOF_VOID_P == 4
241 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
242 CMP3("00000000", "%p", (void*)NULL
);
243 #elif SIZEOF_VOID_P == 8
244 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
245 CMP3("0000000000000000", "%p", (void*)NULL
);
247 #elif defined(__MINGW32__)
248 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
249 CMP3("0", "%p", (void*)NULL
);
250 #elif defined(__GNUG__)
251 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
252 CMP3("(nil)", "%p", (void*)NULL
);
256 void VsnprintfTestCase::N()
260 wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%d %s%n\n"), 3, wxT("bears"), &nchar
);
261 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
264 void VsnprintfTestCase::E()
266 // NB: there are no standards about the minimum exponent width
267 // (and the width of the %e conversion specifier refers to the
268 // mantissa, not to the exponent).
269 // Since newer MSVC versions use 3 digits as minimum exponent
270 // width while GNU libc uses 2 digits as minimum width, here we
271 // workaround this problem using for the exponent values with at
272 // least three digits.
274 // printf("%e",2.342E+02);
275 // -> under MSVC7.1 prints: 2.342000e+002
276 // -> under GNU libc 2.4 prints: 2.342000e+02
277 CMP3("2.342000e+112", "%e",2.342E+112);
278 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
279 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
280 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
282 CMP3("-0.02342", "%G",-2.342E-02);
283 CMP3("3.1415E-116", "%G",3.1415e-116);
284 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
285 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
286 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
287 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
290 void VsnprintfTestCase::F()
292 CMP3("3.300000", "%5f", 3.3);
293 CMP3("3.000000", "%5f", 3.0);
294 CMP3("0.000100", "%5f", .999999E-4);
295 CMP3("0.000990", "%5f", .99E-3);
296 CMP3("3333.000000", "%5f", 3333.0);
299 void VsnprintfTestCase::G()
301 // NOTE: the same about E() testcase applies here...
303 CMP3(" 3.3", "%5g", 3.3);
304 CMP3(" 3", "%5g", 3.0);
305 CMP3("9.99999e-115", "%5g", .999999E-114);
306 CMP3("0.00099", "%5g", .99E-3);
307 CMP3(" 3333", "%5g", 3333.0);
308 CMP3(" 0.01", "%5g", 0.01);
310 CMP3(" 3", "%5.g", 3.3);
311 CMP3(" 3", "%5.g", 3.0);
312 CMP3("1e-114", "%5.g", .999999E-114);
313 CMP3("0.0001", "%5.g", 1.0E-4);
314 CMP3("0.001", "%5.g", .99E-3);
315 CMP3("3e+103", "%5.g", 3333.0E100
);
316 CMP3(" 0.01", "%5.g", 0.01);
318 CMP3(" 3.3", "%5.2g", 3.3);
319 CMP3(" 3", "%5.2g", 3.0);
320 CMP3("1e-114", "%5.2g", .999999E-114);
321 CMP3("0.00099", "%5.2g", .99E-3);
322 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
323 CMP3(" 0.01", "%5.2g", 0.01);
326 void VsnprintfTestCase::S()
328 CMP3(" abc", "%5s", wxT("abc"));
329 CMP3(" a", "%5s", wxT("a"));
330 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
331 CMP3("abc ", "%-5s", wxT("abc"));
332 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
334 CMP3("abcde", "%.5s", wxT("abcdefghi"));
336 // do the same tests but with Unicode characters:
339 // Unicode code points from U+03B1 to U+03B9 are the greek letters alpha-iota;
340 // UTF8 encoding of such code points is 0xCEB1 to 0xCEB9
342 #define ALPHA "\xCE\xB1"
344 #define ABC "\xCE\xB1\xCE\xB2\xCE\xB3"
346 #define ABCDE "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
347 // alpha+beta+gamma+delta+epsilon
348 #define ABCDEFGHI "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9"
349 // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
351 // the 'expected' and 'arg' parameters of this macro are supposed to be
353 #define CMP3_UTF8(expected, fmt, arg) \
354 CPPUNIT_ASSERT_EQUAL \
356 wxString::FromUTF8(expected).length(), \
357 wxSnprintf(buf, MAX_TEST_LEN, fmt, wxString::FromUTF8(arg)) \
359 CPPUNIT_ASSERT_EQUAL \
361 wxString::FromUTF8(expected), \
365 CMP3_UTF8(" " ABC
, "%5s", ABC
);
366 CMP3_UTF8(" " ALPHA
, "%5s", ALPHA
);
367 CMP3_UTF8(ABCDEFGHI
, "%5s", ABCDEFGHI
);
368 CMP3_UTF8(ABC
" ", "%-5s", ABC
);
369 CMP3_UTF8(ABCDEFGHI
, "%-5s", ABCDEFGHI
);
370 CMP3_UTF8(ABCDE
, "%.5s", ABCDEFGHI
);
371 #endif // wxUSE_UNICODE
373 // test a string which has a NULL character after "ab";
374 // obviously it should be handled exactly like just as "ab"
375 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
378 void VsnprintfTestCase::Asterisk()
380 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
381 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
382 CMP5("0.1", "%*.*f", 3, 1, 0.123);
384 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
386 CMP4(" a", "%*c", 8, 'a');
387 CMP4(" four", "%*s", 8, "four");
388 CMP6(" four four", "%*s %*s", 8, "four", 6, "four");
391 void VsnprintfTestCase::Percent()
393 // some tests without any argument passed through ...
395 CMP2("%%%", "%%%%%%");
397 CMP3("% abc", "%%%5s", wxT("abc"));
398 CMP3("% abc%", "%%%5s%%", wxT("abc"));
400 // do not test odd number of '%' symbols as different implementations
401 // of snprintf() give different outputs as this situation is not considered
402 // by any standard (in fact, GCC will also warn you about a spurious % if
403 // you write %%% as argument of some *printf function !)
404 // Compare(wxT("%"), wxT("%%%"));
408 void VsnprintfTestCase::LongLong()
410 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
411 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
413 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
416 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
417 CMP3("123456789abcdef", "%I64x", wxLL(0x123456789abcdef));
422 // this test is only for our own implementation, the system implementation
423 // doesn't always give errors for invalid format strings (e.g. glibc doesn't)
424 // and as it's not required too (the behaviour is "undefined" according to the
425 // spec), there is really no sense in testing for it
426 #if wxUSE_WXVSNPRINTF
428 void VsnprintfTestCase::WrongFormatStrings()
430 // test how wxVsnprintf() behaves with wrong format string:
433 // NB: the next 2 tests currently return an error but it would be nice
434 // if they didn't (see ticket #9367)
436 // two positionals with the same index:
437 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$s %1$s"), "hello");
438 CPPUNIT_ASSERT(r
!= -1);
440 // three positionals with the same index mixed with other pos args:
441 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
442 CPPUNIT_ASSERT(r
!= -1);
445 // a missing positional arg: this should result in an error but not all
446 // implementations detect it (e.g. glibc doesn't)
447 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %3$d"), 1, 2, 3);
448 CPPUNIT_ASSERT_EQUAL(-1, r
);
450 // positional and non-positionals in the same format string:
451 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %d %3$d"), 1, 2, 3);
452 CPPUNIT_ASSERT_EQUAL(-1, r
);
455 #endif // wxUSE_WXVSNPRINTF
457 // BigToSmallBuffer() test case helper:
459 void VsnprintfTestCase::DoBigToSmallBuffer(T
*buffer
, int size
)
461 // Remember that wx*printf could be mapped either to system
462 // implementation or to wx implementation.
463 // In the first case, when the output buffer is too small, the returned
464 // value can be the number of characters required for the output buffer
465 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
466 // just a negative number, usually -1; (this is how e.g. MSVC's
467 // *printf() behaves). Luckily, in all implementations, when the
468 // output buffer is too small, it's nonetheless filled up to its max size.
470 // Note that in the second case (i.e. when we're using our own implementation),
471 // wxVsnprintf() will return the number of characters written in the standard
473 // -1 if there was an error in the format string
474 // maxSize+1 if the output buffer is too small
477 errStr
<< "The size of the buffer was " << size
;
478 std::string
errMsg(errStr
.mb_str());
480 // test without positionals
481 CMPTOSIZE(buffer
, size
, errMsg
,
482 "123456789012 - test - 123 -4.567",
483 "%i%li - test - %d %.3f",
484 123, (long int)456789012, 123, -4.567);
486 #if wxUSE_PRINTF_POS_PARAMS
487 // test with positional
488 CMPTOSIZE(buffer
, size
, errMsg
,
489 "-4.567 123 - test - 456789012 123",
490 "%4$.3f %1$i - test - %2$li %3$d",
491 123, (long int)456789012, 123, -4.567);
494 // test unicode/ansi conversion specifiers
496 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
497 // format and gcc would warn about this otherwise
499 r
= wxUnsafeSnprintf(buffer
, size
,
500 wxT("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
501 L
"unicode", L
'U', "ansi", 'A');
503 wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size
- 1);
505 CPPUNIT_ASSERT( r
!= -1 );
506 CPPUNIT_ASSERT_EQUAL(
512 void VsnprintfTestCase::BigToSmallBuffer()
514 // VC6 can't compile this code
515 #if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
517 wchar_t bufw
[1024], bufw2
[16], bufw3
[4], bufw4
;
518 DoBigToSmallBuffer(bufw
, 1024);
519 DoBigToSmallBuffer(bufw2
, 16);
520 DoBigToSmallBuffer(bufw3
, 4);
521 DoBigToSmallBuffer(&bufw4
, 1);
522 #endif // wxUSE_UNICODE
524 char bufa
[1024], bufa2
[16], bufa3
[4], bufa4
;
525 DoBigToSmallBuffer(bufa
, 1024);
526 DoBigToSmallBuffer(bufa2
, 16);
527 DoBigToSmallBuffer(bufa3
, 4);
528 DoBigToSmallBuffer(&bufa4
, 1);
532 // Miscellaneous() test case helper:
533 void VsnprintfTestCase::DoMisc(
535 const wxString
& expectedString
,
537 const wxChar
*format
, ...)
539 const size_t BUFSIZE
= MAX_TEST_LEN
- 1;
541 static int count
= 0;
543 wxASSERT(max
<= BUFSIZE
);
545 for (i
= 0; i
< BUFSIZE
; i
++)
550 va_start(ap
, format
);
552 int n
= wxVsnprintf(buf
, max
, format
, ap
);
556 // Prepare messages so that it is possible to see from the error which
558 wxString errStr
, overflowStr
;
559 errStr
<< wxT("No.: ") << ++count
<< wxT(", expected: ") << expectedLen
560 << wxT(" '") << expectedString
<< wxT("', result: ");
561 overflowStr
<< errStr
<< wxT("buffer overflow");
562 errStr
<< n
<< wxT(" '") << buf
<< wxT("'");
564 // turn them into std::strings
565 std::string
errMsg(errStr
.mb_str());
566 std::string
overflowMsg(overflowStr
.mb_str());
568 CPPUNIT_ASSERT_MESSAGE(errMsg
,
569 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
571 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
573 for (i
= max
; i
< BUFSIZE
; i
++)
574 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
577 void VsnprintfTestCase::Miscellaneous()
579 // expectedLen, expectedString, max, format, ...
580 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
581 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
582 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
583 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
585 DoMisc(6, wxT("123456"), 8, wxT("123456"));
586 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
587 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
589 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
590 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
591 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
592 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
594 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
595 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
596 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
598 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
599 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
602 #endif // wxUSE_WXVSNPRINTF