]>
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 VZ |
89 | CPPUNIT_TEST( Percent ); |
90 | CPPUNIT_TEST( LongLong ); | |
7a828c7f VZ |
91 | |
92 | CPPUNIT_TEST( BigToSmallBuffer ); | |
93 | CPPUNIT_TEST_SUITE_END(); | |
94 | ||
5921d2f8 VZ |
95 | void D(); |
96 | void X(); | |
97 | void O(); | |
98 | void P(); | |
99 | void N(); | |
7a828c7f VZ |
100 | void E(); |
101 | void F(); | |
102 | void G(); | |
103 | void S(); | |
829d1102 | 104 | void Asterisk(); |
412a5c57 VZ |
105 | void Percent(); |
106 | void LongLong(); | |
107 | void Unicode(); | |
7a828c7f VZ |
108 | |
109 | void BigToSmallBuffer(); | |
7a828c7f | 110 | void Misc(wxChar *buffer, int size); |
7a828c7f VZ |
111 | |
112 | DECLARE_NO_COPY_CLASS(VsnprintfTestCase) | |
113 | }; | |
114 | ||
115 | // register in the unnamed registry so that these tests are run by default | |
116 | CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase ); | |
117 | ||
118 | // also include in it's own registry so that these tests can be run alone | |
119 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" ); | |
120 | ||
121 | VsnprintfTestCase::VsnprintfTestCase() | |
122 | { | |
123 | } | |
124 | ||
5921d2f8 VZ |
125 | void VsnprintfTestCase::D() |
126 | { | |
127 | CMP3("+123456", "%+d", 123456); | |
128 | CMP3("-123456", "%d", -123456); | |
129 | CMP3(" 123456", "% d", 123456); | |
130 | CMP3(" 123456", "%10d", 123456); | |
131 | CMP3("0000123456", "%010d", 123456); | |
132 | CMP3("-123456 ", "%-10d", -123456); | |
133 | } | |
134 | ||
135 | void VsnprintfTestCase::X() | |
136 | { | |
137 | CMP3("ABCD", "%X", 0xABCD); | |
138 | CMP3("0XABCD", "%#X", 0xABCD); | |
139 | CMP3("0xabcd", "%#x", 0xABCD); | |
140 | } | |
141 | ||
142 | void VsnprintfTestCase::O() | |
143 | { | |
144 | CMP3("1234567", "%o", 01234567); | |
145 | CMP3("01234567", "%#o", 01234567); | |
146 | } | |
147 | ||
148 | void VsnprintfTestCase::P() | |
149 | { | |
150 | // WARNING: printing of pointers is not fully standard. | |
151 | // GNU prints them as %#x except for NULL pointers which are | |
152 | // printed as '(nil)'. | |
153 | // MSVC always print them as %8X on 32 bit systems and as %16X | |
154 | // on 64 bit systems | |
155 | #ifdef __VISUALC__ | |
156 | #if SIZEOF_VOID_P == 4 | |
157 | CMP3("00ABCDEF", "%p", (void*)0xABCDEF); | |
158 | CMP3("00000000", "%p", (void*)NULL); | |
159 | #elif SIZEOF_VOID_P == 8 | |
160 | CMP3("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF); | |
161 | CMP3("0000000000000000", "%p", (void*)NULL); | |
162 | #endif | |
163 | #elif defined(__GNUG__) | |
164 | CMP3("0xabcdef", "%p", (void*)0xABCDEF); | |
165 | CMP3("(nil)", "%p", (void*)NULL); | |
166 | #endif | |
167 | } | |
168 | ||
169 | void VsnprintfTestCase::N() | |
170 | { | |
171 | int nchar; | |
172 | ||
5677ce32 | 173 | wxSnprintf(buf, MAX_TEST_LEN, _T("%d %s%n\n"), 3, _T("bears"), &nchar); |
5921d2f8 VZ |
174 | CPPUNIT_ASSERT_EQUAL( 7, nchar ); |
175 | } | |
176 | ||
7a828c7f VZ |
177 | void VsnprintfTestCase::E() |
178 | { | |
bacd54f6 VZ |
179 | // NB: there are no standards about the minimum exponent width |
180 | // (and the width of the %e conversion specifier refers to the | |
181 | // mantissa, not to the exponent). | |
182 | // Since newer MSVC versions use 3 digits as minimum exponent | |
183 | // width while GNU libc uses 2 digits as minimum width, here we | |
184 | // workaround this problem using for the exponent values with at | |
185 | // least three digits. | |
186 | // Some examples: | |
187 | // printf("%e",2.342E+02); | |
188 | // -> under MSVC7.1 prints: 2.342000e+002 | |
189 | // -> under GNU libc 2.4 prints: 2.342000e+02 | |
829d1102 VZ |
190 | CMP3("2.342000e+112", "%e",2.342E+112); |
191 | CMP3("-2.3420e-112", "%10.4e",-2.342E-112); | |
192 | CMP3("-2.3420e-112", "%11.4e",-2.342E-112); | |
193 | CMP3(" -2.3420e-112", "%15.4e",-2.342E-112); | |
194 | ||
195 | CMP3("-0.02342", "%G",-2.342E-02); | |
196 | CMP3("3.1415E-116", "%G",3.1415e-116); | |
197 | CMP3("0003.141500e+103", "%016e", 3141.5e100); | |
198 | CMP3(" 3.141500e+103", "%16e", 3141.5e100); | |
199 | CMP3("3.141500e+103 ", "%-16e", 3141.5e100); | |
200 | CMP3("3.142e+103", "%010.3e", 3141.5e100); | |
7a828c7f VZ |
201 | } |
202 | ||
203 | void VsnprintfTestCase::F() | |
204 | { | |
829d1102 VZ |
205 | CMP3("3.300000", "%5f", 3.3); |
206 | CMP3("3.000000", "%5f", 3.0); | |
207 | CMP3("0.000100", "%5f", .999999E-4); | |
208 | CMP3("0.000990", "%5f", .99E-3); | |
209 | CMP3("3333.000000", "%5f", 3333.0); | |
7a828c7f VZ |
210 | } |
211 | ||
212 | void VsnprintfTestCase::G() | |
213 | { | |
bacd54f6 VZ |
214 | // NOTE: the same about E() testcase applies here... |
215 | ||
829d1102 VZ |
216 | CMP3(" 3.3", "%5g", 3.3); |
217 | CMP3(" 3", "%5g", 3.0); | |
218 | CMP3("9.99999e-115", "%5g", .999999E-114); | |
219 | CMP3("0.00099", "%5g", .99E-3); | |
220 | CMP3(" 3333", "%5g", 3333.0); | |
221 | CMP3(" 0.01", "%5g", 0.01); | |
222 | ||
223 | CMP3(" 3", "%5.g", 3.3); | |
224 | CMP3(" 3", "%5.g", 3.0); | |
225 | CMP3("1e-114", "%5.g", .999999E-114); | |
226 | CMP3("0.0001", "%5.g", 1.0E-4); | |
227 | CMP3("0.001", "%5.g", .99E-3); | |
228 | CMP3("3e+103", "%5.g", 3333.0E100); | |
229 | CMP3(" 0.01", "%5.g", 0.01); | |
230 | ||
231 | CMP3(" 3.3", "%5.2g", 3.3); | |
232 | CMP3(" 3", "%5.2g", 3.0); | |
233 | CMP3("1e-114", "%5.2g", .999999E-114); | |
234 | CMP3("0.00099", "%5.2g", .99E-3); | |
235 | CMP3("3.3e+103", "%5.2g", 3333.0E100); | |
236 | CMP3(" 0.01", "%5.2g", 0.01); | |
7a828c7f VZ |
237 | } |
238 | ||
239 | void VsnprintfTestCase::S() | |
240 | { | |
829d1102 VZ |
241 | CMP3(" abc", "%5s", wxT("abc")); |
242 | CMP3(" a", "%5s", wxT("a")); | |
243 | CMP3("abcdefghi", "%5s", wxT("abcdefghi")); | |
244 | CMP3("abc ", "%-5s", wxT("abc")); | |
245 | CMP3("abcdefghi", "%-5s", wxT("abcdefghi")); | |
246 | ||
247 | CMP3("abcde", "%.5s", wxT("abcdefghi")); | |
248 | ||
412a5c57 VZ |
249 | // do the same tests but with Unicode characters: |
250 | #if wxUSE_UNICODE | |
251 | #define ALPHA "\x3B1" | |
252 | #define BETA "\x3B2" | |
253 | #define GAMMA "\x3B3" | |
254 | #define DELTA "\x3B4" | |
255 | #define EPSILON "\x3B5" | |
256 | #define ZETA "\x3B6" | |
257 | #define ETA "\x3B7" | |
258 | #define THETA "\x3B8" | |
259 | #define IOTA "\x3B9" | |
260 | ||
261 | #define ABC ALPHA BETA GAMMA | |
262 | #define ABCDE ALPHA BETA GAMMA DELTA EPSILON | |
263 | #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA | |
264 | ||
265 | CMP3(" " ABC, "%5s", wxT(ABC)); | |
266 | CMP3(" " ALPHA, "%5s", wxT(ALPHA)); | |
267 | CMP3(ABCDEFGHI, "%5s", wxT(ABCDEFGHI)); | |
268 | CMP3(ABC " ", "%-5s", wxT(ABC)); | |
269 | CMP3(ABCDEFGHI, "%-5s", wxT(ABCDEFGHI)); | |
270 | ||
271 | CMP3(ABCDE, "%.5s", wxT(ABCDEFGHI)); | |
272 | #endif | |
273 | } | |
274 | ||
275 | void VsnprintfTestCase::Asterisk() | |
276 | { | |
277 | CMP5(" 0.1", "%*.*f", 10, 1, 0.123); | |
278 | CMP5(" 0.1230", "%*.*f", 10, 4, 0.123); | |
279 | CMP5("0.1", "%*.*f", 3, 1, 0.123); | |
280 | ||
281 | CMP4("%0.002", "%%%.*f", 3, 0.0023456789); | |
282 | } | |
283 | ||
284 | void VsnprintfTestCase::Percent() | |
285 | { | |
829d1102 | 286 | // some tests without any argument passed through ... |
10b7d13c MW |
287 | CMP2("%", "%%"); |
288 | CMP2("%%%", "%%%%%%"); | |
829d1102 | 289 | |
412a5c57 VZ |
290 | CMP3("% abc", "%%%5s", wxT("abc")); |
291 | CMP3("% abc%", "%%%5s%%", wxT("abc")); | |
292 | ||
829d1102 VZ |
293 | // do not test odd number of '%' symbols as different implementations |
294 | // of snprintf() give different outputs as this situation is not considered | |
295 | // by any standard (in fact, GCC will also warn you about a spurious % if | |
296 | // you write %%% as argument of some *printf function !) | |
297 | // Compare(wxT("%"), wxT("%%%")); | |
298 | } | |
299 | ||
412a5c57 | 300 | void VsnprintfTestCase::LongLong() |
829d1102 | 301 | { |
412a5c57 VZ |
302 | CMP3("123456789", "%lld", (long long int)123456789); |
303 | CMP3("-123456789", "%lld", (long long int)-123456789); | |
829d1102 | 304 | |
412a5c57 | 305 | CMP3("123456789", "%llu", (unsigned long long int)123456789); |
7a828c7f VZ |
306 | } |
307 | ||
308 | void VsnprintfTestCase::Misc(wxChar *buffer, int size) | |
309 | { | |
10b7d13c MW |
310 | // NB: remember that wx*printf could be mapped either to system |
311 | // implementation or to wx implementation. | |
312 | // In the first case, when the output buffer is too small, the returned | |
313 | // value can be the number of characters required for the output buffer | |
314 | // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or | |
315 | // just a negative number, usually -1; (this is how e.g. MSVC's | |
316 | // *printf() behaves). Fortunately, in all implementations, when the | |
317 | // output buffer is too small, it's nonetheless filled up to its max | |
318 | // size. | |
7a828c7f VZ |
319 | |
320 | // test without positionals | |
10b7d13c MW |
321 | CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666", |
322 | "%i %li - test - %d %.3f", | |
829d1102 VZ |
323 | 123, (long int)444444444, 555, -0.666); |
324 | ||
325 | #if wxUSE_PRINTF_POS_PARAMS | |
326 | // test with positional | |
10b7d13c MW |
327 | CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555", |
328 | "%4$.3f %1$i - test - %2$li %3$d", | |
829d1102 VZ |
329 | 123, (long int)444444444, 555, -0.666); |
330 | #endif | |
7a828c7f VZ |
331 | |
332 | // test unicode/ansi conversion specifiers | |
bacd54f6 | 333 | // NB: this line will output two warnings like these, on GCC: |
10b7d13c MW |
334 | // warning: use of 'h' length modifier with 's' type character (i.e. |
335 | // GCC warns you that 'h' is not legal on 's' conv spec) but they must | |
336 | // be ignored as here we explicitely want to test the wxSnprintf() | |
337 | // behaviour in such case | |
829d1102 VZ |
338 | |
339 | CMPTOSIZE(buffer, size, | |
10b7d13c MW |
340 | "unicode string: unicode!! W - ansi string: ansi!! w\n\n", |
341 | "unicode string: %ls %lc - ansi string: %hs %hc\n\n", | |
829d1102 | 342 | L"unicode!!", L'W', "ansi!!", 'w'); |
7a828c7f VZ |
343 | } |
344 | ||
345 | void VsnprintfTestCase::BigToSmallBuffer() | |
346 | { | |
347 | wxChar buf[1024], buf2[16], buf3[4], buf4; | |
348 | ||
349 | Misc(buf, 1024); | |
350 | Misc(buf2, 16); | |
351 | Misc(buf3, 4); | |
352 | Misc(&buf4, 1); | |
353 | } |