]>
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 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/wx.h" | |
24 | #include "wx/wxchar.h" | |
25 | #endif // WX_PRECOMP | |
26 | ||
27 | ||
7a828c7f | 28 | |
829d1102 VZ |
29 | #define MAX_TEST_LEN 1024 |
30 | ||
31 | ||
32 | // temporary buffers | |
10b7d13c | 33 | static wxChar buf[MAX_TEST_LEN]; |
829d1102 VZ |
34 | |
35 | // these macros makes it possible to write all tests without repeating a lot of times wxT() macro | |
36 | ||
10b7d13c | 37 | #define ASSERT_STR_EQUAL( a, b ) \ |
3c8813b6 | 38 | CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) ); |
10b7d13c | 39 | |
829d1102 VZ |
40 | #define CMP5(expected, x, y, z, w) \ |
41 | wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \ | |
829d1102 | 42 | \ |
10b7d13c | 43 | ASSERT_STR_EQUAL( wxT(expected), buf ); |
829d1102 VZ |
44 | |
45 | #define CMP4(expected, x, y, z) \ | |
46 | wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \ | |
829d1102 | 47 | \ |
10b7d13c | 48 | ASSERT_STR_EQUAL( wxT(expected), buf ); |
829d1102 VZ |
49 | |
50 | #define CMP3(expected, x, y) \ | |
51 | wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \ | |
829d1102 | 52 | \ |
10b7d13c | 53 | ASSERT_STR_EQUAL( wxT(expected), buf ); |
829d1102 VZ |
54 | |
55 | #define CMP2(expected, x) \ | |
56 | wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \ | |
829d1102 | 57 | \ |
10b7d13c MW |
58 | ASSERT_STR_EQUAL( wxT(expected), buf ); |
59 | ||
3c8813b6 MW |
60 | #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \ |
61 | wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \ | |
62 | \ | |
63 | CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \ | |
64 | wxString(buffer) ) | |
829d1102 | 65 | |
829d1102 | 66 | |
7a828c7f VZ |
67 | |
68 | // ---------------------------------------------------------------------------- | |
69 | // test class | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | class VsnprintfTestCase : public CppUnit::TestCase | |
73 | { | |
74 | public: | |
75 | VsnprintfTestCase(); | |
76 | ||
77 | private: | |
78 | CPPUNIT_TEST_SUITE( VsnprintfTestCase ); | |
5921d2f8 VZ |
79 | CPPUNIT_TEST( D ); |
80 | CPPUNIT_TEST( X ); | |
81 | CPPUNIT_TEST( O ); | |
82 | CPPUNIT_TEST( P ); | |
83 | CPPUNIT_TEST( N ); | |
7a828c7f VZ |
84 | CPPUNIT_TEST( E ); |
85 | CPPUNIT_TEST( F ); | |
86 | CPPUNIT_TEST( G ); | |
87 | CPPUNIT_TEST( S ); | |
829d1102 | 88 | CPPUNIT_TEST( Asterisk ); |
412a5c57 | 89 | CPPUNIT_TEST( Percent ); |
b95d0984 | 90 | #ifdef wxLongLong_t |
412a5c57 | 91 | CPPUNIT_TEST( LongLong ); |
b95d0984 | 92 | #endif |
7a828c7f VZ |
93 | |
94 | CPPUNIT_TEST( BigToSmallBuffer ); | |
b95d0984 | 95 | CPPUNIT_TEST( Scratch ); |
7a828c7f VZ |
96 | CPPUNIT_TEST_SUITE_END(); |
97 | ||
5921d2f8 VZ |
98 | void D(); |
99 | void X(); | |
100 | void O(); | |
101 | void P(); | |
102 | void N(); | |
7a828c7f VZ |
103 | void E(); |
104 | void F(); | |
105 | void G(); | |
106 | void S(); | |
829d1102 | 107 | void Asterisk(); |
412a5c57 | 108 | void Percent(); |
b95d0984 | 109 | #ifdef wxLongLong_t |
412a5c57 | 110 | void LongLong(); |
b95d0984 | 111 | #endif |
412a5c57 | 112 | void Unicode(); |
7a828c7f VZ |
113 | |
114 | void BigToSmallBuffer(); | |
b95d0984 | 115 | void Scratch(); |
7a828c7f | 116 | void Misc(wxChar *buffer, int size); |
7a828c7f VZ |
117 | |
118 | DECLARE_NO_COPY_CLASS(VsnprintfTestCase) | |
119 | }; | |
120 | ||
121 | // register in the unnamed registry so that these tests are run by default | |
122 | CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase ); | |
123 | ||
124 | // also include in it's own registry so that these tests can be run alone | |
125 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" ); | |
126 | ||
127 | VsnprintfTestCase::VsnprintfTestCase() | |
128 | { | |
129 | } | |
130 | ||
5921d2f8 VZ |
131 | void VsnprintfTestCase::D() |
132 | { | |
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); | |
139 | } | |
140 | ||
141 | void VsnprintfTestCase::X() | |
142 | { | |
143 | CMP3("ABCD", "%X", 0xABCD); | |
144 | CMP3("0XABCD", "%#X", 0xABCD); | |
145 | CMP3("0xabcd", "%#x", 0xABCD); | |
146 | } | |
147 | ||
148 | void VsnprintfTestCase::O() | |
149 | { | |
150 | CMP3("1234567", "%o", 01234567); | |
151 | CMP3("01234567", "%#o", 01234567); | |
152 | } | |
153 | ||
154 | void VsnprintfTestCase::P() | |
155 | { | |
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 | |
160 | // on 64 bit systems | |
161 | #ifdef __VISUALC__ | |
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); | |
168 | #endif | |
169 | #elif defined(__GNUG__) | |
170 | CMP3("0xabcdef", "%p", (void*)0xABCDEF); | |
171 | CMP3("(nil)", "%p", (void*)NULL); | |
172 | #endif | |
173 | } | |
174 | ||
175 | void VsnprintfTestCase::N() | |
176 | { | |
177 | int nchar; | |
178 | ||
5677ce32 | 179 | wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar); |
5921d2f8 VZ |
180 | CPPUNIT_ASSERT_EQUAL( 7, nchar ); |
181 | } | |
182 | ||
7a828c7f VZ |
183 | void VsnprintfTestCase::E() |
184 | { | |
bacd54f6 VZ |
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. | |
192 | // Some examples: | |
193 | // printf("%e",2.342E+02); | |
194 | // -> under MSVC7.1 prints: 2.342000e+002 | |
195 | // -> under GNU libc 2.4 prints: 2.342000e+02 | |
829d1102 VZ |
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); | |
200 | ||
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); | |
7a828c7f VZ |
207 | } |
208 | ||
209 | void VsnprintfTestCase::F() | |
210 | { | |
829d1102 VZ |
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); | |
7a828c7f VZ |
216 | } |
217 | ||
218 | void VsnprintfTestCase::G() | |
219 | { | |
bacd54f6 VZ |
220 | // NOTE: the same about E() testcase applies here... |
221 | ||
829d1102 VZ |
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); | |
228 | ||
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); | |
236 | ||
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); | |
7a828c7f VZ |
243 | } |
244 | ||
245 | void VsnprintfTestCase::S() | |
246 | { | |
829d1102 VZ |
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")); | |
252 | ||
253 | CMP3("abcde", "%.5s", wxT("abcdefghi")); | |
254 | ||
412a5c57 VZ |
255 | // do the same tests but with Unicode characters: |
256 | #if wxUSE_UNICODE | |
257 | #define ALPHA "\x3B1" | |
258 | #define BETA "\x3B2" | |
259 | #define GAMMA "\x3B3" | |
260 | #define DELTA "\x3B4" | |
261 | #define EPSILON "\x3B5" | |
262 | #define ZETA "\x3B6" | |
263 | #define ETA "\x3B7" | |
264 | #define THETA "\x3B8" | |
265 | #define IOTA "\x3B9" | |
266 | ||
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 | |
270 | ||
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)); | |
276 | ||
277 | CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI)); | |
278 | #endif | |
279 | } | |
280 | ||
281 | void VsnprintfTestCase::Asterisk() | |
282 | { | |
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); | |
286 | ||
287 | CMP4("%0.002", "%%%.*f", 3, 0.0023456789); | |
288 | } | |
289 | ||
290 | void VsnprintfTestCase::Percent() | |
291 | { | |
829d1102 | 292 | // some tests without any argument passed through ... |
10b7d13c MW |
293 | CMP2("%", "%%"); |
294 | CMP2("%%%", "%%%%%%"); | |
829d1102 | 295 | |
412a5c57 VZ |
296 | CMP3("% abc", "%%%5s", wxT("abc")); |
297 | CMP3("% abc%", "%%%5s%%", wxT("abc")); | |
298 | ||
829d1102 VZ |
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("%%%")); | |
304 | } | |
305 | ||
b95d0984 | 306 | #ifdef wxLongLong_t |
412a5c57 | 307 | void VsnprintfTestCase::LongLong() |
829d1102 | 308 | { |
b95d0984 MW |
309 | CMP3("123456789", "%lld", (wxLongLong_t)123456789); |
310 | CMP3("-123456789", "%lld", (wxLongLong_t)-123456789); | |
829d1102 | 311 | |
b95d0984 | 312 | CMP3("123456789", "%llu", (wxULongLong_t)123456789); |
7a828c7f | 313 | } |
b95d0984 | 314 | #endif |
7a828c7f VZ |
315 | |
316 | void VsnprintfTestCase::Misc(wxChar *buffer, int size) | |
317 | { | |
10b7d13c MW |
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 | |
326 | // size. | |
7a828c7f VZ |
327 | |
328 | // test without positionals | |
10b7d13c MW |
329 | CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666", |
330 | "%i %li - test - %d %.3f", | |
829d1102 VZ |
331 | 123, (long int)444444444, 555, -0.666); |
332 | ||
333 | #if wxUSE_PRINTF_POS_PARAMS | |
334 | // test with positional | |
10b7d13c MW |
335 | CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555", |
336 | "%4$.3f %1$i - test - %2$li %3$d", | |
829d1102 VZ |
337 | 123, (long int)444444444, 555, -0.666); |
338 | #endif | |
7a828c7f VZ |
339 | |
340 | // test unicode/ansi conversion specifiers | |
bacd54f6 | 341 | // NB: this line will output two warnings like these, on GCC: |
10b7d13c MW |
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 | |
829d1102 VZ |
346 | |
347 | CMPTOSIZE(buffer, size, | |
10b7d13c MW |
348 | "unicode string: unicode!! W - ansi string: ansi!! w\n\n", |
349 | "unicode string: %ls %lc - ansi string: %hs %hc\n\n", | |
829d1102 | 350 | L"unicode!!", L'W', "ansi!!", 'w'); |
7a828c7f VZ |
351 | } |
352 | ||
353 | void VsnprintfTestCase::BigToSmallBuffer() | |
354 | { | |
355 | wxChar buf[1024], buf2[16], buf3[4], buf4; | |
356 | ||
357 | Misc(buf, 1024); | |
358 | Misc(buf2, 16); | |
359 | Misc(buf3, 4); | |
360 | Misc(&buf4, 1); | |
361 | } | |
b95d0984 MW |
362 | |
363 | static void DoScratch( | |
364 | int expectedLen, | |
365 | const wxString& expectedString, | |
366 | size_t max, | |
367 | const wxChar *format, ...) | |
368 | { | |
369 | const size_t BUFSIZE = 16; | |
370 | wxChar buf[BUFSIZE + 1]; | |
371 | size_t i; | |
372 | static int count = 0; | |
373 | ||
374 | wxASSERT(max <= BUFSIZE); | |
375 | ||
376 | for (i = 0; i < BUFSIZE; i++) | |
377 | buf[i] = '*'; | |
378 | buf[BUFSIZE] = 0; | |
379 | ||
380 | va_list ap; | |
381 | va_start(ap, format); | |
382 | ||
383 | int n = wxVsnprintf(buf, max, format, ap); | |
384 | ||
385 | va_end(ap); | |
386 | ||
387 | // Prepare messages so that it is possible to see from the error which | |
388 | // test was running. | |
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("'"); | |
394 | ||
395 | // turn them into std::strings | |
396 | std::string errMsg(errStr.mb_str()); | |
397 | std::string overflowMsg(overflowStr.mb_str()); | |
398 | ||
399 | CPPUNIT_ASSERT_MESSAGE(errMsg, | |
400 | (expectedLen == -1 && size_t(n) >= max) || expectedLen == n); | |
401 | ||
402 | CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf); | |
403 | ||
404 | for (i = max; i < BUFSIZE; i++) | |
405 | CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*'); | |
406 | } | |
407 | ||
408 | // Originally intended to test the final copy from the scratch, hence the | |
409 | // name, but turns up problems elsewhere too. | |
410 | // | |
411 | void VsnprintfTestCase::Scratch() | |
412 | { | |
413 | // expectedLen, expectedString, max, format, ... | |
414 | DoScratch(5, wxT("-1234"), 8, wxT("%d"), -1234); | |
415 | DoScratch(7, wxT("1234567"), 8, wxT("%d"), 1234567); | |
416 | DoScratch(-1, wxT("1234567"), 8, wxT("%d"), 12345678); | |
417 | DoScratch(-1, wxT("-123456"), 8, wxT("%d"), -1234567890); | |
418 | ||
419 | DoScratch(6, wxT("123456"), 8, wxT("123456")); | |
420 | DoScratch(7, wxT("1234567"), 8, wxT("1234567")); | |
421 | DoScratch(-1, wxT("1234567"), 8, wxT("12345678")); | |
422 | ||
423 | DoScratch(6, wxT("123450"), 8, wxT("12345%d"), 0); | |
424 | DoScratch(7, wxT("1234560"), 8, wxT("123456%d"), 0); | |
425 | DoScratch(-1, wxT("1234567"), 8, wxT("1234567%d"), 0); | |
426 | DoScratch(-1, wxT("1234567"), 8, wxT("12345678%d"), 0); | |
427 | ||
428 | DoScratch(6, wxT("12%45%"), 8, wxT("12%%45%%")); | |
429 | DoScratch(7, wxT("12%45%7"), 8, wxT("12%%45%%7")); | |
430 | DoScratch(-1, wxT("12%45%7"), 8, wxT("12%%45%%78")); | |
431 | } |