]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/vsnprintf.cpp
quote an underscore
[wxWidgets.git] / tests / strings / vsnprintf.cpp
CommitLineData
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
33static wxChar buf[MAX_TEST_LEN], buf2[MAX_TEST_LEN];
34
35
36// these macros makes it possible to write all tests without repeating a lot of times wxT() macro
37
38#define CMP5(expected, x, y, z, w) \
39 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z, w); \
40 snprintf(buf2, MAX_TEST_LEN, wxT(x), y, z, w); \
41 \
42 CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
43 CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
44
45#define CMP4(expected, x, y, z) \
46 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y, z); \
47 snprintf(buf2, MAX_TEST_LEN, wxT(x), y, z); \
48 \
49 CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
50 CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
51
52#define CMP3(expected, x, y) \
53 wxSnprintf(buf, MAX_TEST_LEN, wxT(x), y); \
54 snprintf(buf2, MAX_TEST_LEN, wxT(x), y); \
55 \
56 CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
57 CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
58
59#define CMP2(expected, x) \
60 wxSnprintf(buf, MAX_TEST_LEN, wxT(x)); \
61 snprintf(buf2, MAX_TEST_LEN, wxT(x)); \
62 \
63 CPPUNIT_ASSERT_STR_EQUAL( buf2, buf ); \
64 CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
65
66#define CMPTOSIZE(buffer, size, fmt, x, y, z, w) \
67 wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), x, y, z, w); \
68 snprintf(buf2, MAX_TEST_LEN, wxT(fmt), x, y, z, w); \
69 \
70 CPPUNIT_ASSERT_EQUAL( wxString(buf2, size), wxString(buf, size) );
71
7a828c7f
VZ
72
73// ----------------------------------------------------------------------------
74// test class
75// ----------------------------------------------------------------------------
76
77class VsnprintfTestCase : public CppUnit::TestCase
78{
79public:
80 VsnprintfTestCase();
81
82private:
83 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
84 CPPUNIT_TEST( E );
85 CPPUNIT_TEST( F );
86 CPPUNIT_TEST( G );
87 CPPUNIT_TEST( S );
829d1102 88 CPPUNIT_TEST( Asterisk );
7a828c7f
VZ
89
90 CPPUNIT_TEST( BigToSmallBuffer );
91 CPPUNIT_TEST_SUITE_END();
92
93 void E();
94 void F();
95 void G();
96 void S();
829d1102 97 void Asterisk();
7a828c7f
VZ
98
99 void BigToSmallBuffer();
7a828c7f 100 void Misc(wxChar *buffer, int size);
7a828c7f
VZ
101
102 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
103};
104
105// register in the unnamed registry so that these tests are run by default
106CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
107
108// also include in it's own registry so that these tests can be run alone
109CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
110
111VsnprintfTestCase::VsnprintfTestCase()
112{
113}
114
7a828c7f
VZ
115void VsnprintfTestCase::E()
116{
bacd54f6
VZ
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.
124 // Some examples:
125 // printf("%e",2.342E+02);
126 // -> under MSVC7.1 prints: 2.342000e+002
127 // -> under GNU libc 2.4 prints: 2.342000e+02
829d1102
VZ
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);
132
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);
7a828c7f
VZ
139}
140
141void VsnprintfTestCase::F()
142{
829d1102
VZ
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);
7a828c7f
VZ
148}
149
150void VsnprintfTestCase::G()
151{
bacd54f6
VZ
152 // NOTE: the same about E() testcase applies here...
153
829d1102
VZ
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);
160
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);
168
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);
7a828c7f
VZ
175}
176
177void VsnprintfTestCase::S()
178{
829d1102
VZ
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"));
184
185 CMP3("abcde", "%.5s", wxT("abcdefghi"));
186
187 // some tests without any argument passed through ...
188 CMP2(wxT("%"), wxT("%%"));
189 CMP2(wxT("%%%"), wxT("%%%%%%"));
190
191 // do not test odd number of '%' symbols as different implementations
192 // of snprintf() give different outputs as this situation is not considered
193 // by any standard (in fact, GCC will also warn you about a spurious % if
194 // you write %%% as argument of some *printf function !)
195 // Compare(wxT("%"), wxT("%%%"));
196}
197
198void VsnprintfTestCase::Asterisk()
199{
200 CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
201 CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
202 CMP5("0.1", "%*.*f", 3, 1, 0.123);
203
204 CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
7a828c7f
VZ
205}
206
207void VsnprintfTestCase::Misc(wxChar *buffer, int size)
208{
829d1102
VZ
209 // NB: remember that wx*printf could be mapped either to system implementation or to
210 // wx implementation.
211 // In the first case, when the output buffer is too small, the returned value can
212 // be the number of characters required for the output buffer (conforming to ISO C99;
213 // implemented in e.g. GNU libc >= 2.1), or just a negative number, usually -1;
214 // (this is how e.g. MSVC's *printf() behaves).
215 // Fortunately, in all implementations, when the output buffer is too small, it's
216 // nonetheless filled up to its max size.
7a828c7f
VZ
217
218 // test without positionals
829d1102
VZ
219 CMPTOSIZE(buffer, size,
220 wxT("%i %li - test - %d %.3f"),
221 123, (long int)444444444, 555, -0.666);
222
223#if wxUSE_PRINTF_POS_PARAMS
224 // test with positional
225 CMPTOSIZE(buffer, size,
226 wxT("%4$.3f %1$i - test - %2$li %3$d"),
227 123, (long int)444444444, 555, -0.666);
228#endif
7a828c7f
VZ
229
230 // test unicode/ansi conversion specifiers
bacd54f6
VZ
231 // NB: this line will output two warnings like these, on GCC:
232 // warning: use of ‘h’ length modifier with ‘s’ type character
233 // (i.e. GCC warns you that 'h' is not legal on 's' conv spec) but they must be ignored
234 // as here we explicitely want to test the wxSnprintf() behaviour in such case
829d1102
VZ
235
236 CMPTOSIZE(buffer, size,
237 wxT("unicode string: %ls %lc - ansi string: %hs %hc\n\n"),
238 L"unicode!!", L'W', "ansi!!", 'w');
7a828c7f
VZ
239}
240
241void VsnprintfTestCase::BigToSmallBuffer()
242{
243 wxChar buf[1024], buf2[16], buf3[4], buf4;
244
245 Misc(buf, 1024);
246 Misc(buf2, 16);
247 Misc(buf3, 4);
248 Misc(&buf4, 1);
249}