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