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