Compile fixes.
[wxWidgets.git] / 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)
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
28
29 #define MAX_TEST_LEN 1024
30
31
32 // temporary buffers
33 static wxChar buf[MAX_TEST_LEN];
34
35 // these macros makes it possible to write all tests without repeating a lot of times wxT() macro
36
37 #define ASSERT_STR_EQUAL( a, b ) \
38 CPPUNIT_ASSERT( wxString(a) == wxString(b) );
39
40 #define CMP5(expected, x, y, z, w) \
41 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
42 \
43 ASSERT_STR_EQUAL( wxT(expected), buf );
44
45 #define CMP4(expected, x, y, z) \
46 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
47 \
48 ASSERT_STR_EQUAL( wxT(expected), buf );
49
50 #define CMP3(expected, x, y) \
51 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
52 \
53 ASSERT_STR_EQUAL( wxT(expected), buf );
54
55 #define CMP2(expected, x) \
56 wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
57 \
58 ASSERT_STR_EQUAL( wxT(expected), buf );
59
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( wxString(wxT(expected)).Left(size - 1) == buffer )
64
65
66
67 // ----------------------------------------------------------------------------
68 // test class
69 // ----------------------------------------------------------------------------
70
71 class VsnprintfTestCase : public CppUnit::TestCase
72 {
73 public:
74 VsnprintfTestCase();
75
76 private:
77 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
78 CPPUNIT_TEST( E );
79 CPPUNIT_TEST( F );
80 CPPUNIT_TEST( G );
81 CPPUNIT_TEST( S );
82 CPPUNIT_TEST( Asterisk );
83
84 CPPUNIT_TEST( BigToSmallBuffer );
85 CPPUNIT_TEST_SUITE_END();
86
87 void E();
88 void F();
89 void G();
90 void S();
91 void Asterisk();
92
93 void BigToSmallBuffer();
94 void Misc(wxChar *buffer, int size);
95
96 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
97 };
98
99 // register in the unnamed registry so that these tests are run by default
100 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
101
102 // also include in it's own registry so that these tests can be run alone
103 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
104
105 VsnprintfTestCase::VsnprintfTestCase()
106 {
107 }
108
109 void VsnprintfTestCase::E()
110 {
111 // NB: there are no standards about the minimum exponent width
112 // (and the width of the %e conversion specifier refers to the
113 // mantissa, not to the exponent).
114 // Since newer MSVC versions use 3 digits as minimum exponent
115 // width while GNU libc uses 2 digits as minimum width, here we
116 // workaround this problem using for the exponent values with at
117 // least three digits.
118 // Some examples:
119 // printf("%e",2.342E+02);
120 // -> under MSVC7.1 prints: 2.342000e+002
121 // -> under GNU libc 2.4 prints: 2.342000e+02
122 CMP3("2.342000e+112", "%e",2.342E+112);
123 CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
124 CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
125 CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
126
127 CMP3("-0.02342", "%G",-2.342E-02);
128 CMP3("3.1415E-116", "%G",3.1415e-116);
129 CMP3("0003.141500e+103", "%016e", 3141.5e100);
130 CMP3(" 3.141500e+103", "%16e", 3141.5e100);
131 CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
132 CMP3("3.142e+103", "%010.3e", 3141.5e100);
133 }
134
135 void VsnprintfTestCase::F()
136 {
137 CMP3("3.300000", "%5f", 3.3);
138 CMP3("3.000000", "%5f", 3.0);
139 CMP3("0.000100", "%5f", .999999E-4);
140 CMP3("0.000990", "%5f", .99E-3);
141 CMP3("3333.000000", "%5f", 3333.0);
142 }
143
144 void VsnprintfTestCase::G()
145 {
146 // NOTE: the same about E() testcase applies here...
147
148 CMP3(" 3.3", "%5g", 3.3);
149 CMP3(" 3", "%5g", 3.0);
150 CMP3("9.99999e-115", "%5g", .999999E-114);
151 CMP3("0.00099", "%5g", .99E-3);
152 CMP3(" 3333", "%5g", 3333.0);
153 CMP3(" 0.01", "%5g", 0.01);
154
155 CMP3(" 3", "%5.g", 3.3);
156 CMP3(" 3", "%5.g", 3.0);
157 CMP3("1e-114", "%5.g", .999999E-114);
158 CMP3("0.0001", "%5.g", 1.0E-4);
159 CMP3("0.001", "%5.g", .99E-3);
160 CMP3("3e+103", "%5.g", 3333.0E100);
161 CMP3(" 0.01", "%5.g", 0.01);
162
163 CMP3(" 3.3", "%5.2g", 3.3);
164 CMP3(" 3", "%5.2g", 3.0);
165 CMP3("1e-114", "%5.2g", .999999E-114);
166 CMP3("0.00099", "%5.2g", .99E-3);
167 CMP3("3.3e+103", "%5.2g", 3333.0E100);
168 CMP3(" 0.01", "%5.2g", 0.01);
169 }
170
171 void VsnprintfTestCase::S()
172 {
173 CMP3(" abc", "%5s", wxT("abc"));
174 CMP3(" a", "%5s", wxT("a"));
175 CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
176 CMP3("abc ", "%-5s", wxT("abc"));
177 CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
178
179 CMP3("abcde", "%.5s", wxT("abcdefghi"));
180
181 // some tests without any argument passed through ...
182 CMP2("%", "%%");
183 CMP2("%%%", "%%%%%%");
184
185 // do not test odd number of '%' symbols as different implementations
186 // of snprintf() give different outputs as this situation is not considered
187 // by any standard (in fact, GCC will also warn you about a spurious % if
188 // you write %%% as argument of some *printf function !)
189 // Compare(wxT("%"), wxT("%%%"));
190 }
191
192 void VsnprintfTestCase::Asterisk()
193 {
194 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
195 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
196 CMP5("0.1", "%*.*f", 3, 1, 0.123);
197
198 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
199 }
200
201 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
202 {
203 // NB: remember that wx*printf could be mapped either to system
204 // implementation or to wx implementation.
205 // In the first case, when the output buffer is too small, the returned
206 // value can be the number of characters required for the output buffer
207 // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
208 // just a negative number, usually -1; (this is how e.g. MSVC's
209 // *printf() behaves). Fortunately, in all implementations, when the
210 // output buffer is too small, it's nonetheless filled up to its max
211 // size.
212
213 // test without positionals
214 CMPTOSIZE(buffer, size, "123 444444444 - test - 555 -0.666",
215 "%i %li - test - %d %.3f",
216 123, (long int)444444444, 555, -0.666);
217
218 #if wxUSE_PRINTF_POS_PARAMS
219 // test with positional
220 CMPTOSIZE(buffer, size, "-0.666 123 - test - 444444444 555",
221 "%4$.3f %1$i - test - %2$li %3$d",
222 123, (long int)444444444, 555, -0.666);
223 #endif
224
225 // test unicode/ansi conversion specifiers
226 // NB: this line will output two warnings like these, on GCC:
227 // warning: use of 'h' length modifier with 's' type character (i.e.
228 // GCC warns you that 'h' is not legal on 's' conv spec) but they must
229 // be ignored as here we explicitely want to test the wxSnprintf()
230 // behaviour in such case
231
232 CMPTOSIZE(buffer, size,
233 "unicode string: unicode!! W - ansi string: ansi!! w\n\n",
234 "unicode string: %ls %lc - ansi string: %hs %hc\n\n",
235 L"unicode!!", L'W', "ansi!!", 'w');
236 }
237
238 void VsnprintfTestCase::BigToSmallBuffer()
239 {
240 wxChar buf[1024], buf2[16], buf3[4], buf4;
241
242 Misc(buf, 1024);
243 Misc(buf2, 16);
244 Misc(buf3, 4);
245 Misc(&buf4, 1);
246 }