]>
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( buf
, buf2
);
112 CPPUNIT_ASSERT_STR_EQUAL( buf
, expected
);
116 void VsnprintfTestCase::E()
118 CMP("2.342000e+02", "%e",2.342E+02);
119 CMP("-2.3420e-02", "%10.4e",-2.342E-02);
120 CMP("-2.3420e-02", "%11.4e",-2.342E-02);
121 CMP(" -2.3420e-02", "%15.4e",-2.342E-02);
123 CMP("-0.02342", "%G",-2.342E-02);
124 CMP("3.1415E-06", "%G",3.1415e-6);
125 CMP("00003.141500e+03", "%016e", 3141.5);
126 CMP(" 3.141500e+03", "%16e", 3141.5);
127 CMP("3.141500e+03 ", "%-16e", 3141.5);
128 CMP("03.142e+03", "%010.3e", 3141.5);
131 void VsnprintfTestCase::F()
133 CMP("3.300000", "%5f", 3.3);
134 CMP("3.000000", "%5f", 3.0);
135 CMP("0.000100", "%5f", .999999E-4);
136 CMP("0.000990", "%5f", .99E-3);
137 CMP("3333.000000", "%5f", 3333.0);
140 void VsnprintfTestCase::G()
142 CMP(" 3.3", "%5g", 3.3);
143 CMP(" 3", "%5g", 3.0);
144 CMP("9.99999e-05", "%5g", .999999E-4);
145 CMP("0.00099", "%5g", .99E-3);
146 CMP(" 3333", "%5g", 3333.0);
147 CMP(" 0.01", "%5g", 0.01);
149 CMP(" 3", "%5.g", 3.3);
150 CMP(" 3", "%5.g", 3.0);
151 CMP("1e-04", "%5.g", .999999E-4);
152 CMP("0.0001", "%5.g", 1.0E-4);
153 CMP("0.001", "%5.g", .99E-3);
154 CMP("3e+03", "%5.g", 3333.0);
155 CMP(" 0.01", "%5.g", 0.01);
157 CMP(" 3.3", "%5.2g", 3.3);
158 CMP(" 3", "%5.2g", 3.0);
159 CMP("1e-04", "%5.2g", .999999E-4);
160 CMP("0.00099", "%5.2g", .99E-3);
161 CMP("3.3e+03", "%5.2g", 3333.0);
162 CMP(" 0.01", "%5.2g", 0.01);
165 void VsnprintfTestCase::S()
167 CMP(" abc", "%5s", wxT("abc"));
168 CMP(" a", "%5s", wxT("a"));
169 CMP("abcdefghi", "%5s", wxT("abcdefghi"));
170 CMP("abc ", "%-5s", wxT("abc"));
171 CMP("abcdefghi", "%-5s", wxT("abcdefghi"));
173 CMP("abcde", "%.5s", wxT("abcdefghi"));
176 void VsnprintfTestCase::Misc(wxChar
*buffer
, int size
)
180 // test without positionals
181 ret
= wxSnprintf(buffer
, size
,
182 wxT("\n\n%s %e %le %i %li - test - %d %i %% -%*.*f-\n\n"), wxT("aa"), 123.123,
183 123123123123123123123123.123123123123, 456, (long int)33333333, 789, 999, 10, 1, 0.123);
186 CPPUNIT_ASSERT_STR_EQUAL(
187 wxT("\n\naa 1.231230e+02 1.231231e+23 456 33333333 - test - 789 999 %% - 0.1-\n\n"),
191 // test woth positional
192 ret
= wxSnprintf(buffer
, size
,
193 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.123,
194 123123123123123123123123.123123123123, 456, (long int)33333333, 789, 999, wxT("aa"));
197 CPPUNIT_ASSERT_STR_EQUAL(
198 wxT("\n\naa 1.231230e+02 1.231231e+23 456 33333333 - test - 789 999 %% 0.1231\n\n"),
202 // test unicode/ansi conversion specifiers
203 ret
= wxSnprintf(buffer
, size
,
204 wxT("\n\nunicode string: %ls %lc - ansi string: %hs %hc\n\n"), L
"unicode!!", L
'W', "ansi!!", 'w');
207 CPPUNIT_ASSERT_STR_EQUAL(
208 wxT("\n\nunicode string: unicode!! W - ansi string: ansi!! w\n\n"),
213 void VsnprintfTestCase::BigToSmallBuffer()
215 wxChar buf
[1024], buf2
[16], buf3
[4], buf4
;