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