]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/vararg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/vararg.cpp
3 // Purpose: Test for wx vararg look-alike macros
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
25 #include "wx/string.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class VarArgTestCase
: public CppUnit::TestCase
37 CPPUNIT_TEST_SUITE( VarArgTestCase
);
38 CPPUNIT_TEST( StringPrintf
);
39 CPPUNIT_TEST( CharPrintf
);
41 CPPUNIT_TEST( StdString
);
43 CPPUNIT_TEST( Sscanf
);
44 CPPUNIT_TEST( RepeatedPrintf
);
45 CPPUNIT_TEST_SUITE_END();
53 void RepeatedPrintf();
55 DECLARE_NO_COPY_CLASS(VarArgTestCase
)
58 // register in the unnamed registry so that these tests are run by default
59 CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase
);
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase
, "VarArgTestCase" );
64 void VarArgTestCase::StringPrintf()
68 // test passing literals:
69 s
.Printf("%s %i", "foo", 42);
70 CPPUNIT_ASSERT( s
== "foo 42" );
71 s
.Printf("%s %s %i", _T("bar"), "=", 11);
73 // test passing c_str():
74 CPPUNIT_ASSERT( s
== "bar = 11" );
75 s2
.Printf("(%s)", s
.c_str());
76 CPPUNIT_ASSERT( s2
== "(bar = 11)" );
77 s2
.Printf(_T("[%s](%s)"), s
.c_str(), "str");
78 CPPUNIT_ASSERT( s2
== "[bar = 11](str)" );
80 s2
.Printf("%s mailbox", wxString("Opening").c_str());
81 CPPUNIT_ASSERT( s2
== "Opening mailbox" );
83 // test passing wxString directly:
84 s2
.Printf(_T("[%s](%s)"), s
, "str");
85 CPPUNIT_ASSERT( s2
== "[bar = 11](str)" );
87 // test passing wxCharBufferType<T>:
89 s2
.Printf(_T("(%s)"), s
.mb_str());
90 CPPUNIT_ASSERT( s2
== "(FooBar)" );
91 s2
.Printf(_T("value=%s;"), s
.wc_str());
92 CPPUNIT_ASSERT( s2
== "value=FooBar;" );
94 // this tests correct passing of wxCStrData constructed from string
97 s2
.Printf(_T("foo %s"), !cond
? s
.c_str() : _T("bar"));
100 void VarArgTestCase::CharPrintf()
105 // test using wchar_t:
106 s
.Printf("char=%c", L
'c');
107 CPPUNIT_ASSERT_EQUAL( "char=c", s
);
109 // test wxUniCharRef:
110 s
.Printf("string[1] is %c", foo
[1]);
111 CPPUNIT_ASSERT_EQUAL( "string[1] is o", s
);
115 s
.Printf("%c to %c", 'a', c
);
116 CPPUNIT_ASSERT_EQUAL( "a to z", s
);
118 // test char used as integer:
120 #pragma warning(disable:4305) // truncation of constant value in VC6
121 #pragma warning(disable:4309) // truncation of constant value
125 #pragma warning(default:4305) // truncation of constant value in VC6
126 #pragma warning(default:4309)
128 s
.Printf("value is %i (int)", c
);
129 CPPUNIT_ASSERT_EQUAL( wxString("value is -16 (int)"), s
);
131 unsigned char u
= 240;
132 s
.Printf("value is %i (int)", u
);
133 CPPUNIT_ASSERT_EQUAL( "value is 240 (int)", s
);
137 void VarArgTestCase::StdString()
139 // test passing std::[w]string
142 std::string
mb("multi-byte");
143 std::string
wc("widechar");
145 s
.Printf("string %s(%i).", mb
, 1);
146 CPPUNIT_ASSERT_EQUAL( "string multi-byte(1).", s
);
148 s
.Printf("string %s(%i).", wc
, 2);
149 CPPUNIT_ASSERT_EQUAL( "string widechar(2).", s
);
151 #endif // wxUSE_STD_STRING
153 void VarArgTestCase::Sscanf()
159 wxString
input("42 test");
161 wxSscanf(input
, "%d %s", &i
, &str
);
162 CPPUNIT_ASSERT( i
== 42 );
163 CPPUNIT_ASSERT( wxStrcmp(str
, "test") == 0 );
166 wxSscanf(input
, L
"%d %s", &i
, &wstr
);
167 CPPUNIT_ASSERT( i
== 42 );
168 CPPUNIT_ASSERT( wxStrcmp(wstr
, "test") == 0 );
171 void VarArgTestCase::RepeatedPrintf()
173 wxCharBuffer
buffer(2);
174 char *p
= buffer
.data();
180 s
= wxString::Format("buffer %s, len %d", buffer
, wxStrlen(buffer
));
181 CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s
);
183 s
= wxString::Format("buffer %s, len %d", buffer
, wxStrlen(buffer
));
184 CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s
);