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