]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/vararg.cpp
Consistently handle DST start time in wxDateTime::Set().
[wxWidgets.git] / tests / strings / vararg.cpp
CommitLineData
c9f78968
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/strings/vararg.cpp
3// Purpose: Test for wx vararg look-alike macros
4// Author: Vaclav Slavik
5// Created: 2007-02-20
c9f78968
VS
6// Copyright: (c) 2007 REA Elektronik GmbH
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif // WX_PRECOMP
23
24#include "wx/string.h"
25
26// ----------------------------------------------------------------------------
27// test class
28// ----------------------------------------------------------------------------
29
30class VarArgTestCase : public CppUnit::TestCase
31{
32public:
33 VarArgTestCase() {}
34
35private:
36 CPPUNIT_TEST_SUITE( VarArgTestCase );
37 CPPUNIT_TEST( StringPrintf );
47346406 38 CPPUNIT_TEST( CharPrintf );
76fc401a
VS
39#if wxUSE_STD_STRING
40 CPPUNIT_TEST( StdString );
715efa4e
VZ
41#endif
42#if wxUSE_LONGLONG
43 CPPUNIT_TEST( LongLongPrintf );
76fc401a 44#endif
eb6cb207 45 CPPUNIT_TEST( Sscanf );
2f539d95 46 CPPUNIT_TEST( RepeatedPrintf );
ad6e5c47 47 CPPUNIT_TEST( ArgsValidation );
c9f78968
VS
48 CPPUNIT_TEST_SUITE_END();
49
50 void StringPrintf();
47346406 51 void CharPrintf();
76fc401a
VS
52#if wxUSE_STD_STRING
53 void StdString();
715efa4e
VZ
54#endif
55#if wxUSE_LONGLONG
56 void LongLongPrintf();
76fc401a 57#endif
eb6cb207 58 void Sscanf();
2f539d95 59 void RepeatedPrintf();
ad6e5c47 60 void ArgsValidation();
c9f78968
VS
61
62 DECLARE_NO_COPY_CLASS(VarArgTestCase)
63};
64
65// register in the unnamed registry so that these tests are run by default
66CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
67
e3778b4d 68// also include in its own registry so that these tests can be run alone
c9f78968
VS
69CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
70
71void VarArgTestCase::StringPrintf()
72{
73 wxString s, s2;
74
359bd4d1 75 // test passing literals:
c9f78968
VS
76 s.Printf("%s %i", "foo", 42);
77 CPPUNIT_ASSERT( s == "foo 42" );
9a83f860 78 s.Printf("%s %s %i", wxT("bar"), "=", 11);
359bd4d1
VS
79
80 // test passing c_str():
c9f78968
VS
81 CPPUNIT_ASSERT( s == "bar = 11" );
82 s2.Printf("(%s)", s.c_str());
83 CPPUNIT_ASSERT( s2 == "(bar = 11)" );
9a83f860 84 s2.Printf(wxT("[%s](%s)"), s.c_str(), "str");
c9f78968
VS
85 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
86
0dc8d224
VS
87 s2.Printf("%s mailbox", wxString("Opening").c_str());
88 CPPUNIT_ASSERT( s2 == "Opening mailbox" );
89
359bd4d1 90 // test passing wxString directly:
9a83f860 91 s2.Printf(wxT("[%s](%s)"), s, "str");
c9f78968 92 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
359bd4d1
VS
93
94 // test passing wxCharBufferType<T>:
95 s = "FooBar";
9a83f860 96 s2.Printf(wxT("(%s)"), s.mb_str());
359bd4d1 97 CPPUNIT_ASSERT( s2 == "(FooBar)" );
9a83f860 98 s2.Printf(wxT("value=%s;"), s.wc_str());
359bd4d1 99 CPPUNIT_ASSERT( s2 == "value=FooBar;" );
baf3d1dc
VS
100
101 // this tests correct passing of wxCStrData constructed from string
102 // literal:
103 bool cond = true;
9a83f860 104 s2.Printf(wxT("foo %s"), !cond ? s.c_str() : wxT("bar"));
47346406
VS
105}
106
107void VarArgTestCase::CharPrintf()
108{
109 wxString foo("foo");
110 wxString s;
111
112 // test using wchar_t:
113 s.Printf("char=%c", L'c');
1de532f5 114 CPPUNIT_ASSERT_EQUAL( "char=c", s );
47346406
VS
115
116 // test wxUniCharRef:
117 s.Printf("string[1] is %c", foo[1]);
1de532f5 118 CPPUNIT_ASSERT_EQUAL( "string[1] is o", s );
47346406
VS
119
120 // test char
121 char c = 'z';
122 s.Printf("%c to %c", 'a', c);
1de532f5 123 CPPUNIT_ASSERT_EQUAL( "a to z", s );
47346406
VS
124
125 // test char used as integer:
2452025c 126 #ifdef _MSC_VER
5098c258 127 #pragma warning(disable:4305) // truncation of constant value in VC6
2452025c
VZ
128 #pragma warning(disable:4309) // truncation of constant value
129 #endif
47346406 130 c = 240;
2452025c 131 #ifdef _MSC_VER
5098c258 132 #pragma warning(default:4305) // truncation of constant value in VC6
2452025c
VZ
133 #pragma warning(default:4309)
134 #endif
47346406 135 s.Printf("value is %i (int)", c);
1de532f5 136 CPPUNIT_ASSERT_EQUAL( wxString("value is -16 (int)"), s );
76fc401a 137
47346406
VS
138 unsigned char u = 240;
139 s.Printf("value is %i (int)", u);
1de532f5 140 CPPUNIT_ASSERT_EQUAL( "value is 240 (int)", s );
76fc401a
VS
141}
142
143#if wxUSE_STD_STRING
144void VarArgTestCase::StdString()
145{
146 // test passing std::[w]string
147 wxString s;
148
149 std::string mb("multi-byte");
150 std::string wc("widechar");
151
152 s.Printf("string %s(%i).", mb, 1);
1de532f5 153 CPPUNIT_ASSERT_EQUAL( "string multi-byte(1).", s );
76fc401a
VS
154
155 s.Printf("string %s(%i).", wc, 2);
1de532f5 156 CPPUNIT_ASSERT_EQUAL( "string widechar(2).", s );
c9f78968 157}
76fc401a 158#endif // wxUSE_STD_STRING
eb6cb207 159
715efa4e
VZ
160#if wxUSE_LONGLONG
161void VarArgTestCase::LongLongPrintf()
162{
163 const char * const llfmt = "%" wxLongLongFmtSpec "d";
164
165 CPPUNIT_ASSERT_EQUAL( "17", wxString::Format(llfmt, wxLL(17)) );
166
167 wxLongLong ll = 1234567890;
168 CPPUNIT_ASSERT_EQUAL( "1234567890", wxString::Format(llfmt, ll) );
169}
170#endif // wxUSE_LONGLONG
171
eb6cb207
VS
172void VarArgTestCase::Sscanf()
173{
174 int i = 0;
175 char str[20];
176 wchar_t wstr[20];
177
178 wxString input("42 test");
179
180 wxSscanf(input, "%d %s", &i, &str);
181 CPPUNIT_ASSERT( i == 42 );
182 CPPUNIT_ASSERT( wxStrcmp(str, "test") == 0 );
183
184 i = 0;
185 wxSscanf(input, L"%d %s", &i, &wstr);
186 CPPUNIT_ASSERT( i == 42 );
187 CPPUNIT_ASSERT( wxStrcmp(wstr, "test") == 0 );
188}
2f539d95
VS
189
190void VarArgTestCase::RepeatedPrintf()
191{
192 wxCharBuffer buffer(2);
193 char *p = buffer.data();
194 *p = 'h';
195 p++;
196 *p = 'i';
197
198 wxString s;
38914d5a 199 s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
2f539d95
VS
200 CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s);
201
38914d5a 202 s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
2f539d95
VS
203 CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s);
204}
ad6e5c47
VS
205
206void VarArgTestCase::ArgsValidation()
207{
208 void *ptr = this;
209 int written;
210 short int swritten;
211
212 // these are valid:
213 wxString::Format("a string(%s,%s), ptr %p, int %i",
214 wxString(), "foo", "char* as pointer", 1);
215
01307821
VZ
216 // Microsoft has helpfully disabled support for "%n" in their CRT by
217 // default starting from VC8 and somehow even calling
1cf9a80c
VZ
218 // _set_printf_count_output() doesn't help here, so don't use "%n" at all
219 // with it.
220#if wxCHECK_VISUALC_VERSION(8)
221 #define wxNO_PRINTF_PERCENT_N
222#endif // VC8+
223
224 // Similarly, many modern Linux distributions ship with g++ that uses
225 // -D_FORTIFY_SOURCE=2 flag by default and this option prevents "%n" from
226 // being used in a string outside of read-only memory, meaning that it
227 // can't be used in wxString to which we (may, depending on build options)
228 // assign it, so also disable testing of "%n" in this case lest we die with
229 // an abort inside vswprintf().
230#if defined(_FORTIFY_SOURCE)
231 #if _FORTIFY_SOURCE >= 2
232 #define wxNO_PRINTF_PERCENT_N
233 #endif
234#endif
235
236#ifndef wxNO_PRINTF_PERCENT_N
ad6e5c47
VS
237 wxString::Format("foo%i%n", 42, &written);
238 CPPUNIT_ASSERT_EQUAL( 5, written );
1cf9a80c 239#endif
ad6e5c47
VS
240
241 // but these are not:
242 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i: too many arguments", 42, 1, 2, 3) );
243 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i", "foo") );
244 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", (void*)this) );
245
32f6c515
VZ
246 // for some reason assert is not generated with VC6, don't know what's
247 // going there so disable it for now to make the test suite pass when using
248 // this compiler until someone has time to debug this (FIXME-VC6)
249#ifndef __VISUALC6__
ad6e5c47 250 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%d", ptr) );
32f6c515 251#endif
ad6e5c47 252
1cf9a80c
VZ
253 // we don't check wxNO_PRINTF_PERCENT_N here as these expressions should
254 // result in an assert in our code before the CRT functions are even called
ad6e5c47
VS
255 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%i%n", &written) );
256 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%n", ptr) );
257 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%i%n", 42, &swritten) );
258
71e33c75 259 // the following test (correctly) fails at compile-time with <type_traits>
cda93786
VZ
260#if !defined(HAVE_TYPE_TRAITS) && !defined(HAVE_TR1_TYPE_TRAITS)
261 wxObject obj;
262 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", obj) );
263
264 wxObject& ref = obj;
265 WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", ref) );
ad6e5c47 266#endif
3b12da35
VS
267
268 // %c should accept integers too
269 wxString::Format("%c", 80);
270 wxString::Format("%c", wxChar(80) + wxChar(1));
b113edcd
VS
271
272 // check size_t handling
273 size_t len = sizeof(*this);
bb5a9514 274#ifdef __WINDOWS__
b113edcd
VS
275 wxString::Format("%Iu", len);
276#else
277 wxString::Format("%zu", len);
278#endif
ad6e5c47 279}