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