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