]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
38a775fe214682742875e81182ace9cfe94e19e9
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 CPPUNIT_TEST( WrongFormatStrings
);
112 CPPUNIT_TEST( Miscellaneous
);
113 CPPUNIT_TEST_SUITE_END();
132 void BigToSmallBuffer();
133 void WrongFormatStrings();
134 void Miscellaneous();
135 void Misc(wxChar
*buffer
, int size
);
137 // compares the expectedString and the result of wxVsnprintf() char by char
138 // for all its lenght (not only for first expectedLen chars) and also
139 // checks the return value
140 void DoMisc(int expectedLen
, const wxString
& expectedString
,
141 size_t max
, const wxChar
*format
, ...);
143 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
146 // register in the unnamed registry so that these tests are run by default
147 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
149 // also include in it's own registry so that these tests can be run alone
150 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
152 VsnprintfTestCase::VsnprintfTestCase()
156 void VsnprintfTestCase::C()
158 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
161 // the NULL characters _can_ be passed to %c to e.g. create strings
162 // with embedded NULs (because strings are not always supposed to be
165 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
168 void VsnprintfTestCase::D()
170 CMP3("+123456", "%+d", 123456);
171 CMP3("-123456", "%d", -123456);
172 CMP3(" 123456", "% d", 123456);
173 CMP3(" 123456", "%10d", 123456);
174 CMP3("0000123456", "%010d", 123456);
175 CMP3("-123456 ", "%-10d", -123456);
178 void VsnprintfTestCase::X()
180 CMP3("ABCD", "%X", 0xABCD);
181 CMP3("0XABCD", "%#X", 0xABCD);
182 CMP3("0xabcd", "%#x", 0xABCD);
185 void VsnprintfTestCase::O()
187 CMP3("1234567", "%o", 01234567);
188 CMP3("01234567", "%#o", 01234567);
191 void VsnprintfTestCase::P()
193 // WARNING: printing of pointers is not fully standard.
194 // GNU prints them as %#x except for NULL pointers which are
195 // printed as '(nil)'.
196 // MSVC always print them as %8X on 32 bit systems and as %16X
199 #if SIZEOF_VOID_P == 4
200 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
201 CMP3("00000000", "%p", (void*)NULL
);
202 #elif SIZEOF_VOID_P == 8
203 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
204 CMP3("0000000000000000", "%p", (void*)NULL
);
206 #elif defined(__GNUG__)
207 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
208 CMP3("(nil)", "%p", (void*)NULL
);
212 void VsnprintfTestCase::N()
216 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
217 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
220 void VsnprintfTestCase::E()
222 // NB: there are no standards about the minimum exponent width
223 // (and the width of the %e conversion specifier refers to the
224 // mantissa, not to the exponent).
225 // Since newer MSVC versions use 3 digits as minimum exponent
226 // width while GNU libc uses 2 digits as minimum width, here we
227 // workaround this problem using for the exponent values with at
228 // least three digits.
230 // printf("%e",2.342E+02);
231 // -> under MSVC7.1 prints: 2.342000e+002
232 // -> under GNU libc 2.4 prints: 2.342000e+02
233 CMP3("2.342000e+112", "%e",2.342E+112);
234 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
235 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
236 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
238 CMP3("-0.02342", "%G",-2.342E-02);
239 CMP3("3.1415E-116", "%G",3.1415e-116);
240 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
241 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
242 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
243 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
246 void VsnprintfTestCase::F()
248 CMP3("3.300000", "%5f", 3.3);
249 CMP3("3.000000", "%5f", 3.0);
250 CMP3("0.000100", "%5f", .999999E-4);
251 CMP3("0.000990", "%5f", .99E-3);
252 CMP3("3333.000000", "%5f", 3333.0);
255 void VsnprintfTestCase::G()
257 // NOTE: the same about E() testcase applies here...
259 CMP3(" 3.3", "%5g", 3.3);
260 CMP3(" 3", "%5g", 3.0);
261 CMP3("9.99999e-115", "%5g", .999999E-114);
262 CMP3("0.00099", "%5g", .99E-3);
263 CMP3(" 3333", "%5g", 3333.0);
264 CMP3(" 0.01", "%5g", 0.01);
266 CMP3(" 3", "%5.g", 3.3);
267 CMP3(" 3", "%5.g", 3.0);
268 CMP3("1e-114", "%5.g", .999999E-114);
269 CMP3("0.0001", "%5.g", 1.0E-4);
270 CMP3("0.001", "%5.g", .99E-3);
271 CMP3("3e+103", "%5.g", 3333.0E100
);
272 CMP3(" 0.01", "%5.g", 0.01);
274 CMP3(" 3.3", "%5.2g", 3.3);
275 CMP3(" 3", "%5.2g", 3.0);
276 CMP3("1e-114", "%5.2g", .999999E-114);
277 CMP3("0.00099", "%5.2g", .99E-3);
278 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
279 CMP3(" 0.01", "%5.2g", 0.01);
282 void VsnprintfTestCase::S()
284 CMP3(" abc", "%5s", wxT("abc"));
285 CMP3(" a", "%5s", wxT("a"));
286 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
287 CMP3("abc ", "%-5s", wxT("abc"));
288 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
290 CMP3("abcde", "%.5s", wxT("abcdefghi"));
292 // do the same tests but with Unicode characters:
293 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
294 #define ALPHA "\x3B1"
296 #define GAMMA "\x3B3"
297 #define DELTA "\x3B4"
298 #define EPSILON "\x3B5"
301 #define THETA "\x3B8"
304 #define ABC ALPHA BETA GAMMA
305 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
306 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
308 CMP3(" " ABC
, "%5s", wxT(ABC
));
309 CMP3(" " ALPHA
, "%5s", wxT(ALPHA
));
310 CMP3(ABCDEFGHI
, "%5s", wxT(ABCDEFGHI
));
311 CMP3(ABC
" ", "%-5s", wxT(ABC
));
312 CMP3(ABCDEFGHI
, "%-5s", wxT(ABCDEFGHI
));
314 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
317 // test a string which have the NULL character after "ab";
318 // obviously it should be handled exactly like just as "ab"
319 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
322 void VsnprintfTestCase::Asterisk()
324 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
325 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
326 CMP5("0.1", "%*.*f", 3, 1, 0.123);
328 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
331 void VsnprintfTestCase::Percent()
333 // some tests without any argument passed through ...
335 CMP2("%%%", "%%%%%%");
337 CMP3("% abc", "%%%5s", wxT("abc"));
338 CMP3("% abc%", "%%%5s%%", wxT("abc"));
340 // do not test odd number of '%' symbols as different implementations
341 // of snprintf() give different outputs as this situation is not considered
342 // by any standard (in fact, GCC will also warn you about a spurious % if
343 // you write %%% as argument of some *printf function !)
344 // Compare(wxT("%"), wxT("%%%"));
348 void VsnprintfTestCase::LongLong()
350 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
351 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
353 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
356 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
357 CMP3("123456789abcdef", "%I64x", (wxLongLong_t
)0x123456789abcdef);
362 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
364 // Remember that wx*printf could be mapped either to system
365 // implementation or to wx implementation.
366 // In the first case, when the output buffer is too small, the returned
367 // value can be the number of characters required for the output buffer
368 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
369 // just a negative number, usually -1; (this is how e.g. MSVC's
370 // *printf() behaves). Luckily, in all implementations, when the
371 // output buffer is too small, it's nonetheless filled up to its max size.
373 // Note that in the second case (i.e. when we're using our own implementation),
374 // wxVsnprintf() will return the number of characters written in the standard
376 // -1 if there was an error in the format string
377 // maxSize+1 if the output buffer is too small
379 // test without positionals
380 CMPTOSIZE(buffer
, size
, "123 444444444 - test - 555 -0.666",
381 "%i %li - test - %d %.3f",
382 123, (long int)444444444, 555, -0.666);
384 #if wxUSE_PRINTF_POS_PARAMS
385 // test with positional
386 CMPTOSIZE(buffer
, size
, "-0.666 123 - test - 444444444 555",
387 "%4$.3f %1$i - test - %2$li %3$d",
388 123, (long int)444444444, 555, -0.666);
391 // test unicode/ansi conversion specifiers
392 // NB: this line will output two warnings like these, on GCC:
393 // warning: use of 'h' length modifier with 's' type character (i.e.
394 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
395 // be ignored as here we explicitely want to test the wxSnprintf()
396 // behaviour in such case
398 CMPTOSIZE(buffer
, size
,
399 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
400 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
401 L
"unicode!!", L
'W', "ansi!!", 'w');
404 void VsnprintfTestCase::WrongFormatStrings()
406 // test how wxVsnprintf() behaves with wrong format string:
408 #if wxUSE_PRINTF_POS_PARAMS
410 // two positionals with the same index:
411 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$s %1$s"), "hello");
412 CPPUNIT_ASSERT(r
== -1);
414 // three positionals with the same index mixed with other pos args:
415 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
416 CPPUNIT_ASSERT(r
== -1);
418 // a missing positional arg:
419 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %3$d"), 1, 2, 3);
420 CPPUNIT_ASSERT(r
== -1);
422 // positional and non-positionals in the same format string:
423 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %d %3$d"), 1, 2, 3);
424 CPPUNIT_ASSERT(r
== -1);
426 #endif // wxUSE_PRINTF_POS_PARAMS
429 void VsnprintfTestCase::BigToSmallBuffer()
431 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;
439 void VsnprintfTestCase::DoMisc(
441 const wxString
& expectedString
,
443 const wxChar
*format
, ...)
445 const size_t BUFSIZE
= MAX_TEST_LEN
- 1;
447 static int count
= 0;
449 wxASSERT(max
<= BUFSIZE
);
451 for (i
= 0; i
< BUFSIZE
; i
++)
456 va_start(ap
, format
);
458 int n
= wxVsnprintf(buf
, max
, format
, ap
);
462 // Prepare messages so that it is possible to see from the error which
464 wxString errStr
, overflowStr
;
465 errStr
<< _T("No.: ") << ++count
<< _T(", expected: ") << expectedLen
466 << _T(" '") << expectedString
<< _T("', result: ");
467 overflowStr
<< errStr
<< _T("buffer overflow");
468 errStr
<< n
<< _T(" '") << buf
<< _T("'");
470 // turn them into std::strings
471 std::string
errMsg(errStr
.mb_str());
472 std::string
overflowMsg(overflowStr
.mb_str());
474 CPPUNIT_ASSERT_MESSAGE(errMsg
,
475 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
477 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
479 for (i
= max
; i
< BUFSIZE
; i
++)
480 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
483 void VsnprintfTestCase::Miscellaneous()
485 // expectedLen, expectedString, max, format, ...
486 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
487 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
488 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
489 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
491 DoMisc(6, wxT("123456"), 8, wxT("123456"));
492 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
493 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
495 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
496 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
497 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
498 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
500 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
501 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
502 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
504 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
505 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);