]>
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 // ----------------------------------------------------------------------------
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), \
81 // this is the same as wxSnprintf() but it passes the format string to
82 // wxVsnprintf() without using ATTRIBUTE_PRINTF and thus suppresses the gcc
83 // checks (and resulting warnings) for the format string
85 // use with extreme care and only when you're really sure the warnings must be
88 wxUnsafeSnprintf(wxChar
*buf
, size_t len
, const wxChar
*fmt
, ...)
93 int rc
= wxVsnprintf(buf
, len
, fmt
, args
);
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 class VsnprintfTestCase
: public CppUnit::TestCase
110 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
121 CPPUNIT_TEST( Asterisk
);
122 CPPUNIT_TEST( Percent
);
124 CPPUNIT_TEST( LongLong
);
127 CPPUNIT_TEST( BigToSmallBuffer
);
128 #if wxUSE_WXVSNPRINTF
129 CPPUNIT_TEST( WrongFormatStrings
);
130 #endif // wxUSE_WXVSNPRINTF
131 CPPUNIT_TEST( Miscellaneous
);
132 CPPUNIT_TEST_SUITE_END();
151 void BigToSmallBuffer();
152 #if wxUSE_WXVSNPRINTF
153 void WrongFormatStrings();
154 #endif // wxUSE_WXVSNPRINTF
155 void Miscellaneous();
156 void Misc(wxChar
*buffer
, int size
);
158 // compares the expectedString and the result of wxVsnprintf() char by char
159 // for all its lenght (not only for first expectedLen chars) and also
160 // checks the return value
161 void DoMisc(int expectedLen
, const wxString
& expectedString
,
162 size_t max
, const wxChar
*format
, ...);
164 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
167 // register in the unnamed registry so that these tests are run by default
168 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
170 // also include in it's own registry so that these tests can be run alone
171 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
173 VsnprintfTestCase::VsnprintfTestCase()
177 void VsnprintfTestCase::C()
179 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
182 // the NULL characters _can_ be passed to %c to e.g. create strings
183 // with embedded NULs (because strings are not always supposed to be
186 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
189 void VsnprintfTestCase::D()
191 CMP3("+123456", "%+d", 123456);
192 CMP3("-123456", "%d", -123456);
193 CMP3(" 123456", "% d", 123456);
194 CMP3(" 123456", "%10d", 123456);
195 CMP3("0000123456", "%010d", 123456);
196 CMP3("-123456 ", "%-10d", -123456);
199 void VsnprintfTestCase::X()
201 CMP3("ABCD", "%X", 0xABCD);
202 CMP3("0XABCD", "%#X", 0xABCD);
203 CMP3("0xabcd", "%#x", 0xABCD);
206 void VsnprintfTestCase::O()
208 CMP3("1234567", "%o", 01234567);
209 CMP3("01234567", "%#o", 01234567);
212 void VsnprintfTestCase::P()
214 // WARNING: printing of pointers is not fully standard.
215 // GNU prints them as %#x except for NULL pointers which are
216 // printed as '(nil)'.
217 // MSVC always print them as %8X on 32 bit systems and as %16X
220 #if SIZEOF_VOID_P == 4
221 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
222 CMP3("00000000", "%p", (void*)NULL
);
223 #elif SIZEOF_VOID_P == 8
224 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
225 CMP3("0000000000000000", "%p", (void*)NULL
);
227 #elif defined(__GNUG__)
228 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
229 CMP3("(nil)", "%p", (void*)NULL
);
233 void VsnprintfTestCase::N()
237 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
238 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
241 void VsnprintfTestCase::E()
243 // NB: there are no standards about the minimum exponent width
244 // (and the width of the %e conversion specifier refers to the
245 // mantissa, not to the exponent).
246 // Since newer MSVC versions use 3 digits as minimum exponent
247 // width while GNU libc uses 2 digits as minimum width, here we
248 // workaround this problem using for the exponent values with at
249 // least three digits.
251 // printf("%e",2.342E+02);
252 // -> under MSVC7.1 prints: 2.342000e+002
253 // -> under GNU libc 2.4 prints: 2.342000e+02
254 CMP3("2.342000e+112", "%e",2.342E+112);
255 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
256 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
257 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
259 CMP3("-0.02342", "%G",-2.342E-02);
260 CMP3("3.1415E-116", "%G",3.1415e-116);
261 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
262 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
263 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
264 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
267 void VsnprintfTestCase::F()
269 CMP3("3.300000", "%5f", 3.3);
270 CMP3("3.000000", "%5f", 3.0);
271 CMP3("0.000100", "%5f", .999999E-4);
272 CMP3("0.000990", "%5f", .99E-3);
273 CMP3("3333.000000", "%5f", 3333.0);
276 void VsnprintfTestCase::G()
278 // NOTE: the same about E() testcase applies here...
280 CMP3(" 3.3", "%5g", 3.3);
281 CMP3(" 3", "%5g", 3.0);
282 CMP3("9.99999e-115", "%5g", .999999E-114);
283 CMP3("0.00099", "%5g", .99E-3);
284 CMP3(" 3333", "%5g", 3333.0);
285 CMP3(" 0.01", "%5g", 0.01);
287 CMP3(" 3", "%5.g", 3.3);
288 CMP3(" 3", "%5.g", 3.0);
289 CMP3("1e-114", "%5.g", .999999E-114);
290 CMP3("0.0001", "%5.g", 1.0E-4);
291 CMP3("0.001", "%5.g", .99E-3);
292 CMP3("3e+103", "%5.g", 3333.0E100
);
293 CMP3(" 0.01", "%5.g", 0.01);
295 CMP3(" 3.3", "%5.2g", 3.3);
296 CMP3(" 3", "%5.2g", 3.0);
297 CMP3("1e-114", "%5.2g", .999999E-114);
298 CMP3("0.00099", "%5.2g", .99E-3);
299 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
300 CMP3(" 0.01", "%5.2g", 0.01);
303 void VsnprintfTestCase::S()
305 CMP3(" abc", "%5s", wxT("abc"));
306 CMP3(" a", "%5s", wxT("a"));
307 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
308 CMP3("abc ", "%-5s", wxT("abc"));
309 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
311 CMP3("abcde", "%.5s", wxT("abcdefghi"));
313 // do the same tests but with Unicode characters:
314 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
315 #define ALPHA "\x3B1"
317 #define GAMMA "\x3B3"
318 #define DELTA "\x3B4"
319 #define EPSILON "\x3B5"
322 #define THETA "\x3B8"
325 #define ABC ALPHA BETA GAMMA
326 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
327 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
329 CMP3(" " ABC
, "%5s", wxT(ABC
));
330 CMP3(" " ALPHA
, "%5s", wxT(ALPHA
));
331 CMP3(ABCDEFGHI
, "%5s", wxT(ABCDEFGHI
));
332 CMP3(ABC
" ", "%-5s", wxT(ABC
));
333 CMP3(ABCDEFGHI
, "%-5s", wxT(ABCDEFGHI
));
335 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
338 // test a string which have the NULL character after "ab";
339 // obviously it should be handled exactly like just as "ab"
340 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
343 void VsnprintfTestCase::Asterisk()
345 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
346 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
347 CMP5("0.1", "%*.*f", 3, 1, 0.123);
349 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
352 void VsnprintfTestCase::Percent()
354 // some tests without any argument passed through ...
356 CMP2("%%%", "%%%%%%");
358 CMP3("% abc", "%%%5s", wxT("abc"));
359 CMP3("% abc%", "%%%5s%%", wxT("abc"));
361 // do not test odd number of '%' symbols as different implementations
362 // of snprintf() give different outputs as this situation is not considered
363 // by any standard (in fact, GCC will also warn you about a spurious % if
364 // you write %%% as argument of some *printf function !)
365 // Compare(wxT("%"), wxT("%%%"));
369 void VsnprintfTestCase::LongLong()
371 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
372 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
374 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
377 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
378 CMP3("123456789abcdef", "%I64x", (wxLongLong_t
)0x123456789abcdef);
383 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
385 // Remember that wx*printf could be mapped either to system
386 // implementation or to wx implementation.
387 // In the first case, when the output buffer is too small, the returned
388 // value can be the number of characters required for the output buffer
389 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
390 // just a negative number, usually -1; (this is how e.g. MSVC's
391 // *printf() behaves). Luckily, in all implementations, when the
392 // output buffer is too small, it's nonetheless filled up to its max size.
394 // Note that in the second case (i.e. when we're using our own implementation),
395 // wxVsnprintf() will return the number of characters written in the standard
397 // -1 if there was an error in the format string
398 // maxSize+1 if the output buffer is too small
400 // test without positionals
401 CMPTOSIZE(buffer
, size
, "123 444444444 - test - 555 -0.666",
402 "%i %li - test - %d %.3f",
403 123, (long int)444444444, 555, -0.666);
405 #if wxUSE_PRINTF_POS_PARAMS
406 // test with positional
407 CMPTOSIZE(buffer
, size
, "-0.666 123 - test - 444444444 555",
408 "%4$.3f %1$i - test - %2$li %3$d",
409 123, (long int)444444444, 555, -0.666);
412 // test unicode/ansi conversion specifiers
414 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
415 // format and gcc would warn about this otherwise
417 r
= wxUnsafeSnprintf(buffer
, size
,
418 _T("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
419 L
"unicode!!", L
'W', "ansi!!", 'w');
420 CPPUNIT_ASSERT( r
!= -1 );
421 CPPUNIT_ASSERT_EQUAL(
422 wxString(wxT("unicode string: unicode!! W - ansi string: ansi!! w\n\n")).Left(size
- 1),
428 // this test is only for our own implementation, the system implementation
429 // doesn't always give errors for invalid format strings (e.g. glibc doesn't)
430 // and as it's not required too (the behaviour is "undefined" according to the
431 // spec), there is really no sense in testing for it (and the first 2 formats
432 // in this test are not invalid at all in fact)
433 #if wxUSE_WXVSNPRINTF
435 void VsnprintfTestCase::WrongFormatStrings()
437 // test how wxVsnprintf() behaves with wrong format string:
439 // NB: the next 2 tests currently return an error but they shouldn't,
440 // according to POSIX reusing the parameters is allowed
442 // two positionals with the same index:
443 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$s %1$s"), "hello");
444 CPPUNIT_ASSERT(r
!= -1);
446 // three positionals with the same index mixed with other pos args:
447 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
448 CPPUNIT_ASSERT(r
!= -1);
450 // a missing positional arg: this should result in an error but not all
451 // implementations detect it (e.g. glibc doesn't)
452 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %3$d"), 1, 2, 3);
453 CPPUNIT_ASSERT_EQUAL(-1, r
);
455 // positional and non-positionals in the same format string:
456 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %d %3$d"), 1, 2, 3);
457 CPPUNIT_ASSERT_EQUAL(-1, r
);
460 #endif // wxUSE_WXVSNPRINTF
462 void VsnprintfTestCase::BigToSmallBuffer()
464 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;
472 void VsnprintfTestCase::DoMisc(
474 const wxString
& expectedString
,
476 const wxChar
*format
, ...)
478 const size_t BUFSIZE
= MAX_TEST_LEN
- 1;
480 static int count
= 0;
482 wxASSERT(max
<= BUFSIZE
);
484 for (i
= 0; i
< BUFSIZE
; i
++)
489 va_start(ap
, format
);
491 int n
= wxVsnprintf(buf
, max
, format
, ap
);
495 // Prepare messages so that it is possible to see from the error which
497 wxString errStr
, overflowStr
;
498 errStr
<< _T("No.: ") << ++count
<< _T(", expected: ") << expectedLen
499 << _T(" '") << expectedString
<< _T("', result: ");
500 overflowStr
<< errStr
<< _T("buffer overflow");
501 errStr
<< n
<< _T(" '") << buf
<< _T("'");
503 // turn them into std::strings
504 std::string
errMsg(errStr
.mb_str());
505 std::string
overflowMsg(overflowStr
.mb_str());
507 CPPUNIT_ASSERT_MESSAGE(errMsg
,
508 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
510 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
512 for (i
= max
; i
< BUFSIZE
; i
++)
513 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
516 void VsnprintfTestCase::Miscellaneous()
518 // expectedLen, expectedString, max, format, ...
519 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
520 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
521 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
522 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
524 DoMisc(6, wxT("123456"), 8, wxT("123456"));
525 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
526 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
528 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
529 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
530 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
531 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
533 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
534 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
535 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
537 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
538 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);