]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
cleanup
[wxWidgets.git] / 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)
7 // Created: 2006-04-01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 #include "testprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #include "wx/wxchar.h"
25 #endif // WX_PRECOMP
26
27
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
31
32
33
34 // ----------------------------------------------------------------------------
35 // global utilities for testing
36 // ----------------------------------------------------------------------------
37
38 #define MAX_TEST_LEN 1024
39
40 // temporary buffers
41 static wxChar buf[MAX_TEST_LEN];
42 int r;
43
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)
48
49 #define ASSERT_STR_EQUAL( a, b ) \
50 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
51
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 );
56
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 );
61
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 );
66
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 );
71
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), \
79 wxString(buffer) )
80
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
84 //
85 // use with extreme care and only when you're really sure the warnings must be
86 // suppressed!
87 template<typename T>
88 static int
89 wxUnsafeSnprintf(T *buf, size_t len, const wxChar *fmt, ...)
90 {
91 va_list args;
92 va_start(args, fmt);
93
94 int rc = wxVsnprintf(buf, len, fmt, args);
95
96 va_end(args);
97
98 return rc;
99 }
100
101 // ----------------------------------------------------------------------------
102 // test class
103 // ----------------------------------------------------------------------------
104
105 class VsnprintfTestCase : public CppUnit::TestCase
106 {
107 public:
108 VsnprintfTestCase();
109
110 private:
111 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
112 CPPUNIT_TEST( C );
113 CPPUNIT_TEST( D );
114 CPPUNIT_TEST( X );
115 CPPUNIT_TEST( O );
116 CPPUNIT_TEST( P );
117 CPPUNIT_TEST( N );
118 CPPUNIT_TEST( E );
119 CPPUNIT_TEST( F );
120 CPPUNIT_TEST( G );
121 CPPUNIT_TEST( S );
122 CPPUNIT_TEST( Asterisk );
123 CPPUNIT_TEST( Percent );
124 #ifdef wxLongLong_t
125 CPPUNIT_TEST( LongLong );
126 #endif
127
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();
134
135 void C();
136 void D();
137 void X();
138 void O();
139 void P();
140 void N();
141 void E();
142 void F();
143 void G();
144 void S();
145 void Asterisk();
146 void Percent();
147 #ifdef wxLongLong_t
148 void LongLong();
149 #endif
150 void Unicode();
151
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);
158
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, ...);
164
165 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
166 };
167
168 // register in the unnamed registry so that these tests are run by default
169 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
170
171 // also include in it's own registry so that these tests can be run alone
172 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
173
174 VsnprintfTestCase::VsnprintfTestCase()
175 {
176 }
177
178 void VsnprintfTestCase::C()
179 {
180 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
181
182 // NOTE:
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
185 // NUL-terminated).
186
187 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
188 }
189
190 void VsnprintfTestCase::D()
191 {
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);
198 }
199
200 void VsnprintfTestCase::X()
201 {
202 CMP3("ABCD", "%X", 0xABCD);
203 CMP3("0XABCD", "%#X", 0xABCD);
204 CMP3("0xabcd", "%#x", 0xABCD);
205 }
206
207 void VsnprintfTestCase::O()
208 {
209 CMP3("1234567", "%o", 01234567);
210 CMP3("01234567", "%#o", 01234567);
211 }
212
213 void VsnprintfTestCase::P()
214 {
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
219 // on 64 bit systems
220 #ifdef __VISUALC__
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);
227 #endif
228 #elif defined(__GNUG__)
229 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
230 CMP3("(nil)", "%p", (void*)NULL);
231 #endif
232 }
233
234 void VsnprintfTestCase::N()
235 {
236 int nchar;
237
238 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
239 CPPUNIT_ASSERT_EQUAL( 7, nchar );
240 }
241
242 void VsnprintfTestCase::E()
243 {
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.
251 // Some examples:
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);
259
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);
266 }
267
268 void VsnprintfTestCase::F()
269 {
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);
275 }
276
277 void VsnprintfTestCase::G()
278 {
279 // NOTE: the same about E() testcase applies here...
280
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);
287
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);
295
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);
302 }
303
304 void VsnprintfTestCase::S()
305 {
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"));
311
312 CMP3("abcde", "%.5s", wxT("abcdefghi"));
313
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"
317 #define BETA "\x3B2"
318 #define GAMMA "\x3B3"
319 #define DELTA "\x3B4"
320 #define EPSILON "\x3B5"
321 #define ZETA "\x3B6"
322 #define ETA "\x3B7"
323 #define THETA "\x3B8"
324 #define IOTA "\x3B9"
325
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
329
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));
335
336 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
337 #endif
338
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"));
342 }
343
344 void VsnprintfTestCase::Asterisk()
345 {
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);
349
350 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
351 }
352
353 void VsnprintfTestCase::Percent()
354 {
355 // some tests without any argument passed through ...
356 CMP2("%", "%%");
357 CMP2("%%%", "%%%%%%");
358
359 CMP3("% abc", "%%%5s", wxT("abc"));
360 CMP3("% abc%", "%%%5s%%", wxT("abc"));
361
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("%%%"));
367 }
368
369 #ifdef wxLongLong_t
370 void VsnprintfTestCase::LongLong()
371 {
372 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
373 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
374
375 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
376
377 #ifdef __WXMSW__
378 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
379 CMP3("123456789abcdef", "%I64x", (wxLongLong_t)0x123456789abcdef);
380 #endif
381 }
382 #endif
383
384 template<typename T>
385 void VsnprintfTestCase::Misc(T *buffer, int size)
386 {
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.
395 //
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
398 // output or
399 // -1 if there was an error in the format string
400 // maxSize+1 if the output buffer is too small
401
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);
406
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);
412 #endif
413
414 // test unicode/ansi conversion specifiers
415 //
416 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
417 // format and gcc would warn about this otherwise
418
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),
425 wxString(buffer)
426 );
427 }
428
429
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
436
437 void VsnprintfTestCase::WrongFormatStrings()
438 {
439 // test how wxVsnprintf() behaves with wrong format string:
440
441 // NB: the next 2 tests currently return an error but they shouldn't,
442 // according to POSIX reusing the parameters is allowed
443
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);
447
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);
451
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);
456
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);
460 }
461
462 #endif // wxUSE_WXVSNPRINTF
463
464 void VsnprintfTestCase::BigToSmallBuffer()
465 {
466 char bufa[1024], bufa2[16], bufa3[4], bufa4;
467
468 #if wxUSE_UNICODE
469 wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
470 Misc(bufw, 1024);
471 Misc(bufw2, 16);
472 Misc(bufw3, 4);
473 Misc(&bufw4, 1);
474 #endif // wxUSE_UNICODE
475
476 Misc(bufa, 1024);
477 Misc(bufa2, 16);
478 Misc(bufa3, 4);
479 Misc(&bufa4, 1);
480 }
481
482 void VsnprintfTestCase::DoMisc(
483 int expectedLen,
484 const wxString& expectedString,
485 size_t max,
486 const wxChar *format, ...)
487 {
488 const size_t BUFSIZE = MAX_TEST_LEN - 1;
489 size_t i;
490 static int count = 0;
491
492 wxASSERT(max <= BUFSIZE);
493
494 for (i = 0; i < BUFSIZE; i++)
495 buf[i] = '*';
496 buf[BUFSIZE] = 0;
497
498 va_list ap;
499 va_start(ap, format);
500
501 int n = wxVsnprintf(buf, max, format, ap);
502
503 va_end(ap);
504
505 // Prepare messages so that it is possible to see from the error which
506 // test was running.
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("'");
512
513 // turn them into std::strings
514 std::string errMsg(errStr.mb_str());
515 std::string overflowMsg(overflowStr.mb_str());
516
517 CPPUNIT_ASSERT_MESSAGE(errMsg,
518 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
519
520 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
521
522 for (i = max; i < BUFSIZE; i++)
523 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
524 }
525
526 void VsnprintfTestCase::Miscellaneous()
527 {
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);
533
534 DoMisc(6, wxT("123456"), 8, wxT("123456"));
535 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
536 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
537
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);
542
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"));
546
547 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
548 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
549 }