]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
added test for %c and other minor changes (patch 1643843)
[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
82
83 // ----------------------------------------------------------------------------
84 // test class
85 // ----------------------------------------------------------------------------
86
87 class VsnprintfTestCase : public CppUnit::TestCase
88 {
89 public:
90 VsnprintfTestCase();
91
92 private:
93 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
94 CPPUNIT_TEST( C );
95 CPPUNIT_TEST( D );
96 CPPUNIT_TEST( X );
97 CPPUNIT_TEST( O );
98 CPPUNIT_TEST( P );
99 CPPUNIT_TEST( N );
100 CPPUNIT_TEST( E );
101 CPPUNIT_TEST( F );
102 CPPUNIT_TEST( G );
103 CPPUNIT_TEST( S );
104 CPPUNIT_TEST( Asterisk );
105 CPPUNIT_TEST( Percent );
106 #ifdef wxLongLong_t
107 CPPUNIT_TEST( LongLong );
108 #endif
109
110 CPPUNIT_TEST( BigToSmallBuffer );
111 CPPUNIT_TEST( WrongFormatStrings );
112 CPPUNIT_TEST( Miscellaneous );
113 CPPUNIT_TEST_SUITE_END();
114
115 void C();
116 void D();
117 void X();
118 void O();
119 void P();
120 void N();
121 void E();
122 void F();
123 void G();
124 void S();
125 void Asterisk();
126 void Percent();
127 #ifdef wxLongLong_t
128 void LongLong();
129 #endif
130 void Unicode();
131
132 void BigToSmallBuffer();
133 void WrongFormatStrings();
134 void Miscellaneous();
135 void Misc(wxChar *buffer, int size);
136
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, ...);
142
143 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
144 };
145
146 // register in the unnamed registry so that these tests are run by default
147 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
148
149 // also include in it's own registry so that these tests can be run alone
150 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
151
152 VsnprintfTestCase::VsnprintfTestCase()
153 {
154 }
155
156 void VsnprintfTestCase::C()
157 {
158 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
159
160 // NOTE:
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
163 // NUL-terminated).
164
165 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
166 }
167
168 void VsnprintfTestCase::D()
169 {
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);
176 }
177
178 void VsnprintfTestCase::X()
179 {
180 CMP3("ABCD", "%X", 0xABCD);
181 CMP3("0XABCD", "%#X", 0xABCD);
182 CMP3("0xabcd", "%#x", 0xABCD);
183 }
184
185 void VsnprintfTestCase::O()
186 {
187 CMP3("1234567", "%o", 01234567);
188 CMP3("01234567", "%#o", 01234567);
189 }
190
191 void VsnprintfTestCase::P()
192 {
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
197 // on 64 bit systems
198 #ifdef __VISUALC__
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);
205 #endif
206 #elif defined(__GNUG__)
207 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
208 CMP3("(nil)", "%p", (void*)NULL);
209 #endif
210 }
211
212 void VsnprintfTestCase::N()
213 {
214 int nchar;
215
216 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
217 CPPUNIT_ASSERT_EQUAL( 7, nchar );
218 }
219
220 void VsnprintfTestCase::E()
221 {
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.
229 // Some examples:
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);
237
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);
244 }
245
246 void VsnprintfTestCase::F()
247 {
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);
253 }
254
255 void VsnprintfTestCase::G()
256 {
257 // NOTE: the same about E() testcase applies here...
258
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);
265
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);
273
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);
280 }
281
282 void VsnprintfTestCase::S()
283 {
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"));
289
290 CMP3("abcde", "%.5s", wxT("abcdefghi"));
291
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"
295 #define BETA "\x3B2"
296 #define GAMMA "\x3B3"
297 #define DELTA "\x3B4"
298 #define EPSILON "\x3B5"
299 #define ZETA "\x3B6"
300 #define ETA "\x3B7"
301 #define THETA "\x3B8"
302 #define IOTA "\x3B9"
303
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
307
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));
313
314 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
315 #endif
316
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"));
320 }
321
322 void VsnprintfTestCase::Asterisk()
323 {
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);
327
328 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
329 }
330
331 void VsnprintfTestCase::Percent()
332 {
333 // some tests without any argument passed through ...
334 CMP2("%", "%%");
335 CMP2("%%%", "%%%%%%");
336
337 CMP3("% abc", "%%%5s", wxT("abc"));
338 CMP3("% abc%", "%%%5s%%", wxT("abc"));
339
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("%%%"));
345 }
346
347 #ifdef wxLongLong_t
348 void VsnprintfTestCase::LongLong()
349 {
350 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
351 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
352
353 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
354
355 #ifdef __WXMSW__
356 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
357 CMP3("123456789abcdef", "%I64x", (wxLongLong_t)0x123456789abcdef);
358 #endif
359 }
360 #endif
361
362 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
363 {
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.
372 //
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
375 // output or
376 // -1 if there was an error in the format string
377 // maxSize+1 if the output buffer is too small
378
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);
383
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);
389 #endif
390
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
397
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');
402 }
403
404 void VsnprintfTestCase::WrongFormatStrings()
405 {
406 // test how wxVsnprintf() behaves with wrong format string:
407
408 #if wxUSE_PRINTF_POS_PARAMS
409
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);
413
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);
417
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);
421
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);
425
426 #endif // wxUSE_PRINTF_POS_PARAMS
427 }
428
429 void VsnprintfTestCase::BigToSmallBuffer()
430 {
431 wxChar buf[1024], buf2[16], buf3[4], buf4;
432
433 Misc(buf, 1024);
434 Misc(buf2, 16);
435 Misc(buf3, 4);
436 Misc(&buf4, 1);
437 }
438
439 void VsnprintfTestCase::DoMisc(
440 int expectedLen,
441 const wxString& expectedString,
442 size_t max,
443 const wxChar *format, ...)
444 {
445 const size_t BUFSIZE = MAX_TEST_LEN - 1;
446 size_t i;
447 static int count = 0;
448
449 wxASSERT(max <= BUFSIZE);
450
451 for (i = 0; i < BUFSIZE; i++)
452 buf[i] = '*';
453 buf[BUFSIZE] = 0;
454
455 va_list ap;
456 va_start(ap, format);
457
458 int n = wxVsnprintf(buf, max, format, ap);
459
460 va_end(ap);
461
462 // Prepare messages so that it is possible to see from the error which
463 // test was running.
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("'");
469
470 // turn them into std::strings
471 std::string errMsg(errStr.mb_str());
472 std::string overflowMsg(overflowStr.mb_str());
473
474 CPPUNIT_ASSERT_MESSAGE(errMsg,
475 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
476
477 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
478
479 for (i = max; i < BUFSIZE; i++)
480 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
481 }
482
483 void VsnprintfTestCase::Miscellaneous()
484 {
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);
490
491 DoMisc(6, wxT("123456"), 8, wxT("123456"));
492 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
493 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
494
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);
499
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"));
503
504 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
505 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
506 }