]>
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"
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.
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)
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class VsnprintfTestCase
: public CppUnit::TestCase
46 CPPUNIT_TEST_SUITE( VsnprintfTestCase
);
52 CPPUNIT_TEST( BigToSmallBuffer
);
53 CPPUNIT_TEST_SUITE_END();
60 void BigToSmallBuffer();
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;
67 DECLARE_NO_COPY_CLASS(VsnprintfTestCase
)
70 // register in the unnamed registry so that these tests are run by default
71 CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase
);
73 // also include in it's own registry so that these tests can be run alone
74 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase
, "VsnprintfTestCase" );
76 VsnprintfTestCase::VsnprintfTestCase()
80 void VsnprintfTestCase::CompareV(wxChar
*buf
, wxChar
*buf2
, size_t len
,
81 const wxChar
*format
, va_list argptr
) const
85 wxVaCopy(argptr2
, argptr
);
88 wxVsnprintf( buf
, len
, format
, argptr
);
92 vsnprintf( buf2
, len
, format
, argptr2
); // use system's implementation
99 void VsnprintfTestCase::Compare(const wxChar
*expected
, const wxChar
*format
, ...) const
101 static wxChar buf
[1024],
105 va_start( argptr
, format
);
106 CompareV( buf
, buf2
, 1024, format
, argptr
);
110 CPPUNIT_ASSERT_STR_EQUAL( buf2
, buf
);
112 CPPUNIT_ASSERT_STR_EQUAL( expected
, buf
);
116 void VsnprintfTestCase::E()
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.
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);
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
);
142 void VsnprintfTestCase::F()
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);
151 void VsnprintfTestCase::G()
153 // NOTE: the same about E() testcase applies here...
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);
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);
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);
178 void VsnprintfTestCase::S()
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"));
186 CMP("abcde", "%.5s", wxT("abcdefghi"));
188 // some tests without any argument:
189 Compare(wxT("%"), wxT("%%"));
190 Compare(wxT("%%%"), wxT("%%%%%%"));
191 Compare(wxT("%%"), wxT("%%%"));
194 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
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);
204 CPPUNIT_ASSERT_STR_EQUAL(
205 wxT("\n\naa 1.231230e+102 1.231231e+123 456 33333333 - test - 789 999 % - 0.1-\n\n"),
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"));
215 CPPUNIT_ASSERT_STR_EQUAL(
216 wxT("\n\naa 1.231230e+102 1.231231e+123 456 33333333 - test - 789 999 % 0.1231\n\n"),
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');
229 CPPUNIT_ASSERT_STR_EQUAL(
230 wxT("\n\nunicode string: unicode!! W - ansi string: ansi!! w\n\n"),
235 void VsnprintfTestCase::BigToSmallBuffer()
237 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;