]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vsnprintf.cpp
allow partial read/writes (patch 1476893)
[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( buf, buf2 );
111 #else
112 CPPUNIT_ASSERT_STR_EQUAL( buf, expected );
113 #endif
114 }
115
116 void VsnprintfTestCase::E()
117 {
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);
122
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);
129 }
130
131 void VsnprintfTestCase::F()
132 {
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);
138 }
139
140 void VsnprintfTestCase::G()
141 {
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);
148
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);
156
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);
163 }
164
165 void VsnprintfTestCase::S()
166 {
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"));
172
173 CMP("abcde", "%.5s", wxT("abcdefghi"));
174 }
175
176 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
177 {
178 int ret;
179
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);
184 if (ret >= 0)
185 {
186 CPPUNIT_ASSERT_STR_EQUAL(
187 wxT("\n\naa 1.231230e+02 1.231231e+23 456 33333333 - test - 789 999 %% - 0.1-\n\n"),
188 buffer);
189 }
190
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"));
195 if (ret >= 0)
196 {
197 CPPUNIT_ASSERT_STR_EQUAL(
198 wxT("\n\naa 1.231230e+02 1.231231e+23 456 33333333 - test - 789 999 %% 0.1231\n\n"),
199 buffer);
200 }
201
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');
205 if (ret >= 0)
206 {
207 CPPUNIT_ASSERT_STR_EQUAL(
208 wxT("\n\nunicode string: unicode!! W - ansi string: ansi!! w\n\n"),
209 buffer);
210 }
211 }
212
213 void VsnprintfTestCase::BigToSmallBuffer()
214 {
215 wxChar buf[1024], buf2[16], buf3[4], buf4;
216
217 Misc(buf, 1024);
218 Misc(buf2, 16);
219 Misc(buf3, 4);
220 Misc(&buf4, 1);
221 }