]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/vsnprintf.cpp
Disable test failing under VC6 only.
[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); \
75ac34ce 58 CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
35d3edcd
FM
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); \
75ac34ce 63 CPPUNIT_ASSERT_EQUAL( r, 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); \
75ac34ce 68 CPPUNIT_ASSERT_EQUAL( r, 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); \
75ac34ce 73 CPPUNIT_ASSERT_EQUAL( r, 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)); \
75ac34ce 78 CPPUNIT_ASSERT_EQUAL( r, 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 92// this is the same as wxSnprintf() but it passes the format string to
862bb5c7 93// wxVsnprintf() without using WX_ATTRIBUTE_PRINTF and thus suppresses the gcc
64b989fe
VZ
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:
16f71580
FM
119 VsnprintfTestCase() {}
120
121 virtual void setUp();
122
7a828c7f
VZ
123private:
124 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
7de1a0d1 125 CPPUNIT_TEST( C );
5921d2f8
VZ
126 CPPUNIT_TEST( D );
127 CPPUNIT_TEST( X );
128 CPPUNIT_TEST( O );
129 CPPUNIT_TEST( P );
130 CPPUNIT_TEST( N );
7a828c7f
VZ
131 CPPUNIT_TEST( E );
132 CPPUNIT_TEST( F );
133 CPPUNIT_TEST( G );
134 CPPUNIT_TEST( S );
829d1102 135 CPPUNIT_TEST( Asterisk );
412a5c57 136 CPPUNIT_TEST( Percent );
b95d0984 137#ifdef wxLongLong_t
412a5c57 138 CPPUNIT_TEST( LongLong );
b95d0984 139#endif
7a828c7f
VZ
140
141 CPPUNIT_TEST( BigToSmallBuffer );
06b5b352 142#if wxUSE_WXVSNPRINTF
f2bbe5b6 143 CPPUNIT_TEST( WrongFormatStrings );
06b5b352 144#endif // wxUSE_WXVSNPRINTF
cc4a79c0 145 CPPUNIT_TEST( Miscellaneous );
36340644
FM
146 CPPUNIT_TEST( GlibcMisc1 );
147 CPPUNIT_TEST( GlibcMisc2 );
7a828c7f
VZ
148 CPPUNIT_TEST_SUITE_END();
149
7de1a0d1 150 void C();
5921d2f8
VZ
151 void D();
152 void X();
153 void O();
154 void P();
155 void N();
7a828c7f
VZ
156 void E();
157 void F();
158 void G();
159 void S();
829d1102 160 void Asterisk();
412a5c57 161 void Percent();
b95d0984 162#ifdef wxLongLong_t
412a5c57 163 void LongLong();
b95d0984 164#endif
412a5c57 165 void Unicode();
7a828c7f 166
35d3edcd
FM
167 template<typename T>
168 void DoBigToSmallBuffer(T *buffer, int size);
7a828c7f 169 void BigToSmallBuffer();
35d3edcd 170
06b5b352 171#if wxUSE_WXVSNPRINTF
f2bbe5b6 172 void WrongFormatStrings();
06b5b352 173#endif // wxUSE_WXVSNPRINTF
7a828c7f 174
7de1a0d1
VZ
175 // compares the expectedString and the result of wxVsnprintf() char by char
176 // for all its lenght (not only for first expectedLen chars) and also
177 // checks the return value
178 void DoMisc(int expectedLen, const wxString& expectedString,
179 size_t max, const wxChar *format, ...);
35d3edcd 180 void Miscellaneous();
7de1a0d1 181
36340644
FM
182 void GlibcMisc1();
183 void GlibcMisc2();
184
7a828c7f
VZ
185 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
186};
187
188// register in the unnamed registry so that these tests are run by default
189CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
190
191// also include in it's own registry so that these tests can be run alone
192CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
193
16f71580 194void VsnprintfTestCase::setUp()
7a828c7f 195{
103339b0
FM
196 // this call is required to avoid check failures when running on machines
197 // with a locale where the decimal point is not '.'
ca5016d4 198 wxSetlocale(LC_ALL, "C");
7a828c7f
VZ
199}
200
7de1a0d1
VZ
201void VsnprintfTestCase::C()
202{
203 CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
204
205 // NOTE:
206 // the NULL characters _can_ be passed to %c to e.g. create strings
207 // with embedded NULs (because strings are not always supposed to be
208 // NUL-terminated).
209
210 DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
211}
212
5921d2f8
VZ
213void VsnprintfTestCase::D()
214{
215 CMP3("+123456", "%+d", 123456);
216 CMP3("-123456", "%d", -123456);
217 CMP3(" 123456", "% d", 123456);
218 CMP3(" 123456", "%10d", 123456);
219 CMP3("0000123456", "%010d", 123456);
220 CMP3("-123456 ", "%-10d", -123456);
221}
222
223void VsnprintfTestCase::X()
224{
225 CMP3("ABCD", "%X", 0xABCD);
226 CMP3("0XABCD", "%#X", 0xABCD);
227 CMP3("0xabcd", "%#x", 0xABCD);
228}
229
230void VsnprintfTestCase::O()
231{
232 CMP3("1234567", "%o", 01234567);
233 CMP3("01234567", "%#o", 01234567);
234}
235
236void VsnprintfTestCase::P()
237{
238 // WARNING: printing of pointers is not fully standard.
239 // GNU prints them as %#x except for NULL pointers which are
240 // printed as '(nil)'.
241 // MSVC always print them as %8X on 32 bit systems and as %16X
242 // on 64 bit systems
3879212e
VZ
243 // mingw32 uses MSVC CRT by default so uses the same rules
244#if defined(__VISUALC__) || (defined(__MINGW32__) && !__USE_MINGW_ANSI_STDIO)
5921d2f8
VZ
245 #if SIZEOF_VOID_P == 4
246 CMP3("00ABCDEF", "%p", (void*)0xABCDEF);
247 CMP3("00000000", "%p", (void*)NULL);
248 #elif SIZEOF_VOID_P == 8
249 CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
250 CMP3("0000000000000000", "%p", (void*)NULL);
251 #endif
3879212e
VZ
252#elif defined(__MINGW32__)
253 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
254 CMP3("0", "%p", (void*)NULL);
5921d2f8
VZ
255#elif defined(__GNUG__)
256 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
257 CMP3("(nil)", "%p", (void*)NULL);
258#endif
259}
260
261void VsnprintfTestCase::N()
262{
263 int nchar;
264
9a83f860 265 wxSnprintf(buf, MAX_TEST_LEN, wxT("%d %s%n\n"), 3, wxT("bears"), &nchar);
5921d2f8
VZ
266 CPPUNIT_ASSERT_EQUAL( 7, nchar );
267}
268
7a828c7f
VZ
269void VsnprintfTestCase::E()
270{
bacd54f6
VZ
271 // NB: there are no standards about the minimum exponent width
272 // (and the width of the %e conversion specifier refers to the
273 // mantissa, not to the exponent).
274 // Since newer MSVC versions use 3 digits as minimum exponent
275 // width while GNU libc uses 2 digits as minimum width, here we
276 // workaround this problem using for the exponent values with at
277 // least three digits.
278 // Some examples:
279 // printf("%e",2.342E+02);
280 // -> under MSVC7.1 prints: 2.342000e+002
281 // -> under GNU libc 2.4 prints: 2.342000e+02
829d1102
VZ
282 CMP3("2.342000e+112", "%e",2.342E+112);
283 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
284 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
285 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
286
287 CMP3("-0.02342", "%G",-2.342E-02);
288 CMP3("3.1415E-116", "%G",3.1415e-116);
289 CMP3("0003.141500e+103", "%016e", 3141.5e100);
290 CMP3(" 3.141500e+103", "%16e", 3141.5e100);
291 CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
292 CMP3("3.142e+103", "%010.3e", 3141.5e100);
7a828c7f
VZ
293}
294
295void VsnprintfTestCase::F()
296{
829d1102
VZ
297 CMP3("3.300000", "%5f", 3.3);
298 CMP3("3.000000", "%5f", 3.0);
299 CMP3("0.000100", "%5f", .999999E-4);
300 CMP3("0.000990", "%5f", .99E-3);
301 CMP3("3333.000000", "%5f", 3333.0);
7a828c7f
VZ
302}
303
304void VsnprintfTestCase::G()
305{
bacd54f6
VZ
306 // NOTE: the same about E() testcase applies here...
307
829d1102
VZ
308 CMP3(" 3.3", "%5g", 3.3);
309 CMP3(" 3", "%5g", 3.0);
310 CMP3("9.99999e-115", "%5g", .999999E-114);
311 CMP3("0.00099", "%5g", .99E-3);
312 CMP3(" 3333", "%5g", 3333.0);
313 CMP3(" 0.01", "%5g", 0.01);
314
315 CMP3(" 3", "%5.g", 3.3);
316 CMP3(" 3", "%5.g", 3.0);
317 CMP3("1e-114", "%5.g", .999999E-114);
318 CMP3("0.0001", "%5.g", 1.0E-4);
319 CMP3("0.001", "%5.g", .99E-3);
320 CMP3("3e+103", "%5.g", 3333.0E100);
321 CMP3(" 0.01", "%5.g", 0.01);
322
323 CMP3(" 3.3", "%5.2g", 3.3);
324 CMP3(" 3", "%5.2g", 3.0);
325 CMP3("1e-114", "%5.2g", .999999E-114);
326 CMP3("0.00099", "%5.2g", .99E-3);
327 CMP3("3.3e+103", "%5.2g", 3333.0E100);
328 CMP3(" 0.01", "%5.2g", 0.01);
7a828c7f
VZ
329}
330
331void VsnprintfTestCase::S()
332{
829d1102
VZ
333 CMP3(" abc", "%5s", wxT("abc"));
334 CMP3(" a", "%5s", wxT("a"));
335 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
336 CMP3("abc ", "%-5s", wxT("abc"));
337 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
338
339 CMP3("abcde", "%.5s", wxT("abcdefghi"));
340
412a5c57 341 // do the same tests but with Unicode characters:
35d3edcd
FM
342#if wxUSE_UNICODE
343
344 // Unicode code points from U+03B1 to U+03B9 are the greek letters alpha-iota;
345 // UTF8 encoding of such code points is 0xCEB1 to 0xCEB9
346
347#define ALPHA "\xCE\xB1"
348 // alpha
349#define ABC "\xCE\xB1\xCE\xB2\xCE\xB3"
350 // alpha+beta+gamma
351#define ABCDE "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
352 // alpha+beta+gamma+delta+epsilon
353#define ABCDEFGHI "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9"
354 // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
355
245ff47f
VZ
356 // the 'expected' and 'arg' parameters of this macro are supposed to be
357 // UTF-8 strings
358#define CMP3_UTF8(expected, fmt, arg) \
359 CPPUNIT_ASSERT_EQUAL \
360 ( \
361 wxString::FromUTF8(expected).length(), \
362 wxSnprintf(buf, MAX_TEST_LEN, fmt, wxString::FromUTF8(arg)) \
363 ); \
364 CPPUNIT_ASSERT_EQUAL \
365 ( \
366 wxString::FromUTF8(expected), \
367 buf \
368 )
369
370 CMP3_UTF8(" " ABC, "%5s", ABC);
371 CMP3_UTF8(" " ALPHA, "%5s", ALPHA);
372 CMP3_UTF8(ABCDEFGHI, "%5s", ABCDEFGHI);
373 CMP3_UTF8(ABC " ", "%-5s", ABC);
374 CMP3_UTF8(ABCDEFGHI, "%-5s", ABCDEFGHI);
375 CMP3_UTF8(ABCDE, "%.5s", ABCDEFGHI);
376#endif // wxUSE_UNICODE
7de1a0d1 377
35d3edcd 378 // test a string which has a NULL character after "ab";
7de1a0d1
VZ
379 // obviously it should be handled exactly like just as "ab"
380 CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
412a5c57
VZ
381}
382
383void VsnprintfTestCase::Asterisk()
384{
385 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
386 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
387 CMP5("0.1", "%*.*f", 3, 1, 0.123);
388
389 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
35d3edcd
FM
390
391 CMP4(" a", "%*c", 8, 'a');
392 CMP4(" four", "%*s", 8, "four");
393 CMP6(" four four", "%*s %*s", 8, "four", 6, "four");
412a5c57
VZ
394}
395
396void VsnprintfTestCase::Percent()
397{
829d1102 398 // some tests without any argument passed through ...
10b7d13c
MW
399 CMP2("%", "%%");
400 CMP2("%%%", "%%%%%%");
829d1102 401
412a5c57
VZ
402 CMP3("% abc", "%%%5s", wxT("abc"));
403 CMP3("% abc%", "%%%5s%%", wxT("abc"));
404
829d1102
VZ
405 // do not test odd number of '%' symbols as different implementations
406 // of snprintf() give different outputs as this situation is not considered
407 // by any standard (in fact, GCC will also warn you about a spurious % if
408 // you write %%% as argument of some *printf function !)
409 // Compare(wxT("%"), wxT("%%%"));
410}
411
b95d0984 412#ifdef wxLongLong_t
412a5c57 413void VsnprintfTestCase::LongLong()
829d1102 414{
b95d0984
MW
415 CMP3("123456789", "%lld", (wxLongLong_t)123456789);
416 CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
829d1102 417
b95d0984 418 CMP3("123456789", "%llu", (wxULongLong_t)123456789);
b726328b
VZ
419
420#ifdef __WXMSW__
421 CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
6ed28b94 422 CMP3("123456789abcdef", "%I64x", wxLL(0x123456789abcdef));
b726328b 423#endif
7a828c7f 424}
b95d0984 425#endif
7a828c7f 426
35d3edcd
FM
427// this test is only for our own implementation, the system implementation
428// doesn't always give errors for invalid format strings (e.g. glibc doesn't)
429// and as it's not required too (the behaviour is "undefined" according to the
430// spec), there is really no sense in testing for it
431#if wxUSE_WXVSNPRINTF
432
433void VsnprintfTestCase::WrongFormatStrings()
434{
435 // test how wxVsnprintf() behaves with wrong format string:
436
437#if 0
438 // NB: the next 2 tests currently return an error but it would be nice
439 // if they didn't (see ticket #9367)
440
441 // two positionals with the same index:
442 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
443 CPPUNIT_ASSERT(r != -1);
444
445 // three positionals with the same index mixed with other pos args:
446 r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
447 CPPUNIT_ASSERT(r != -1);
448#endif
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// BigToSmallBuffer() test case helper:
7315ad28 463template<typename T>
35d3edcd 464void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
7a828c7f 465{
7de1a0d1
VZ
466 // Remember that wx*printf could be mapped either to system
467 // implementation or to wx implementation.
468 // In the first case, when the output buffer is too small, the returned
469 // value can be the number of characters required for the output buffer
470 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
471 // just a negative number, usually -1; (this is how e.g. MSVC's
472 // *printf() behaves). Luckily, in all implementations, when the
473 // output buffer is too small, it's nonetheless filled up to its max size.
f2bbe5b6 474 //
7de1a0d1
VZ
475 // Note that in the second case (i.e. when we're using our own implementation),
476 // wxVsnprintf() will return the number of characters written in the standard
64b989fe 477 // output or
7de1a0d1
VZ
478 // -1 if there was an error in the format string
479 // maxSize+1 if the output buffer is too small
7a828c7f 480
35d3edcd
FM
481 wxString errStr;
482 errStr << "The size of the buffer was " << size;
483 std::string errMsg(errStr.mb_str());
484
7a828c7f 485 // test without positionals
35d3edcd
FM
486 CMPTOSIZE(buffer, size, errMsg,
487 "123456789012 - test - 123 -4.567",
488 "%i%li - test - %d %.3f",
489 123, (long int)456789012, 123, -4.567);
829d1102
VZ
490
491#if wxUSE_PRINTF_POS_PARAMS
492 // test with positional
35d3edcd
FM
493 CMPTOSIZE(buffer, size, errMsg,
494 "-4.567 123 - test - 456789012 123",
10b7d13c 495 "%4$.3f %1$i - test - %2$li %3$d",
35d3edcd 496 123, (long int)456789012, 123, -4.567);
829d1102 497#endif
7a828c7f
VZ
498
499 // test unicode/ansi conversion specifiers
64b989fe
VZ
500 //
501 // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
502 // format and gcc would warn about this otherwise
503
504 r = wxUnsafeSnprintf(buffer, size,
9a83f860 505 wxT("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
35d3edcd 506 L"unicode", L'U', "ansi", 'A');
103339b0 507 wxString expected =
35d3edcd 508 wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size - 1);
103339b0 509
64b989fe
VZ
510 CPPUNIT_ASSERT( r != -1 );
511 CPPUNIT_ASSERT_EQUAL(
103339b0 512 expected,
64b989fe
VZ
513 wxString(buffer)
514 );
7a828c7f
VZ
515}
516
517void VsnprintfTestCase::BigToSmallBuffer()
518{
5098c258
VZ
519 // VC6 can't compile this code
520#if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
1a5cd56a 521#if wxUSE_UNICODE
b899f05f 522 wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
35d3edcd
FM
523 DoBigToSmallBuffer(bufw, 1024);
524 DoBigToSmallBuffer(bufw2, 16);
525 DoBigToSmallBuffer(bufw3, 4);
526 DoBigToSmallBuffer(&bufw4, 1);
1a5cd56a 527#endif // wxUSE_UNICODE
7315ad28 528
5098c258 529 char bufa[1024], bufa2[16], bufa3[4], bufa4;
35d3edcd
FM
530 DoBigToSmallBuffer(bufa, 1024);
531 DoBigToSmallBuffer(bufa2, 16);
532 DoBigToSmallBuffer(bufa3, 4);
533 DoBigToSmallBuffer(&bufa4, 1);
5098c258 534#endif // !VC6
7a828c7f 535}
b95d0984 536
35d3edcd 537// Miscellaneous() test case helper:
7de1a0d1 538void VsnprintfTestCase::DoMisc(
b95d0984
MW
539 int expectedLen,
540 const wxString& expectedString,
541 size_t max,
542 const wxChar *format, ...)
543{
7de1a0d1 544 const size_t BUFSIZE = MAX_TEST_LEN - 1;
b95d0984
MW
545 size_t i;
546 static int count = 0;
547
548 wxASSERT(max <= BUFSIZE);
549
550 for (i = 0; i < BUFSIZE; i++)
551 buf[i] = '*';
552 buf[BUFSIZE] = 0;
553
554 va_list ap;
555 va_start(ap, format);
64b989fe 556
b95d0984
MW
557 int n = wxVsnprintf(buf, max, format, ap);
558
559 va_end(ap);
560
561 // Prepare messages so that it is possible to see from the error which
562 // test was running.
563 wxString errStr, overflowStr;
9a83f860
VZ
564 errStr << wxT("No.: ") << ++count << wxT(", expected: ") << expectedLen
565 << wxT(" '") << expectedString << wxT("', result: ");
566 overflowStr << errStr << wxT("buffer overflow");
567 errStr << n << wxT(" '") << buf << wxT("'");
b95d0984
MW
568
569 // turn them into std::strings
570 std::string errMsg(errStr.mb_str());
571 std::string overflowMsg(overflowStr.mb_str());
572
573 CPPUNIT_ASSERT_MESSAGE(errMsg,
574 (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
575
576 CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
577
578 for (i = max; i < BUFSIZE; i++)
579 CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
580}
581
cc4a79c0 582void VsnprintfTestCase::Miscellaneous()
b95d0984
MW
583{
584 // expectedLen, expectedString, max, format, ...
35d3edcd
FM
585 DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
586 DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
587 DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
588 DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
589
590 DoMisc(6, wxT("123456"), 8, wxT("123456"));
591 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
592 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
593
594 DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
595 DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
596 DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
597 DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
598
599 DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
600 DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
601 DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
602
603 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
604 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
b95d0984 605}
05b5a45f 606
36340644
FM
607
608/* (C) Copyright C E Chew
609*
610* Feel free to copy, use and distribute this software provided:
611*
612* 1. you do not pretend that you wrote it
613* 2. you leave this copyright notice intact.
614*/
615
616void VsnprintfTestCase::GlibcMisc1()
617{
618 CMP3(" ", "%5.s", "xyz");
619 CMP3(" 33", "%5.f", 33.3);
620#if defined(__VISUALC__)
621 // see the previous notes about the minimum width of mantissa:
622 CMP3(" 3e+008", "%8.e", 33.3e7);
623 CMP3(" 3E+008", "%8.E", 33.3e7);
624 CMP3("3e+001", "%.g", 33.3);
625 CMP3("3E+001", "%.G", 33.3);
626#else
627 CMP3(" 3e+08", "%8.e", 33.3e7);
628 CMP3(" 3E+08", "%8.E", 33.3e7);
629 CMP3("3e+01", "%.g", 33.3);
630 CMP3("3E+01", "%.G", 33.3);
631#endif
632}
633
634void VsnprintfTestCase::GlibcMisc2()
635{
636 int prec;
637 wxString test_format;
638
639 prec = 0;
640 CMP4("3", "%.*g", prec, 3.3);
641
642 prec = 0;
643 CMP4("3", "%.*G", prec, 3.3);
644
645 prec = 0;
646 CMP4(" 3", "%7.*G", prec, 3.33);
647
648 prec = 3;
649 CMP4(" 041", "%04.*o", prec, 33);
650
651 prec = 7;
652 CMP4(" 0000033", "%09.*u", prec, 33);
653
654 prec = 3;
655 CMP4(" 021", "%04.*x", prec, 33);
656
657 prec = 3;
658 CMP4(" 021", "%04.*X", prec, 33);
659}
660
05b5a45f 661#endif // wxUSE_WXVSNPRINTF
36340644 662