added support and enable long long support when wxLongLong_t is available even if...
[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
29 #define MAX_TEST_LEN 1024
30
31
32 // temporary buffers
33 static wxChar buf[MAX_TEST_LEN];
34 int r;
35
36 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
37
38 #define ASSERT_STR_EQUAL( a, b ) \
39 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
40
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 );
45
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 );
50
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 );
55
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 );
60
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), \
65 wxString(buffer) )
66
67
68
69 // ----------------------------------------------------------------------------
70 // test class
71 // ----------------------------------------------------------------------------
72
73 class VsnprintfTestCase : public CppUnit::TestCase
74 {
75 public:
76 VsnprintfTestCase();
77
78 private:
79 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
80 CPPUNIT_TEST( D );
81 CPPUNIT_TEST( X );
82 CPPUNIT_TEST( O );
83 CPPUNIT_TEST( P );
84 CPPUNIT_TEST( N );
85 CPPUNIT_TEST( E );
86 CPPUNIT_TEST( F );
87 CPPUNIT_TEST( G );
88 CPPUNIT_TEST( S );
89 CPPUNIT_TEST( Asterisk );
90 CPPUNIT_TEST( Percent );
91 #ifdef wxLongLong_t
92 CPPUNIT_TEST( LongLong );
93 #endif
94
95 CPPUNIT_TEST( BigToSmallBuffer );
96 CPPUNIT_TEST( WrongFormatStrings );
97 CPPUNIT_TEST( Miscellaneous );
98 CPPUNIT_TEST_SUITE_END();
99
100 void D();
101 void X();
102 void O();
103 void P();
104 void N();
105 void E();
106 void F();
107 void G();
108 void S();
109 void Asterisk();
110 void Percent();
111 #ifdef wxLongLong_t
112 void LongLong();
113 #endif
114 void Unicode();
115
116 void BigToSmallBuffer();
117 void WrongFormatStrings();
118 void Miscellaneous();
119 void Misc(wxChar *buffer, int size);
120
121 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
122 };
123
124 // register in the unnamed registry so that these tests are run by default
125 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
126
127 // also include in it's own registry so that these tests can be run alone
128 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
129
130 VsnprintfTestCase::VsnprintfTestCase()
131 {
132 }
133
134 void VsnprintfTestCase::D()
135 {
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);
142 }
143
144 void VsnprintfTestCase::X()
145 {
146 CMP3("ABCD", "%X", 0xABCD);
147 CMP3("0XABCD", "%#X", 0xABCD);
148 CMP3("0xabcd", "%#x", 0xABCD);
149 }
150
151 void VsnprintfTestCase::O()
152 {
153 CMP3("1234567", "%o", 01234567);
154 CMP3("01234567", "%#o", 01234567);
155 }
156
157 void VsnprintfTestCase::P()
158 {
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
163 // on 64 bit systems
164 #ifdef __VISUALC__
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);
171 #endif
172 #elif defined(__GNUG__)
173 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
174 CMP3("(nil)", "%p", (void*)NULL);
175 #endif
176 }
177
178 void VsnprintfTestCase::N()
179 {
180 int nchar;
181
182 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
183 CPPUNIT_ASSERT_EQUAL( 7, nchar );
184 }
185
186 void VsnprintfTestCase::E()
187 {
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.
195 // Some examples:
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);
203
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);
210 }
211
212 void VsnprintfTestCase::F()
213 {
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);
219 }
220
221 void VsnprintfTestCase::G()
222 {
223 // NOTE: the same about E() testcase applies here...
224
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);
231
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);
239
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);
246 }
247
248 void VsnprintfTestCase::S()
249 {
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"));
255
256 CMP3("abcde", "%.5s", wxT("abcdefghi"));
257
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"
261 #define BETA "\x3B2"
262 #define GAMMA "\x3B3"
263 #define DELTA "\x3B4"
264 #define EPSILON "\x3B5"
265 #define ZETA "\x3B6"
266 #define ETA "\x3B7"
267 #define THETA "\x3B8"
268 #define IOTA "\x3B9"
269
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
273
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));
279
280 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
281 #endif
282 }
283
284 void VsnprintfTestCase::Asterisk()
285 {
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);
289
290 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
291 }
292
293 void VsnprintfTestCase::Percent()
294 {
295 // some tests without any argument passed through ...
296 CMP2("%", "%%");
297 CMP2("%%%", "%%%%%%");
298
299 CMP3("% abc", "%%%5s", wxT("abc"));
300 CMP3("% abc%", "%%%5s%%", wxT("abc"));
301
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("%%%"));
307 }
308
309 #ifdef wxLongLong_t
310 void VsnprintfTestCase::LongLong()
311 {
312 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
313 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
314
315 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
316
317 #ifdef __WXMSW__
318 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
319 CMP3("123456789abcdef", "%I64x", (wxLongLong_t)0x123456789abcdef);
320 #endif
321 }
322 #endif
323
324 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
325 {
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
334 // size.
335 //
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
338
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);
343
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);
349 #endif
350
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
357
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');
362 }
363
364 void VsnprintfTestCase::WrongFormatStrings()
365 {
366 // test how wxVsnprintf() behaves with wrong format string:
367
368 #if wxUSE_PRINTF_POS_PARAMS
369
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);
373
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);
377
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);
381
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);
385
386 #endif // wxUSE_PRINTF_POS_PARAMS
387 }
388
389 void VsnprintfTestCase::BigToSmallBuffer()
390 {
391 wxChar buf[1024], buf2[16], buf3[4], buf4;
392
393 Misc(buf, 1024);
394 Misc(buf2, 16);
395 Misc(buf3, 4);
396 Misc(&buf4, 1);
397 }
398
399 static void DoMisc(
400 int expectedLen,
401 const wxString& expectedString,
402 size_t max,
403 const wxChar *format, ...)
404 {
405 const size_t BUFSIZE = 16;
406 wxChar buf[BUFSIZE + 1];
407 size_t i;
408 static int count = 0;
409
410 wxASSERT(max <= BUFSIZE);
411
412 for (i = 0; i < BUFSIZE; i++)
413 buf[i] = '*';
414 buf[BUFSIZE] = 0;
415
416 va_list ap;
417 va_start(ap, format);
418
419 int n = wxVsnprintf(buf, max, format, ap);
420
421 va_end(ap);
422
423 // Prepare messages so that it is possible to see from the error which
424 // test was running.
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("'");
430
431 // turn them into std::strings
432 std::string errMsg(errStr.mb_str());
433 std::string overflowMsg(overflowStr.mb_str());
434
435 CPPUNIT_ASSERT_MESSAGE(errMsg,
436 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
437
438 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
439
440 for (i = max; i < BUFSIZE; i++)
441 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
442 }
443
444 void VsnprintfTestCase::Miscellaneous()
445 {
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);
451
452 DoMisc(6, wxT("123456"), 8, wxT("123456"));
453 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
454 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
455
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);
460
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"));
464
465 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
466 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
467 }