]>
git.saurik.com Git - wxWidgets.git/blob - 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)
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
24 #include "wx/wxchar.h"
29 #define MAX_TEST_LEN 1024
33 static wxChar buf
[MAX_TEST_LEN
];
35 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
37 #define ASSERT_STR_EQUAL( a, b ) \
38 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
40 #define CMP5(expected, x, y, z, w) \
41 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
43 ASSERT_STR_EQUAL( wxT(expected), buf );
45 #define CMP4(expected, x, y, z) \
46 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
48 ASSERT_STR_EQUAL( wxT(expected), buf );
50 #define CMP3(expected, x, y) \
51 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
53 ASSERT_STR_EQUAL( wxT(expected), buf );
55 #define CMP2(expected, x) \
56 wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
58 ASSERT_STR_EQUAL( wxT(expected), buf );
60 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
61 wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
63 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class VsnprintfTestCase
: public CppUnit::TestCase
78 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
88 CPPUNIT_TEST( Asterisk
);
89 CPPUNIT_TEST( Percent
);
91 CPPUNIT_TEST( LongLong
);
94 CPPUNIT_TEST( BigToSmallBuffer
);
95 CPPUNIT_TEST( Miscellaneous
);
96 CPPUNIT_TEST_SUITE_END();
114 void BigToSmallBuffer();
115 void Miscellaneous();
116 void Misc(wxChar
*buffer
, int size
);
118 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
121 // register in the unnamed registry so that these tests are run by default
122 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
124 // also include in it's own registry so that these tests can be run alone
125 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
127 VsnprintfTestCase::VsnprintfTestCase()
131 void VsnprintfTestCase::D()
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);
141 void VsnprintfTestCase::X()
143 CMP3("ABCD", "%X", 0xABCD);
144 CMP3("0XABCD", "%#X", 0xABCD);
145 CMP3("0xabcd", "%#x", 0xABCD);
148 void VsnprintfTestCase::O()
150 CMP3("1234567", "%o", 01234567);
151 CMP3("01234567", "%#o", 01234567);
154 void VsnprintfTestCase::P()
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
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
);
169 #elif defined(__GNUG__)
170 CMP3("0xabcdef", "%p", (void*)0xABCDEF);
171 CMP3("(nil)", "%p", (void*)NULL
);
175 void VsnprintfTestCase::N()
179 wxSnprintf(buf
, MAX_TEST_LEN
, _T("%d %s%n\n"), 3, _T("bears"), &nchar
);
180 CPPUNIT_ASSERT_EQUAL( 7, nchar
);
183 void VsnprintfTestCase::E()
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.
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);
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
);
209 void VsnprintfTestCase::F()
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);
218 void VsnprintfTestCase::G()
220 // NOTE: the same about E() testcase applies here...
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);
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);
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);
245 void VsnprintfTestCase::S()
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"));
253 CMP3("abcde", "%.5s", wxT("abcdefghi"));
255 // do the same tests but with Unicode characters:
257 #define ALPHA "\x3B1"
259 #define GAMMA "\x3B3"
260 #define DELTA "\x3B4"
261 #define EPSILON "\x3B5"
264 #define THETA "\x3B8"
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
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
));
277 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
281 void VsnprintfTestCase::Asterisk()
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);
287 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
290 void VsnprintfTestCase::Percent()
292 // some tests without any argument passed through ...
294 CMP2("%%%", "%%%%%%");
296 CMP3("% abc", "%%%5s", wxT("abc"));
297 CMP3("% abc%", "%%%5s%%", wxT("abc"));
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("%%%"));
307 void VsnprintfTestCase::LongLong()
309 CMP3("123456789", "%lld", (wxLongLong_t
)123456789);
310 CMP3("-123456789", "%lld", (wxLongLong_t
)-123456789);
312 CMP3("123456789", "%llu", (wxULongLong_t
)123456789);
316 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
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
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);
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);
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
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');
353 void VsnprintfTestCase::BigToSmallBuffer()
355 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;
365 const wxString
& expectedString
,
367 const wxChar
*format
, ...)
369 const size_t BUFSIZE
= 16;
370 wxChar buf
[BUFSIZE
+ 1];
372 static int count
= 0;
374 wxASSERT(max
<= BUFSIZE
);
376 for (i
= 0; i
< BUFSIZE
; i
++)
381 va_start(ap
, format
);
383 int n
= wxVsnprintf(buf
, max
, format
, ap
);
387 // Prepare messages so that it is possible to see from the error which
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("'");
395 // turn them into std::strings
396 std::string
errMsg(errStr
.mb_str());
397 std::string
overflowMsg(overflowStr
.mb_str());
399 CPPUNIT_ASSERT_MESSAGE(errMsg
,
400 (expectedLen
== -1 && size_t(n
) >= max
) || expectedLen
== n
);
402 CPPUNIT_ASSERT_MESSAGE(errMsg
, expectedString
== buf
);
404 for (i
= max
; i
< BUFSIZE
; i
++)
405 CPPUNIT_ASSERT_MESSAGE(overflowMsg
, buf
[i
] == '*');
408 void VsnprintfTestCase::Miscellaneous()
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);
416 DoMisc(6, wxT("123456"), 8, wxT("123456"));
417 DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
418 DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
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);
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"));
429 DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
430 DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);