]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
added --disable-vararg_macros option to allow not using vararg macros even if they...
[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
35 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
36
37 #define ASSERT_STR_EQUAL( a, b ) \
38 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
39
40 #define CMP5(expected, x, y, z, w) \
41 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
42 \
43 ASSERT_STR_EQUAL( wxT(expected), buf );
44
45 #define CMP4(expected, x, y, z) \
46 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
47 \
48 ASSERT_STR_EQUAL( wxT(expected), buf );
49
50 #define CMP3(expected, x, y) \
51 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
52 \
53 ASSERT_STR_EQUAL( wxT(expected), buf );
54
55 #define CMP2(expected, x) \
56 wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
57 \
58 ASSERT_STR_EQUAL( wxT(expected), buf );
59
60 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
61 wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
62 \
63 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
64 wxString(buffer) )
65
66
67
68 // ----------------------------------------------------------------------------
69 // test class
70 // ----------------------------------------------------------------------------
71
72 class VsnprintfTestCase : public CppUnit::TestCase
73 {
74 public:
75 VsnprintfTestCase();
76
77 private:
78 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
79 CPPUNIT_TEST( D );
80 CPPUNIT_TEST( X );
81 CPPUNIT_TEST( O );
82 CPPUNIT_TEST( P );
83 CPPUNIT_TEST( N );
84 CPPUNIT_TEST( E );
85 CPPUNIT_TEST( F );
86 CPPUNIT_TEST( G );
87 CPPUNIT_TEST( S );
88 CPPUNIT_TEST( Asterisk );
89 CPPUNIT_TEST( Percent );
90 #ifdef wxLongLong_t
91 CPPUNIT_TEST( LongLong );
92 #endif
93
94 CPPUNIT_TEST( BigToSmallBuffer );
95 CPPUNIT_TEST( Miscellaneous );
96 CPPUNIT_TEST_SUITE_END();
97
98 void D();
99 void X();
100 void O();
101 void P();
102 void N();
103 void E();
104 void F();
105 void G();
106 void S();
107 void Asterisk();
108 void Percent();
109 #ifdef wxLongLong_t
110 void LongLong();
111 #endif
112 void Unicode();
113
114 void BigToSmallBuffer();
115 void Miscellaneous();
116 void Misc(wxChar *buffer, int size);
117
118 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
119 };
120
121 // register in the unnamed registry so that these tests are run by default
122 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
123
124 // also include in it's own registry so that these tests can be run alone
125 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
126
127 VsnprintfTestCase::VsnprintfTestCase()
128 {
129 }
130
131 void VsnprintfTestCase::D()
132 {
133 CMP3("+123456", "%+d", 123456);
134 CMP3("-123456", "%d", -123456);
135 CMP3(" 123456", "% d", 123456);
136 CMP3(" 123456", "%10d", 123456);
137 CMP3("0000123456", "%010d", 123456);
138 CMP3("-123456 ", "%-10d", -123456);
139 }
140
141 void VsnprintfTestCase::X()
142 {
143 CMP3("ABCD", "%X", 0xABCD);
144 CMP3("0XABCD", "%#X", 0xABCD);
145 CMP3("0xabcd", "%#x", 0xABCD);
146 }
147
148 void VsnprintfTestCase::O()
149 {
150 CMP3("1234567", "%o", 01234567);
151 CMP3("01234567", "%#o", 01234567);
152 }
153
154 void VsnprintfTestCase::P()
155 {
156 // WARNING: printing of pointers is not fully standard.
157 // GNU prints them as %#x except for NULL pointers which are
158 // printed as '(nil)'.
159 // MSVC always print them as %8X on 32 bit systems and as %16X
160 // on 64 bit systems
161 #ifdef __VISUALC__
162 #if SIZEOF_VOID_P == 4
163 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
164 CMP3("00000000", "%p", (void*)NULL);
165 #elif SIZEOF_VOID_P == 8
166 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
167 CMP3("0000000000000000", "%p", (void*)NULL);
168 #endif
169 #elif defined(__GNUG__)
170 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
171 CMP3("(nil)", "%p", (void*)NULL);
172 #endif
173 }
174
175 void VsnprintfTestCase::N()
176 {
177 int nchar;
178
179 wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar);
180 CPPUNIT_ASSERT_EQUAL( 7, nchar );
181 }
182
183 void VsnprintfTestCase::E()
184 {
185 // NB: there are no standards about the minimum exponent width
186 // (and the width of the %e conversion specifier refers to the
187 // mantissa, not to the exponent).
188 // Since newer MSVC versions use 3 digits as minimum exponent
189 // width while GNU libc uses 2 digits as minimum width, here we
190 // workaround this problem using for the exponent values with at
191 // least three digits.
192 // Some examples:
193 // printf("%e",2.342E+02);
194 // -> under MSVC7.1 prints: 2.342000e+002
195 // -> under GNU libc 2.4 prints: 2.342000e+02
196 CMP3("2.342000e+112", "%e",2.342E+112);
197 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
198 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
199 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
200
201 CMP3("-0.02342", "%G",-2.342E-02);
202 CMP3("3.1415E-116", "%G",3.1415e-116);
203 CMP3("0003.141500e+103", "%016e", 3141.5e100);
204 CMP3(" 3.141500e+103", "%16e", 3141.5e100);
205 CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
206 CMP3("3.142e+103", "%010.3e", 3141.5e100);
207 }
208
209 void VsnprintfTestCase::F()
210 {
211 CMP3("3.300000", "%5f", 3.3);
212 CMP3("3.000000", "%5f", 3.0);
213 CMP3("0.000100", "%5f", .999999E-4);
214 CMP3("0.000990", "%5f", .99E-3);
215 CMP3("3333.000000", "%5f", 3333.0);
216 }
217
218 void VsnprintfTestCase::G()
219 {
220 // NOTE: the same about E() testcase applies here...
221
222 CMP3(" 3.3", "%5g", 3.3);
223 CMP3(" 3", "%5g", 3.0);
224 CMP3("9.99999e-115", "%5g", .999999E-114);
225 CMP3("0.00099", "%5g", .99E-3);
226 CMP3(" 3333", "%5g", 3333.0);
227 CMP3(" 0.01", "%5g", 0.01);
228
229 CMP3(" 3", "%5.g", 3.3);
230 CMP3(" 3", "%5.g", 3.0);
231 CMP3("1e-114", "%5.g", .999999E-114);
232 CMP3("0.0001", "%5.g", 1.0E-4);
233 CMP3("0.001", "%5.g", .99E-3);
234 CMP3("3e+103", "%5.g", 3333.0E100);
235 CMP3(" 0.01", "%5.g", 0.01);
236
237 CMP3(" 3.3", "%5.2g", 3.3);
238 CMP3(" 3", "%5.2g", 3.0);
239 CMP3("1e-114", "%5.2g", .999999E-114);
240 CMP3("0.00099", "%5.2g", .99E-3);
241 CMP3("3.3e+103", "%5.2g", 3333.0E100);
242 CMP3(" 0.01", "%5.2g", 0.01);
243 }
244
245 void VsnprintfTestCase::S()
246 {
247 CMP3(" abc", "%5s", wxT("abc"));
248 CMP3(" a", "%5s", wxT("a"));
249 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
250 CMP3("abc ", "%-5s", wxT("abc"));
251 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
252
253 CMP3("abcde", "%.5s", wxT("abcdefghi"));
254
255 // do the same tests but with Unicode characters:
256 #if wxUSE_UNICODE
257 #define ALPHA "\x3B1"
258 #define BETA "\x3B2"
259 #define GAMMA "\x3B3"
260 #define DELTA "\x3B4"
261 #define EPSILON "\x3B5"
262 #define ZETA "\x3B6"
263 #define ETA "\x3B7"
264 #define THETA "\x3B8"
265 #define IOTA "\x3B9"
266
267 #define ABC ALPHA BETA GAMMA
268 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
269 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
270
271 CMP3(" " ABC, "%5s", wxT(ABC));
272 CMP3(" " ALPHA, "%5s", wxT(ALPHA));
273 CMP3(ABCDEFGHI, "%5s", wxT(ABCDEFGHI));
274 CMP3(ABC " ", "%-5s", wxT(ABC));
275 CMP3(ABCDEFGHI, "%-5s", wxT(ABCDEFGHI));
276
277 CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI));
278 #endif
279 }
280
281 void VsnprintfTestCase::Asterisk()
282 {
283 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
284 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
285 CMP5("0.1", "%*.*f", 3, 1, 0.123);
286
287 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
288 }
289
290 void VsnprintfTestCase::Percent()
291 {
292 // some tests without any argument passed through ...
293 CMP2("%", "%%");
294 CMP2("%%%", "%%%%%%");
295
296 CMP3("% abc", "%%%5s", wxT("abc"));
297 CMP3("% abc%", "%%%5s%%", wxT("abc"));
298
299 // do not test odd number of '%' symbols as different implementations
300 // of snprintf() give different outputs as this situation is not considered
301 // by any standard (in fact, GCC will also warn you about a spurious % if
302 // you write %%% as argument of some *printf function !)
303 // Compare(wxT("%"), wxT("%%%"));
304 }
305
306 #ifdef wxLongLong_t
307 void VsnprintfTestCase::LongLong()
308 {
309 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
310 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
311
312 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
313 }
314 #endif
315
316 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
317 {
318 // NB: remember that wx*printf could be mapped either to system
319 // implementation or to wx implementation.
320 // In the first case, when the output buffer is too small, the returned
321 // value can be the number of characters required for the output buffer
322 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
323 // just a negative number, usually -1; (this is how e.g. MSVC's
324 // *printf() behaves). Fortunately, in all implementations, when the
325 // output buffer is too small, it's nonetheless filled up to its max
326 // size.
327
328 // test without positionals
329 CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
330 "%i %li - test - %d %.3f",
331 123, (long int)444444444, 555, -0.666);
332
333 #if wxUSE_PRINTF_POS_PARAMS
334 // test with positional
335 CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
336 "%4$.3f %1$i - test - %2$li %3$d",
337 123, (long int)444444444, 555, -0.666);
338 #endif
339
340 // test unicode/ansi conversion specifiers
341 // NB: this line will output two warnings like these, on GCC:
342 // warning: use of 'h' length modifier with 's' type character (i.e.
343 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
344 // be ignored as here we explicitely want to test the wxSnprintf()
345 // behaviour in such case
346
347 CMPTOSIZE(buffer, size,
348 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
349 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
350 L"unicode!!", L'W', "ansi!!", 'w');
351 }
352
353 void VsnprintfTestCase::BigToSmallBuffer()
354 {
355 wxChar buf[1024], buf2[16], buf3[4], buf4;
356
357 Misc(buf, 1024);
358 Misc(buf2, 16);
359 Misc(buf3, 4);
360 Misc(&buf4, 1);
361 }
362
363 static void DoMisc(
364 int expectedLen,
365 const wxString& expectedString,
366 size_t max,
367 const wxChar *format, ...)
368 {
369 const size_t BUFSIZE = 16;
370 wxChar buf[BUFSIZE + 1];
371 size_t i;
372 static int count = 0;
373
374 wxASSERT(max <= BUFSIZE);
375
376 for (i = 0; i < BUFSIZE; i++)
377 buf[i] = '*';
378 buf[BUFSIZE] = 0;
379
380 va_list ap;
381 va_start(ap, format);
382
383 int n = wxVsnprintf(buf, max, format, ap);
384
385 va_end(ap);
386
387 // Prepare messages so that it is possible to see from the error which
388 // test was running.
389 wxString errStr, overflowStr;
390 errStr << _T("No.: ") << ++count << _T(", expected: ") << expectedLen
391 << _T(" '") << expectedString << _T("', result: ");
392 overflowStr << errStr << _T("buffer overflow");
393 errStr << n << _T(" '") << buf << _T("'");
394
395 // turn them into std::strings
396 std::string errMsg(errStr.mb_str());
397 std::string overflowMsg(overflowStr.mb_str());
398
399 CPPUNIT_ASSERT_MESSAGE(errMsg,
400 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
401
402 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
403
404 for (i = max; i < BUFSIZE; i++)
405 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
406 }
407
408 void VsnprintfTestCase::Miscellaneous()
409 {
410 // expectedLen, expectedString, max, format, ...
411 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
412 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
413 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
414 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
415
416 DoMisc(6, wxT("123456"), 8, wxT("123456"));
417 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
418 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
419
420 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
421 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
422 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
423 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
424
425 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
426 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
427 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
428
429 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
430 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
431 }