]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
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)
9 // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
24 #include "wx/wxchar.h"
29 #define MAX_TEST_LEN 1024
33 static wxChar buf
[MAX_TEST_LEN
];
35 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
37 #define ASSERT_STR_EQUAL( a, b ) \
38 CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
40 #define CMP5(expected, x, y, z, w) \
41 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
43 ASSERT_STR_EQUAL( wxT(expected), buf );
45 #define CMP4(expected, x, y, z) \
46 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
48 ASSERT_STR_EQUAL( wxT(expected), buf );
50 #define CMP3(expected, x, y) \
51 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
53 ASSERT_STR_EQUAL( wxT(expected), buf );
55 #define CMP2(expected, x) \
56 wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
58 ASSERT_STR_EQUAL( wxT(expected), buf );
60 #define CMPTOSIZE(buffer, size, expected, fmt, x, y, z, w) \
61 wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
63 CPPUNIT_ASSERT_EQUAL( wxString(wxT(expected)).Left(size - 1), \
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class VsnprintfTestCase
: public CppUnit::TestCase
78 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
83 CPPUNIT_TEST( Asterisk
);
84 CPPUNIT_TEST( Percent
);
85 CPPUNIT_TEST( LongLong
);
87 CPPUNIT_TEST( BigToSmallBuffer
);
88 CPPUNIT_TEST_SUITE_END();
99 void BigToSmallBuffer();
100 void Misc(wxChar
*buffer
, int size
);
102 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
105 // register in the unnamed registry so that these tests are run by default
106 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
108 // also include in it's own registry so that these tests can be run alone
109 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
111 VsnprintfTestCase::VsnprintfTestCase()
115 void VsnprintfTestCase::E()
117 // NB: there are no standards about the minimum exponent width
118 // (and the width of the %e conversion specifier refers to the
119 // mantissa, not to the exponent).
120 // Since newer MSVC versions use 3 digits as minimum exponent
121 // width while GNU libc uses 2 digits as minimum width, here we
122 // workaround this problem using for the exponent values with at
123 // least three digits.
125 // printf("%e",2.342E+02);
126 // -> under MSVC7.1 prints: 2.342000e+002
127 // -> under GNU libc 2.4 prints: 2.342000e+02
128 CMP3("2.342000e+112", "%e",2.342E+112);
129 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
130 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
131 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
133 CMP3("-0.02342", "%G",-2.342E-02);
134 CMP3("3.1415E-116", "%G",3.1415e-116);
135 CMP3("0003.141500e+103", "%016e", 3141.5e100
);
136 CMP3(" 3.141500e+103", "%16e", 3141.5e100
);
137 CMP3("3.141500e+103 ", "%-16e", 3141.5e100
);
138 CMP3("3.142e+103", "%010.3e", 3141.5e100
);
141 void VsnprintfTestCase::F()
143 CMP3("3.300000", "%5f", 3.3);
144 CMP3("3.000000", "%5f", 3.0);
145 CMP3("0.000100", "%5f", .999999E-4);
146 CMP3("0.000990", "%5f", .99E-3);
147 CMP3("3333.000000", "%5f", 3333.0);
150 void VsnprintfTestCase::G()
152 // NOTE: the same about E() testcase applies here...
154 CMP3(" 3.3", "%5g", 3.3);
155 CMP3(" 3", "%5g", 3.0);
156 CMP3("9.99999e-115", "%5g", .999999E-114);
157 CMP3("0.00099", "%5g", .99E-3);
158 CMP3(" 3333", "%5g", 3333.0);
159 CMP3(" 0.01", "%5g", 0.01);
161 CMP3(" 3", "%5.g", 3.3);
162 CMP3(" 3", "%5.g", 3.0);
163 CMP3("1e-114", "%5.g", .999999E-114);
164 CMP3("0.0001", "%5.g", 1.0E-4);
165 CMP3("0.001", "%5.g", .99E-3);
166 CMP3("3e+103", "%5.g", 3333.0E100
);
167 CMP3(" 0.01", "%5.g", 0.01);
169 CMP3(" 3.3", "%5.2g", 3.3);
170 CMP3(" 3", "%5.2g", 3.0);
171 CMP3("1e-114", "%5.2g", .999999E-114);
172 CMP3("0.00099", "%5.2g", .99E-3);
173 CMP3("3.3e+103", "%5.2g", 3333.0E100
);
174 CMP3(" 0.01", "%5.2g", 0.01);
177 void VsnprintfTestCase::S()
179 CMP3(" abc", "%5s", wxT("abc"));
180 CMP3(" a", "%5s", wxT("a"));
181 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
182 CMP3("abc ", "%-5s", wxT("abc"));
183 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
185 CMP3("abcde", "%.5s", wxT("abcdefghi"));
187 // do the same tests but with Unicode characters:
189 #define ALPHA "\x3B1"
191 #define GAMMA "\x3B3"
192 #define DELTA "\x3B4"
193 #define EPSILON "\x3B5"
196 #define THETA "\x3B8"
199 #define ABC ALPHA BETA GAMMA
200 #define ABCDE ALPHA BETA GAMMA DELTA EPSILON
201 #define ABCDEFGHI ALPHA BETA GAMMA DELTA EPSILON ZETA ETA THETA IOTA
203 CMP3(" " ABC
, "%5s", wxT(ABC
));
204 CMP3(" " ALPHA
, "%5s", wxT(ALPHA
));
205 CMP3(ABCDEFGHI
, "%5s", wxT(ABCDEFGHI
));
206 CMP3(ABC
" ", "%-5s", wxT(ABC
));
207 CMP3(ABCDEFGHI
, "%-5s", wxT(ABCDEFGHI
));
209 CMP3(ABCDE
, "%.5s", wxT(ABCDEFGHI
));
213 void VsnprintfTestCase::Asterisk()
215 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
216 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
217 CMP5("0.1", "%*.*f", 3, 1, 0.123);
219 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
222 void VsnprintfTestCase::Percent()
224 // some tests without any argument passed through ...
226 CMP2("%%%", "%%%%%%");
228 CMP3("% abc", "%%%5s", wxT("abc"));
229 CMP3("% abc%", "%%%5s%%", wxT("abc"));
231 // do not test odd number of '%' symbols as different implementations
232 // of snprintf() give different outputs as this situation is not considered
233 // by any standard (in fact, GCC will also warn you about a spurious % if
234 // you write %%% as argument of some *printf function !)
235 // Compare(wxT("%"), wxT("%%%"));
238 void VsnprintfTestCase::LongLong()
240 CMP3("123456789", "%lld", (long long int)123456789);
241 CMP3("-123456789", "%lld", (long long int)-123456789);
243 CMP3("123456789", "%llu", (unsigned long long int)123456789);
246 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
248 // NB: remember that wx*printf could be mapped either to system
249 // implementation or to wx implementation.
250 // In the first case, when the output buffer is too small, the returned
251 // value can be the number of characters required for the output buffer
252 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
253 // just a negative number, usually -1; (this is how e.g. MSVC's
254 // *printf() behaves). Fortunately, in all implementations, when the
255 // output buffer is too small, it's nonetheless filled up to its max
258 // test without positionals
259 CMPTOSIZE(buffer
, size
, "123 444444444 - test - 555 -0.666",
260 "%i %li - test - %d %.3f",
261 123, (long int)444444444, 555, -0.666);
263 #if wxUSE_PRINTF_POS_PARAMS
264 // test with positional
265 CMPTOSIZE(buffer
, size
, "-0.666 123 - test - 444444444 555",
266 "%4$.3f %1$i - test - %2$li %3$d",
267 123, (long int)444444444, 555, -0.666);
270 // test unicode/ansi conversion specifiers
271 // NB: this line will output two warnings like these, on GCC:
272 // warning: use of 'h' length modifier with 's' type character (i.e.
273 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
274 // be ignored as here we explicitely want to test the wxSnprintf()
275 // behaviour in such case
277 CMPTOSIZE(buffer
, size
,
278 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
279 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
280 L
"unicode!!", L
'W', "ansi!!", 'w');
283 void VsnprintfTestCase::BigToSmallBuffer()
285 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;