]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
Fixed crash in book pages.
[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 // if 1 then instead of the hard-coded expected strings, the obtained results will be
29 // compared to the output of the system's vsnprintf() implementation.
30 // NOTE: this requires a vsnprintf() implementation which supports positional parameters.
31 #define USE_LIBC 0
32
33 // this makes it possible to write all tests without repeating a lot of times wxT() macro
34 #define CMP(x, y, z) Compare(wxT(x), wxT(y), z)
35
36 // ----------------------------------------------------------------------------
37 // test class
38 // ----------------------------------------------------------------------------
39
40 class VsnprintfTestCase : public CppUnit::TestCase
41 {
42 public:
43 VsnprintfTestCase();
44
45 private:
46 CPPUNIT_TEST_SUITE( VsnprintfTestCase );
47 CPPUNIT_TEST( E );
48 CPPUNIT_TEST( F );
49 CPPUNIT_TEST( G );
50 CPPUNIT_TEST( S );
51
52 CPPUNIT_TEST( BigToSmallBuffer );
53 CPPUNIT_TEST_SUITE_END();
54
55 void E();
56 void F();
57 void G();
58 void S();
59
60 void BigToSmallBuffer();
61
62 // some helpers
63 void Misc(wxChar *buffer, int size);
64 void Compare(const wxChar *expected, const wxChar *format, ...) const;
65 void CompareV(wxChar *buf, wxChar *buf2, size_t len, const wxChar *format, va_list argptr) const;
66
67 DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
68 };
69
70 // register in the unnamed registry so that these tests are run by default
71 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
72
73 // also include in it's own registry so that these tests can be run alone
74 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
75
76 VsnprintfTestCase::VsnprintfTestCase()
77 {
78 }
79
80 void VsnprintfTestCase::CompareV(wxChar *buf, wxChar *buf2, size_t len,
81 const wxChar *format, va_list argptr) const
82 {
83 #if USE_LIBC
84 va_list argptr2;
85 wxVaCopy(argptr2, argptr);
86 #endif
87
88 wxVsnprintf( buf, len, format, argptr );
89 va_end(argptr);
90
91 #if USE_LIBC
92 vsnprintf( buf2, len, format, argptr2 ); // use system's implementation
93 va_end(argptr2);
94 #else
95 wxUnusedVar(buf2);
96 #endif
97 }
98
99 void VsnprintfTestCase::Compare(const wxChar *expected, const wxChar *format, ...) const
100 {
101 static wxChar buf[1024],
102 buf2[1024];
103
104 va_list argptr;
105 va_start( argptr, format );
106 CompareV( buf, buf2, 1024, format, argptr );
107 va_end(argptr);
108
109 #if USE_LIBC
110 CPPUNIT_ASSERT_STR_EQUAL( buf2, buf );
111 #else
112 CPPUNIT_ASSERT_STR_EQUAL( expected, buf );
113 #endif
114 }
115
116 void VsnprintfTestCase::E()
117 {
118 // NB: there are no standards about the minimum exponent width
119 // (and the width of the %e conversion specifier refers to the
120 // mantissa, not to the exponent).
121 // Since newer MSVC versions use 3 digits as minimum exponent
122 // width while GNU libc uses 2 digits as minimum width, here we
123 // workaround this problem using for the exponent values with at
124 // least three digits.
125 // Some examples:
126 // printf("%e",2.342E+02);
127 // -> under MSVC7.1 prints: 2.342000e+002
128 // -> under GNU libc 2.4 prints: 2.342000e+02
129 CMP("2.342000e+112", "%e",2.342E+112);
130 CMP("-2.3420e-112", "%10.4e",-2.342E-112);
131 CMP("-2.3420e-112", "%11.4e",-2.342E-112);
132 CMP(" -2.3420e-112", "%15.4e",-2.342E-112);
133
134 CMP("-0.02342", "%G",-2.342E-02);
135 CMP("3.1415E-116", "%G",3.1415e-116);
136 CMP("0003.141500e+103", "%016e", 3141.5e100);
137 CMP(" 3.141500e+103", "%16e", 3141.5e100);
138 CMP("3.141500e+103 ", "%-16e", 3141.5e100);
139 CMP("3.142e+103", "%010.3e", 3141.5e100);
140 }
141
142 void VsnprintfTestCase::F()
143 {
144 CMP("3.300000", "%5f", 3.3);
145 CMP("3.000000", "%5f", 3.0);
146 CMP("0.000100", "%5f", .999999E-4);
147 CMP("0.000990", "%5f", .99E-3);
148 CMP("3333.000000", "%5f", 3333.0);
149 }
150
151 void VsnprintfTestCase::G()
152 {
153 // NOTE: the same about E() testcase applies here...
154
155 CMP(" 3.3", "%5g", 3.3);
156 CMP(" 3", "%5g", 3.0);
157 CMP("9.99999e-115", "%5g", .999999E-114);
158 CMP("0.00099", "%5g", .99E-3);
159 CMP(" 3333", "%5g", 3333.0);
160 CMP(" 0.01", "%5g", 0.01);
161
162 CMP(" 3", "%5.g", 3.3);
163 CMP(" 3", "%5.g", 3.0);
164 CMP("1e-114", "%5.g", .999999E-114);
165 CMP("0.0001", "%5.g", 1.0E-4);
166 CMP("0.001", "%5.g", .99E-3);
167 CMP("3e+103", "%5.g", 3333.0E100);
168 CMP(" 0.01", "%5.g", 0.01);
169
170 CMP(" 3.3", "%5.2g", 3.3);
171 CMP(" 3", "%5.2g", 3.0);
172 CMP("1e-114", "%5.2g", .999999E-114);
173 CMP("0.00099", "%5.2g", .99E-3);
174 CMP("3.3e+103", "%5.2g", 3333.0E100);
175 CMP(" 0.01", "%5.2g", 0.01);
176 }
177
178 void VsnprintfTestCase::S()
179 {
180 CMP(" abc", "%5s", wxT("abc"));
181 CMP(" a", "%5s", wxT("a"));
182 CMP("abcdefghi", "%5s", wxT("abcdefghi"));
183 CMP("abc ", "%-5s", wxT("abc"));
184 CMP("abcdefghi", "%-5s", wxT("abcdefghi"));
185
186 CMP("abcde", "%.5s", wxT("abcdefghi"));
187
188 // some tests without any argument:
189 Compare(wxT("%"), wxT("%%"));
190 Compare(wxT("%%%"), wxT("%%%%%%"));
191 Compare(wxT("%%"), wxT("%%%"));
192 }
193
194 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
195 {
196 int ret;
197
198 // test without positionals
199 ret = wxSnprintf(buffer, size,
200 wxT("\n\n%s %e %le %i %li - test - %d %i %% -%*.*f-\n\n"), wxT("aa"), 123.123e100,
201 123123123123123123123123.123123123123e100, 456, (long int)33333333, 789, 999, 10, 1, 0.123);
202 if (ret >= 0)
203 {
204 CPPUNIT_ASSERT_STR_EQUAL(
205 wxT("\n\naa 1.231230e+102 1.231231e+123 456 33333333 - test - 789 999 % - 0.1-\n\n"),
206 buffer);
207 }
208
209 // test woth positional
210 ret = wxSnprintf(buffer, size,
211 wxT("\n\n%8$s %2$e %3$le %4$i %5$li - test - %6$d %7$i %% %1$.4f\n\n"), 0.123123123, 123.123e100,
212 123123123123123123123123.123123123123e100, 456, (long int)33333333, 789, 999, wxT("aa"));
213 if (ret >= 0)
214 {
215 CPPUNIT_ASSERT_STR_EQUAL(
216 wxT("\n\naa 1.231230e+102 1.231231e+123 456 33333333 - test - 789 999 % 0.1231\n\n"),
217 buffer);
218 }
219
220 // test unicode/ansi conversion specifiers
221 // NB: this line will output two warnings like these, on GCC:
222 // warning: use of ‘h’ length modifier with ‘s’ type character
223 // (i.e. GCC warns you that 'h' is not legal on 's' conv spec) but they must be ignored
224 // as here we explicitely want to test the wxSnprintf() behaviour in such case
225 ret = wxSnprintf(buffer, size,
226 wxT("\n\nunicode string: %ls %lc - ansi string: %hs %hc\n\n"), L"unicode!!", L'W', "ansi!!", 'w');
227 if (ret >= 0)
228 {
229 CPPUNIT_ASSERT_STR_EQUAL(
230 wxT("\n\nunicode string: unicode!! W - ansi string: ansi!! w\n\n"),
231 buffer);
232 }
233 }
234
235 void VsnprintfTestCase::BigToSmallBuffer()
236 {
237 wxChar buf[1024], buf2[16], buf3[4], buf4;
238
239 Misc(buf, 1024);
240 Misc(buf2, 16);
241 Misc(buf3, 4);
242 Misc(&buf4, 1);
243 }