]>
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"
29 #define MAX_TEST_LEN 1024
33 static wxChar buf
[MAX_TEST_LEN
];
36 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
38 #define ASSERT_STR_EQUAL( a, b ) \
39 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
41 #define CMP5(expected, x, y, z, w) \
42 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
43 CPPUNIT_ASSERT( r > 0 ); \
44 ASSERT_STR_EQUAL( wxT(expected), buf );
46 #define CMP4(expected, x, y, z) \
47 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
48 CPPUNIT_ASSERT( r > 0 ); \
49 ASSERT_STR_EQUAL( wxT(expected), buf );
51 #define CMP3(expected, x, y) \
52 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
53 CPPUNIT_ASSERT( r > 0 ); \
54 ASSERT_STR_EQUAL( wxT(expected), buf );
56 #define CMP2(expected, x) \
57 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
58 CPPUNIT_ASSERT( r > 0 ); \
59 ASSERT_STR_EQUAL( wxT(expected), buf );
61 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
62 r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
63 CPPUNIT_ASSERT( r > 0 ); \
64 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class VsnprintfTestCase
: public CppUnit::TestCase
79 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
89 CPPUNIT_TEST( Asterisk
);
90 CPPUNIT_TEST( Percent
);
92 CPPUNIT_TEST( LongLong
);
95 CPPUNIT_TEST( BigToSmallBuffer
);
96 CPPUNIT_TEST( WrongFormatStrings
);
97 CPPUNIT_TEST( Miscellaneous
);
98 CPPUNIT_TEST_SUITE_END();
116 void BigToSmallBuffer();
117 void WrongFormatStrings();
118 void Miscellaneous();
119 void Misc(wxChar
*buffer
, int size
);
121 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
124 // register in the unnamed registry so that these tests are run by default
125 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
127 // also include in it's own registry so that these tests can be run alone
128 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
130 VsnprintfTestCase::VsnprintfTestCase()
134 void VsnprintfTestCase::D()
136 CMP3("+123456", "%+d", 123456);
137 CMP3("-123456", "%d", -123456);
138 CMP3(" 123456", "% d", 123456);
139 CMP3(" 123456", "%10d", 123456);
140 CMP3("0000123456", "%010d", 123456);
141 CMP3("-123456 ", "%-10d", -123456);
144 void VsnprintfTestCase::X()
146 CMP3("ABCD", "%X", 0xABCD);
147 CMP3("0XABCD", "%#X", 0xABCD);
148 CMP3("0xabcd", "%#x", 0xABCD);
151 void VsnprintfTestCase::O()
153 CMP3("1234567", "%o", 01234567);
154 CMP3("01234567", "%#o", 01234567);
157 void VsnprintfTestCase::P()
159 // WARNING: printing of pointers is not fully standard.
160 // GNU prints them as %#x except for NULL pointers which are
161 // printed as '(nil)'.
162 // MSVC always print them as %8X on 32 bit systems and as %16X
165 #if SIZEOF_VOID_P == 4
166 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
167 CMP3("00000000", "%p", (void*)NULL
);
168 #elif SIZEOF_VOID_P == 8
169 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
170 CMP3("0000000000000000", "%p", (void*)NULL
);
172 #elif defined(__GNUG__)
173 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
174 CMP3("(nil)", "%p", (void*)NULL
);
178 void VsnprintfTestCase::N()
182 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
183 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
186 void VsnprintfTestCase::E()
188 // NB: there are no standards about the minimum exponent width
189 // (and the width of the %e conversion specifier refers to the
190 // mantissa, not to the exponent).
191 // Since newer MSVC versions use 3 digits as minimum exponent
192 // width while GNU libc uses 2 digits as minimum width, here we
193 // workaround this problem using for the exponent values with at
194 // least three digits.
196 // printf("%e",2.342E+02);
197 // -> under MSVC7.1 prints: 2.342000e+002
198 // -> under GNU libc 2.4 prints: 2.342000e+02
199 CMP3("2.342000e+112", "%e",2.342E+112);
200 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
201 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
202 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
204 CMP3("-0.02342", "%G",-2.342E-02);
205 CMP3("3.1415E-116", "%G",3.1415e-116);
206 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
207 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
208 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
209 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
212 void VsnprintfTestCase::F()
214 CMP3("3.300000", "%5f", 3.3);
215 CMP3("3.000000", "%5f", 3.0);
216 CMP3("0.000100", "%5f", .999999E-4);
217 CMP3("0.000990", "%5f", .99E-3);
218 CMP3("3333.000000", "%5f", 3333.0);
221 void VsnprintfTestCase::G()
223 // NOTE: the same about E() testcase applies here...
225 CMP3(" 3.3", "%5g", 3.3);
226 CMP3(" 3", "%5g", 3.0);
227 CMP3("9.99999e-115", "%5g", .999999E-114);
228 CMP3("0.00099", "%5g", .99E-3);
229 CMP3(" 3333", "%5g", 3333.0);
230 CMP3(" 0.01", "%5g", 0.01);
232 CMP3(" 3", "%5.g", 3.3);
233 CMP3(" 3", "%5.g", 3.0);
234 CMP3("1e-114", "%5.g", .999999E-114);
235 CMP3("0.0001", "%5.g", 1.0E-4);
236 CMP3("0.001", "%5.g", .99E-3);
237 CMP3("3e+103", "%5.g", 3333.0E100
);
238 CMP3(" 0.01", "%5.g", 0.01);
240 CMP3(" 3.3", "%5.2g", 3.3);
241 CMP3(" 3", "%5.2g", 3.0);
242 CMP3("1e-114", "%5.2g", .999999E-114);
243 CMP3("0.00099", "%5.2g", .99E-3);
244 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
245 CMP3(" 0.01", "%5.2g", 0.01);
248 void VsnprintfTestCase::S()
250 CMP3(" abc", "%5s", wxT("abc"));
251 CMP3(" a", "%5s", wxT("a"));
252 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
253 CMP3("abc ", "%-5s", wxT("abc"));
254 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
256 CMP3("abcde", "%.5s", wxT("abcdefghi"));
258 // do the same tests but with Unicode characters:
259 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
260 #define ALPHA "\x3B1"
262 #define GAMMA "\x3B3"
263 #define DELTA "\x3B4"
264 #define EPSILON "\x3B5"
267 #define THETA "\x3B8"
270 #define ABC ALPHA BETA GAMMA
271 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
272 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
274 CMP3(" " ABC
, "%5s", wxT(ABC
));
275 CMP3(" " ALPHA
, "%5s", wxT(ALPHA
));
276 CMP3(ABCDEFGHI
, "%5s", wxT(ABCDEFGHI
));
277 CMP3(ABC
" ", "%-5s", wxT(ABC
));
278 CMP3(ABCDEFGHI
, "%-5s", wxT(ABCDEFGHI
));
280 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
284 void VsnprintfTestCase::Asterisk()
286 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
287 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
288 CMP5("0.1", "%*.*f", 3, 1, 0.123);
290 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
293 void VsnprintfTestCase::Percent()
295 // some tests without any argument passed through ...
297 CMP2("%%%", "%%%%%%");
299 CMP3("% abc", "%%%5s", wxT("abc"));
300 CMP3("% abc%", "%%%5s%%", wxT("abc"));
302 // do not test odd number of '%' symbols as different implementations
303 // of snprintf() give different outputs as this situation is not considered
304 // by any standard (in fact, GCC will also warn you about a spurious % if
305 // you write %%% as argument of some *printf function !)
306 // Compare(wxT("%"), wxT("%%%"));
310 void VsnprintfTestCase::LongLong()
312 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
313 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
315 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
318 CMP3("123456789", "%I64d", (wxLongLong_t
)123456789);
319 CMP3("123456789abcdef", "%I64x", (wxLongLong_t
)0x123456789abcdef);
324 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
326 // NB: remember that wx*printf could be mapped either to system
327 // implementation or to wx implementation.
328 // In the first case, when the output buffer is too small, the returned
329 // value can be the number of characters required for the output buffer
330 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
331 // just a negative number, usually -1; (this is how e.g. MSVC's
332 // *printf() behaves). Luckily, in all implementations, when the
333 // output buffer is too small, it's nonetheless filled up to its max
336 // Note that in the second case (i.e. when we're using our own implementation),
337 // wxVsnprintf() will always return the number of characters which
339 // test without positionals
340 CMPTOSIZE(buffer
, size
, "123 444444444 - test - 555 -0.666",
341 "%i %li - test - %d %.3f",
342 123, (long int)444444444, 555, -0.666);
344 #if wxUSE_PRINTF_POS_PARAMS
345 // test with positional
346 CMPTOSIZE(buffer
, size
, "-0.666 123 - test - 444444444 555",
347 "%4$.3f %1$i - test - %2$li %3$d",
348 123, (long int)444444444, 555, -0.666);
351 // test unicode/ansi conversion specifiers
352 // NB: this line will output two warnings like these, on GCC:
353 // warning: use of 'h' length modifier with 's' type character (i.e.
354 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
355 // be ignored as here we explicitely want to test the wxSnprintf()
356 // behaviour in such case
358 CMPTOSIZE(buffer
, size
,
359 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
360 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
361 L
"unicode!!", L
'W', "ansi!!", 'w');
364 void VsnprintfTestCase::WrongFormatStrings()
366 // test how wxVsnprintf() behaves with wrong format string:
368 #if wxUSE_PRINTF_POS_PARAMS
370 // two positionals with the same index:
371 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$s %1$s"), "hello");
372 CPPUNIT_ASSERT(r
== -1);
374 // three positionals with the same index mixed with other pos args:
375 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
376 CPPUNIT_ASSERT(r
== -1);
378 // a missing positional arg:
379 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %3$d"), 1, 2, 3);
380 CPPUNIT_ASSERT(r
== -1);
382 // positional and non-positionals in the same format string:
383 r
= wxSnprintf(buf
, MAX_TEST_LEN
, wxT("%1$d %d %3$d"), 1, 2, 3);
384 CPPUNIT_ASSERT(r
== -1);
386 #endif // wxUSE_PRINTF_POS_PARAMS
389 void VsnprintfTestCase::BigToSmallBuffer()
391 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;
401 const wxString
& expectedString
,
403 const wxChar
*format
, ...)
405 const size_t BUFSIZE
= 16;
406 wxChar buf
[BUFSIZE
+ 1];
408 static int count
= 0;
410 wxASSERT(max
<= BUFSIZE
);
412 for (i
= 0; i
< BUFSIZE
; i
++)
417 va_start(ap
, format
);
419 int n
= wxVsnprintf(buf
, max
, format
, ap
);
423 // Prepare messages so that it is possible to see from the error which
425 wxString errStr
, overflowStr
;
426 errStr
<< _T("No.: ") << ++count
<< _T(", expected: ") << expectedLen
427 << _T(" '") << expectedString
<< _T("', result: ");
428 overflowStr
<< errStr
<< _T("buffer overflow");
429 errStr
<< n
<< _T(" '") << buf
<< _T("'");
431 // turn them into std::strings
432 std::string
errMsg(errStr
.mb_str());
433 std::string
overflowMsg(overflowStr
.mb_str());
435 CPPUNIT_ASSERT_MESSAGE(errMsg
,
436 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
438 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
440 for (i
= max
; i
< BUFSIZE
; i
++)
441 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
444 void VsnprintfTestCase::Miscellaneous()
446 // expectedLen, expectedString, max, format, ...
447 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
448 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
449 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
450 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
452 DoMisc(6, wxT("123456"), 8, wxT("123456"));
453 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
454 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
456 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
457 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
458 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
459 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
461 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
462 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
463 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
465 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
466 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);