]>
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
89 wxUnsafeSnprintf(T
*buf
, size_t len
, const wxChar
*fmt
, ...)
94 int rc
= wxVsnprintf(buf
, len
, fmt
, args
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 class VsnprintfTestCase
: public CppUnit::TestCase
111 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
122 CPPUNIT_TEST( Asterisk
);
123 CPPUNIT_TEST( Percent
);
125 CPPUNIT_TEST( LongLong
);
128 CPPUNIT_TEST( BigToSmallBuffer
);
129 #if wxUSE_WXVSNPRINTF
130 CPPUNIT_TEST( WrongFormatStrings
);
131 #endif // wxUSE_WXVSNPRINTF
132 CPPUNIT_TEST( Miscellaneous
);
133 CPPUNIT_TEST_SUITE_END();
152 void BigToSmallBuffer();
153 #if wxUSE_WXVSNPRINTF
154 void WrongFormatStrings();
155 #endif // wxUSE_WXVSNPRINTF
156 void Miscellaneous();
157 template<typename T
> void Misc(T
*buffer
, int size
);
159 // compares the expectedString and the result of wxVsnprintf() char by char
160 // for all its lenght (not only for first expectedLen chars) and also
161 // checks the return value
162 void DoMisc(int expectedLen
, const wxString
& expectedString
,
163 size_t max
, const wxChar
*format
, ...);
165 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
168 // register in the unnamed registry so that these tests are run by default
169 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
171 // also include in it's own registry so that these tests can be run alone
172 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
174 VsnprintfTestCase::VsnprintfTestCase()
178 void VsnprintfTestCase::C()
180 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
183 // the NULL characters _can_ be passed to %c to e.g. create strings
184 // with embedded NULs (because strings are not always supposed to be
187 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
190 void VsnprintfTestCase::D()
192 CMP3("+123456", "%+d", 123456);
193 CMP3("-123456", "%d", -123456);
194 CMP3(" 123456", "% d", 123456);
195 CMP3(" 123456", "%10d", 123456);
196 CMP3("0000123456", "%010d", 123456);
197 CMP3("-123456 ", "%-10d", -123456);
200 void VsnprintfTestCase::X()
202 CMP3("ABCD", "%X", 0xABCD);
203 CMP3("0XABCD", "%#X", 0xABCD);
204 CMP3("0xabcd", "%#x", 0xABCD);
207 void VsnprintfTestCase::O()
209 CMP3("1234567", "%o", 01234567);
210 CMP3("01234567", "%#o", 01234567);
213 void VsnprintfTestCase::P()
215 // WARNING: printing of pointers is not fully standard.
216 // GNU prints them as %#x except for NULL pointers which are
217 // printed as '(nil)'.
218 // MSVC always print them as %8X on 32 bit systems and as %16X
221 #if SIZEOF_VOID_P == 4
222 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
223 CMP3("00000000", "%p", (void*)NULL
);
224 #elif SIZEOF_VOID_P == 8
225 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
226 CMP3("0000000000000000", "%p", (void*)NULL
);
228 #elif defined(__GNUG__)
229 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
230 CMP3("(nil)", "%p", (void*)NULL
);
234 void VsnprintfTestCase::N()
238 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
239 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
242 void VsnprintfTestCase::E()
244 // NB: there are no standards about the minimum exponent width
245 // (and the width of the %e conversion specifier refers to the
246 // mantissa, not to the exponent).
247 // Since newer MSVC versions use 3 digits as minimum exponent
248 // width while GNU libc uses 2 digits as minimum width, here we
249 // workaround this problem using for the exponent values with at
250 // least three digits.
252 // printf("%e",2.342E+02);
253 // -> under MSVC7.1 prints: 2.342000e+002
254 // -> under GNU libc 2.4 prints: 2.342000e+02
255 CMP3("2.342000e+112", "%e",2.342E+112);
256 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
257 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
258 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
260 CMP3("-0.02342", "%G",-2.342E-02);
261 CMP3("3.1415E-116", "%G",3.1415e-116);
262 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
263 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
264 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
265 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
268 void VsnprintfTestCase::F()
270 CMP3("3.300000", "%5f", 3.3);
271 CMP3("3.000000", "%5f", 3.0);
272 CMP3("0.000100", "%5f", .999999E-4);
273 CMP3("0.000990", "%5f", .99E-3);
274 CMP3("3333.000000", "%5f", 3333.0);
277 void VsnprintfTestCase::G()
279 // NOTE: the same about E() testcase applies here...
281 CMP3(" 3.3", "%5g", 3.3);
282 CMP3(" 3", "%5g", 3.0);
283 CMP3("9.99999e-115", "%5g", .999999E-114);
284 CMP3("0.00099", "%5g", .99E-3);
285 CMP3(" 3333", "%5g", 3333.0);
286 CMP3(" 0.01", "%5g", 0.01);
288 CMP3(" 3", "%5.g", 3.3);
289 CMP3(" 3", "%5.g", 3.0);
290 CMP3("1e-114", "%5.g", .999999E-114);
291 CMP3("0.0001", "%5.g", 1.0E-4);
292 CMP3("0.001", "%5.g", .99E-3);
293 CMP3("3e+103", "%5.g", 3333.0E100
);
294 CMP3(" 0.01", "%5.g", 0.01);
296 CMP3(" 3.3", "%5.2g", 3.3);
297 CMP3(" 3", "%5.2g", 3.0);
298 CMP3("1e-114", "%5.2g", .999999E-114);
299 CMP3("0.00099", "%5.2g", .99E-3);
300 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
301 CMP3(" 0.01", "%5.2g", 0.01);
304 void VsnprintfTestCase::S()
306 CMP3(" abc", "%5s", wxT("abc"));
307 CMP3(" a", "%5s", wxT("a"));
308 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
309 CMP3("abc ", "%-5s", wxT("abc"));
310 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
312 CMP3("abcde", "%.5s", wxT("abcdefghi"));
314 // do the same tests but with Unicode characters:
315 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
316 #define ALPHA "\x3B1"
318 #define GAMMA "\x3B3"
319 #define DELTA "\x3B4"
320 #define EPSILON "\x3B5"
323 #define THETA "\x3B8"
326 #define ABC ALPHA BETA GAMMA
327 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
328 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
330 CMP3(" " ABC
, "%5s", wxT(ABC
));
331 CMP3(" " ALPHA
, "%5s", wxT(ALPHA
));
332 CMP3(ABCDEFGHI
, "%5s", wxT(ABCDEFGHI
));
333 CMP3(ABC
" ", "%-5s", wxT(ABC
));
334 CMP3(ABCDEFGHI
, "%-5s", wxT(ABCDEFGHI
));
336 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
339 // test a string which have the NULL character after "ab";
340 // obviously it should be handled exactly like just as "ab"
341 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
344 void VsnprintfTestCase::Asterisk()
346 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
347 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
348 CMP5("0.1", "%*.*f", 3, 1, 0.123);
350 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
353 void VsnprintfTestCase::Percent()
355 // some tests without any argument passed through ...
357 CMP2("%%%", "%%%%%%");
359 CMP3("% abc", "%%%5s", wxT("abc"));
360 CMP3("% abc%", "%%%5s%%", wxT("abc"));
362 // do not test odd number of '%' symbols as different implementations
363 // of snprintf() give different outputs as this situation is not considered
364 // by any standard (in fact, GCC will also warn you about a spurious % if
365 // you write %%% as argument of some *printf function !)
366 // Compare(wxT("%"), wxT("%%%"));
370 void VsnprintfTestCase::LongLong()
372 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
373 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
375 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
378 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
379 CMP3("123456789abcdef", "%I64x", (wxLongLong_t
)0x123456789abcdef);
385 void VsnprintfTestCase::Misc(T
*buffer
, int size
)
387 // Remember that wx*printf could be mapped either to system
388 // implementation or to wx implementation.
389 // In the first case, when the output buffer is too small, the returned
390 // value can be the number of characters required for the output buffer
391 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
392 // just a negative number, usually -1; (this is how e.g. MSVC's
393 // *printf() behaves). Luckily, in all implementations, when the
394 // output buffer is too small, it's nonetheless filled up to its max size.
396 // Note that in the second case (i.e. when we're using our own implementation),
397 // wxVsnprintf() will return the number of characters written in the standard
399 // -1 if there was an error in the format string
400 // maxSize+1 if the output buffer is too small
402 // test without positionals
403 CMPTOSIZE(buffer
, size
, "123 444444444 - test - 555 -0.666",
404 "%i %li - test - %d %.3f",
405 123, (long int)444444444, 555, -0.666);
407 #if wxUSE_PRINTF_POS_PARAMS
408 // test with positional
409 CMPTOSIZE(buffer
, size
, "-0.666 123 - test - 444444444 555",
410 "%4$.3f %1$i - test - %2$li %3$d",
411 123, (long int)444444444, 555, -0.666);
414 // test unicode/ansi conversion specifiers
416 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
417 // format and gcc would warn about this otherwise
419 r
= wxUnsafeSnprintf(buffer
, size
,
420 _T("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
421 L
"unicode!!", L
'W', "ansi!!", 'w');
422 CPPUNIT_ASSERT( r
!= -1 );
423 CPPUNIT_ASSERT_EQUAL(
424 wxString(wxT("unicode string: unicode!! W - ansi string: ansi!! w\n\n")).Left(size
- 1),
430 // this test is only for our own implementation, the system implementation
431 // doesn't always give errors for invalid format strings (e.g. glibc doesn't)
432 // and as it's not required too (the behaviour is "undefined" according to the
433 // spec), there is really no sense in testing for it (and the first 2 formats
434 // in this test are not invalid at all in fact)
435 #if wxUSE_WXVSNPRINTF
437 void VsnprintfTestCase::WrongFormatStrings()
439 // test how wxVsnprintf() behaves with wrong format string:
441 // NB: the next 2 tests currently return an error but they shouldn't,
442 // according to POSIX reusing the parameters is allowed
444 // two positionals with the same index:
445 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$s %1$s"), "hello");
446 CPPUNIT_ASSERT(r
!= -1);
448 // three positionals with the same index mixed with other pos args:
449 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
450 CPPUNIT_ASSERT(r
!= -1);
452 // a missing positional arg: this should result in an error but not all
453 // implementations detect it (e.g. glibc doesn't)
454 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %3$d"), 1, 2, 3);
455 CPPUNIT_ASSERT_EQUAL(-1, r
);
457 // positional and non-positionals in the same format string:
458 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %d %3$d"), 1, 2, 3);
459 CPPUNIT_ASSERT_EQUAL(-1, r
);
462 #endif // wxUSE_WXVSNPRINTF
464 void VsnprintfTestCase::BigToSmallBuffer()
466 char bufa
[1024], bufa2
[16], bufa3
[4], bufa4
;
469 wchar_t bufw
[1024], bufw2
[16], bufw3
[4], bufw4
;
474 #endif // wxUSE_UNICODE
482 void VsnprintfTestCase::DoMisc(
484 const wxString
& expectedString
,
486 const wxChar
*format
, ...)
488 const size_t BUFSIZE
= MAX_TEST_LEN
- 1;
490 static int count
= 0;
492 wxASSERT(max
<= BUFSIZE
);
494 for (i
= 0; i
< BUFSIZE
; i
++)
499 va_start(ap
, format
);
501 int n
= wxVsnprintf(buf
, max
, format
, ap
);
505 // Prepare messages so that it is possible to see from the error which
507 wxString errStr
, overflowStr
;
508 errStr
<< _T("No.: ") << ++count
<< _T(", expected: ") << expectedLen
509 << _T(" '") << expectedString
<< _T("', result: ");
510 overflowStr
<< errStr
<< _T("buffer overflow");
511 errStr
<< n
<< _T(" '") << buf
<< _T("'");
513 // turn them into std::strings
514 std::string
errMsg(errStr
.mb_str());
515 std::string
overflowMsg(overflowStr
.mb_str());
517 CPPUNIT_ASSERT_MESSAGE(errMsg
,
518 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
520 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
522 for (i
= max
; i
< BUFSIZE
; i
++)
523 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
526 void VsnprintfTestCase::Miscellaneous()
528 // expectedLen, expectedString, max, format, ...
529 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
530 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
531 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
532 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
534 DoMisc(6, wxT("123456"), 8, wxT("123456"));
535 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
536 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
538 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
539 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
540 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
541 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
543 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
544 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
545 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
547 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
548 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);