]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
ca6d276d2bf4c67346be70a9d6c37a831699f558
[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 #if wxUSE_WXVSNPRINTF
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #include "wx/wxchar.h"
27 #endif // WX_PRECOMP
28
29
30 // NOTE: for more info about the specification of wxVsnprintf() behaviour you can
31 // refer to the following page of the GNU libc manual:
32 // http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
33
34
35
36 // ----------------------------------------------------------------------------
37 // global utilities for testing
38 // ----------------------------------------------------------------------------
39
40 #define MAX_TEST_LEN 1024
41
42 // temporary buffers
43 static wxChar buf[MAX_TEST_LEN];
44 int r;
45
46 // these macros makes it possible to write all tests without repeating a lot
47 // of times the wxT() macro
48 // NOTE: you should use expected strings with these macros which do not exceed
49 // MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
50
51 #define ASSERT_STR_EQUAL( a, b ) \
52 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
53
54 #define CMP5(expected, x, y, z, w) \
55 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
56 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
57 ASSERT_STR_EQUAL( wxT(expected), buf );
58
59 #define CMP4(expected, x, y, z) \
60 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
61 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
62 ASSERT_STR_EQUAL( wxT(expected), buf );
63
64 #define CMP3(expected, x, y) \
65 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
66 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
67 ASSERT_STR_EQUAL( wxT(expected), buf );
68
69 #define CMP2(expected, x) \
70 r=wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
71 CPPUNIT_ASSERT( r == (int)wxStrlen(buf) ); \
72 ASSERT_STR_EQUAL( wxT(expected), buf );
73
74 // NOTE: this macro is used also with too-small buffers (see Miscellaneous())
75 // test function, thus the return value can be > size and thus we
76 // cannot check if r == (int)wxStrlen(buf)
77 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
78 r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
79 CPPUNIT_ASSERT( r > 0 ); \
80 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
81 wxString(buffer) )
82
83 // this is the same as wxSnprintf() but it passes the format string to
84 // wxVsnprintf() without using ATTRIBUTE_PRINTF and thus suppresses the gcc
85 // checks (and resulting warnings) for the format string
86 //
87 // use with extreme care and only when you're really sure the warnings must be
88 // suppressed!
89 template<typename T>
90 static int
91 wxUnsafeSnprintf(T *buf, size_t len, const wxChar *fmt, ...)
92 {
93 va_list args;
94 va_start(args, fmt);
95
96 int rc = wxVsnprintf(buf, len, fmt, args);
97
98 va_end(args);
99
100 return rc;
101 }
102
103 // ----------------------------------------------------------------------------
104 // test class
105 // ----------------------------------------------------------------------------
106
107 class VsnprintfTestCase : public CppUnit::TestCase
108 {
109 public:
110 VsnprintfTestCase();
111
112 private:
113 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
114 CPPUNIT_TEST( C );
115 CPPUNIT_TEST( D );
116 CPPUNIT_TEST( X );
117 CPPUNIT_TEST( O );
118 CPPUNIT_TEST( P );
119 CPPUNIT_TEST( N );
120 CPPUNIT_TEST( E );
121 CPPUNIT_TEST( F );
122 CPPUNIT_TEST( G );
123 CPPUNIT_TEST( S );
124 CPPUNIT_TEST( Asterisk );
125 CPPUNIT_TEST( Percent );
126 #ifdef wxLongLong_t
127 CPPUNIT_TEST( LongLong );
128 #endif
129
130 CPPUNIT_TEST( BigToSmallBuffer );
131 #if wxUSE_WXVSNPRINTF
132 CPPUNIT_TEST( WrongFormatStrings );
133 #endif // wxUSE_WXVSNPRINTF
134 CPPUNIT_TEST( Miscellaneous );
135 CPPUNIT_TEST_SUITE_END();
136
137 void C();
138 void D();
139 void X();
140 void O();
141 void P();
142 void N();
143 void E();
144 void F();
145 void G();
146 void S();
147 void Asterisk();
148 void Percent();
149 #ifdef wxLongLong_t
150 void LongLong();
151 #endif
152 void Unicode();
153
154 void BigToSmallBuffer();
155 #if wxUSE_WXVSNPRINTF
156 void WrongFormatStrings();
157 #endif // wxUSE_WXVSNPRINTF
158 void Miscellaneous();
159 template<typename T> void Misc(T *buffer, int size);
160
161 // compares the expectedString and the result of wxVsnprintf() char by char
162 // for all its lenght (not only for first expectedLen chars) and also
163 // checks the return value
164 void DoMisc(int expectedLen, const wxString& expectedString,
165 size_t max, const wxChar *format, ...);
166
167 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
168 };
169
170 // register in the unnamed registry so that these tests are run by default
171 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
172
173 // also include in it's own registry so that these tests can be run alone
174 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
175
176 VsnprintfTestCase::VsnprintfTestCase()
177 {
178 }
179
180 void VsnprintfTestCase::C()
181 {
182 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
183
184 // NOTE:
185 // the NULL characters _can_ be passed to %c to e.g. create strings
186 // with embedded NULs (because strings are not always supposed to be
187 // NUL-terminated).
188
189 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
190 }
191
192 void VsnprintfTestCase::D()
193 {
194 CMP3("+123456", "%+d", 123456);
195 CMP3("-123456", "%d", -123456);
196 CMP3(" 123456", "% d", 123456);
197 CMP3(" 123456", "%10d", 123456);
198 CMP3("0000123456", "%010d", 123456);
199 CMP3("-123456 ", "%-10d", -123456);
200 }
201
202 void VsnprintfTestCase::X()
203 {
204 CMP3("ABCD", "%X", 0xABCD);
205 CMP3("0XABCD", "%#X", 0xABCD);
206 CMP3("0xabcd", "%#x", 0xABCD);
207 }
208
209 void VsnprintfTestCase::O()
210 {
211 CMP3("1234567", "%o", 01234567);
212 CMP3("01234567", "%#o", 01234567);
213 }
214
215 void VsnprintfTestCase::P()
216 {
217 // WARNING: printing of pointers is not fully standard.
218 // GNU prints them as %#x except for NULL pointers which are
219 // printed as '(nil)'.
220 // MSVC always print them as %8X on 32 bit systems and as %16X
221 // on 64 bit systems
222 #ifdef __VISUALC__
223 #if SIZEOF_VOID_P == 4
224 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
225 CMP3("00000000", "%p", (void*)NULL);
226 #elif SIZEOF_VOID_P == 8
227 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
228 CMP3("0000000000000000", "%p", (void*)NULL);
229 #endif
230 #elif defined(__GNUG__)
231 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
232 CMP3("(nil)", "%p", (void*)NULL);
233 #endif
234 }
235
236 void VsnprintfTestCase::N()
237 {
238 int nchar;
239
240 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
241 CPPUNIT_ASSERT_EQUAL( 7, nchar );
242 }
243
244 void VsnprintfTestCase::E()
245 {
246 // NB: there are no standards about the minimum exponent width
247 // (and the width of the %e conversion specifier refers to the
248 // mantissa, not to the exponent).
249 // Since newer MSVC versions use 3 digits as minimum exponent
250 // width while GNU libc uses 2 digits as minimum width, here we
251 // workaround this problem using for the exponent values with at
252 // least three digits.
253 // Some examples:
254 // printf("%e",2.342E+02);
255 // -> under MSVC7.1 prints: 2.342000e+002
256 // -> under GNU libc 2.4 prints: 2.342000e+02
257 CMP3("2.342000e+112", "%e",2.342E+112);
258 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
259 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
260 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
261
262 CMP3("-0.02342", "%G",-2.342E-02);
263 CMP3("3.1415E-116", "%G",3.1415e-116);
264 CMP3("0003.141500e+103", "%016e", 3141.5e100);
265 CMP3(" 3.141500e+103", "%16e", 3141.5e100);
266 CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
267 CMP3("3.142e+103", "%010.3e", 3141.5e100);
268 }
269
270 void VsnprintfTestCase::F()
271 {
272 CMP3("3.300000", "%5f", 3.3);
273 CMP3("3.000000", "%5f", 3.0);
274 CMP3("0.000100", "%5f", .999999E-4);
275 CMP3("0.000990", "%5f", .99E-3);
276 CMP3("3333.000000", "%5f", 3333.0);
277 }
278
279 void VsnprintfTestCase::G()
280 {
281 // NOTE: the same about E() testcase applies here...
282
283 CMP3(" 3.3", "%5g", 3.3);
284 CMP3(" 3", "%5g", 3.0);
285 CMP3("9.99999e-115", "%5g", .999999E-114);
286 CMP3("0.00099", "%5g", .99E-3);
287 CMP3(" 3333", "%5g", 3333.0);
288 CMP3(" 0.01", "%5g", 0.01);
289
290 CMP3(" 3", "%5.g", 3.3);
291 CMP3(" 3", "%5.g", 3.0);
292 CMP3("1e-114", "%5.g", .999999E-114);
293 CMP3("0.0001", "%5.g", 1.0E-4);
294 CMP3("0.001", "%5.g", .99E-3);
295 CMP3("3e+103", "%5.g", 3333.0E100);
296 CMP3(" 0.01", "%5.g", 0.01);
297
298 CMP3(" 3.3", "%5.2g", 3.3);
299 CMP3(" 3", "%5.2g", 3.0);
300 CMP3("1e-114", "%5.2g", .999999E-114);
301 CMP3("0.00099", "%5.2g", .99E-3);
302 CMP3("3.3e+103", "%5.2g", 3333.0E100);
303 CMP3(" 0.01", "%5.2g", 0.01);
304 }
305
306 void VsnprintfTestCase::S()
307 {
308 CMP3(" abc", "%5s", wxT("abc"));
309 CMP3(" a", "%5s", wxT("a"));
310 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
311 CMP3("abc ", "%-5s", wxT("abc"));
312 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
313
314 CMP3("abcde", "%.5s", wxT("abcdefghi"));
315
316 // do the same tests but with Unicode characters:
317 #if wxUSE_UNICODE && !defined(__VISUALC__) // FIXME: this doesn't compile with VC7
318 #define ALPHA "\x3B1"
319 #define BETA "\x3B2"
320 #define GAMMA "\x3B3"
321 #define DELTA "\x3B4"
322 #define EPSILON "\x3B5"
323 #define ZETA "\x3B6"
324 #define ETA "\x3B7"
325 #define THETA "\x3B8"
326 #define IOTA "\x3B9"
327
328 #define ABC ALPHA BETA GAMMA
329 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
330 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
331
332 CMP3(" " ABC, "%5s", wxT(ABC));
333 CMP3(" " ALPHA, "%5s", wxT(ALPHA));
334 CMP3(ABCDEFGHI, "%5s", wxT(ABCDEFGHI));
335 CMP3(ABC " ", "%-5s", wxT(ABC));
336 CMP3(ABCDEFGHI, "%-5s", wxT(ABCDEFGHI));
337
338 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
339 #endif
340
341 // test a string which have the NULL character after "ab";
342 // obviously it should be handled exactly like just as "ab"
343 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
344 }
345
346 void VsnprintfTestCase::Asterisk()
347 {
348 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
349 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
350 CMP5("0.1", "%*.*f", 3, 1, 0.123);
351
352 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
353 }
354
355 void VsnprintfTestCase::Percent()
356 {
357 // some tests without any argument passed through ...
358 CMP2("%", "%%");
359 CMP2("%%%", "%%%%%%");
360
361 CMP3("% abc", "%%%5s", wxT("abc"));
362 CMP3("% abc%", "%%%5s%%", wxT("abc"));
363
364 // do not test odd number of '%' symbols as different implementations
365 // of snprintf() give different outputs as this situation is not considered
366 // by any standard (in fact, GCC will also warn you about a spurious % if
367 // you write %%% as argument of some *printf function !)
368 // Compare(wxT("%"), wxT("%%%"));
369 }
370
371 #ifdef wxLongLong_t
372 void VsnprintfTestCase::LongLong()
373 {
374 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
375 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
376
377 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
378
379 #ifdef __WXMSW__
380 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
381 CMP3("123456789abcdef", "%I64x", wxLL(0x123456789abcdef));
382 #endif
383 }
384 #endif
385
386 template<typename T>
387 void VsnprintfTestCase::Misc(T *buffer, int size)
388 {
389 // Remember that wx*printf could be mapped either to system
390 // implementation or to wx implementation.
391 // In the first case, when the output buffer is too small, the returned
392 // value can be the number of characters required for the output buffer
393 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
394 // just a negative number, usually -1; (this is how e.g. MSVC's
395 // *printf() behaves). Luckily, in all implementations, when the
396 // output buffer is too small, it's nonetheless filled up to its max size.
397 //
398 // Note that in the second case (i.e. when we're using our own implementation),
399 // wxVsnprintf() will return the number of characters written in the standard
400 // output or
401 // -1 if there was an error in the format string
402 // maxSize+1 if the output buffer is too small
403
404 // test without positionals
405 CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
406 "%i %li - test - %d %.3f",
407 123, (long int)444444444, 555, -0.666);
408
409 #if wxUSE_PRINTF_POS_PARAMS
410 // test with positional
411 CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
412 "%4$.3f %1$i - test - %2$li %3$d",
413 123, (long int)444444444, 555, -0.666);
414 #endif
415
416 // test unicode/ansi conversion specifiers
417 //
418 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
419 // format and gcc would warn about this otherwise
420
421 r = wxUnsafeSnprintf(buffer, size,
422 _T("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
423 L"unicode!!", L'W', "ansi!!", 'w');
424 CPPUNIT_ASSERT( r != -1 );
425 CPPUNIT_ASSERT_EQUAL(
426 wxString(wxT("unicode string: unicode!! W - ansi string: ansi!! w\n\n")).Left(size - 1),
427 wxString(buffer)
428 );
429 }
430
431
432 // this test is only for our own implementation, the system implementation
433 // doesn't always give errors for invalid format strings (e.g. glibc doesn't)
434 // and as it's not required too (the behaviour is "undefined" according to the
435 // spec), there is really no sense in testing for it (and the first 2 formats
436 // in this test are not invalid at all in fact)
437 #if wxUSE_WXVSNPRINTF
438
439 void VsnprintfTestCase::WrongFormatStrings()
440 {
441 // test how wxVsnprintf() behaves with wrong format string:
442
443 // NB: the next 2 tests currently return an error but they shouldn't,
444 // according to POSIX reusing the parameters is allowed
445
446 // two positionals with the same index:
447 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
448 CPPUNIT_ASSERT(r != -1);
449
450 // three positionals with the same index mixed with other pos args:
451 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
452 CPPUNIT_ASSERT(r != -1);
453
454 // a missing positional arg: this should result in an error but not all
455 // implementations detect it (e.g. glibc doesn't)
456 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3);
457 CPPUNIT_ASSERT_EQUAL(-1, r);
458
459 // positional and non-positionals in the same format string:
460 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
461 CPPUNIT_ASSERT_EQUAL(-1, r);
462 }
463
464 #endif // wxUSE_WXVSNPRINTF
465
466 void VsnprintfTestCase::BigToSmallBuffer()
467 {
468 // VC6 can't compile this code
469 #if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
470 #if wxUSE_UNICODE
471 wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
472 Misc(bufw, 1024);
473 Misc(bufw2, 16);
474 Misc(bufw3, 4);
475 Misc(&bufw4, 1);
476 #endif // wxUSE_UNICODE
477
478 char bufa[1024], bufa2[16], bufa3[4], bufa4;
479 Misc(bufa, 1024);
480 Misc(bufa2, 16);
481 Misc(bufa3, 4);
482 Misc(&bufa4, 1);
483 #endif // !VC6
484 }
485
486 void VsnprintfTestCase::DoMisc(
487 int expectedLen,
488 const wxString& expectedString,
489 size_t max,
490 const wxChar *format, ...)
491 {
492 const size_t BUFSIZE = MAX_TEST_LEN - 1;
493 size_t i;
494 static int count = 0;
495
496 wxASSERT(max <= BUFSIZE);
497
498 for (i = 0; i < BUFSIZE; i++)
499 buf[i] = '*';
500 buf[BUFSIZE] = 0;
501
502 va_list ap;
503 va_start(ap, format);
504
505 int n = wxVsnprintf(buf, max, format, ap);
506
507 va_end(ap);
508
509 // Prepare messages so that it is possible to see from the error which
510 // test was running.
511 wxString errStr, overflowStr;
512 errStr << _T("No.: ") << ++count << _T(", expected: ") << expectedLen
513 << _T(" '") << expectedString << _T("', result: ");
514 overflowStr << errStr << _T("buffer overflow");
515 errStr << n << _T(" '") << buf << _T("'");
516
517 // turn them into std::strings
518 std::string errMsg(errStr.mb_str());
519 std::string overflowMsg(overflowStr.mb_str());
520
521 CPPUNIT_ASSERT_MESSAGE(errMsg,
522 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
523
524 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
525
526 for (i = max; i < BUFSIZE; i++)
527 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
528 }
529
530 void VsnprintfTestCase::Miscellaneous()
531 {
532 // expectedLen, expectedString, max, format, ...
533 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
534 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
535 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
536 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
537
538 DoMisc(6, wxT("123456"), 8, wxT("123456"));
539 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
540 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
541
542 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
543 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
544 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
545 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
546
547 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
548 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
549 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
550
551 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
552 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
553 }
554
555 #endif // wxUSE_WXVSNPRINTF