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