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