]>
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_SUITE_END();
53 DECLARE_NO_COPY_CLASS(VarArgTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase
);
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase
, "VarArgTestCase" );
62 void VarArgTestCase::StringPrintf()
66 // test passing literals:
67 s
.Printf("%s %i", "foo", 42);
68 CPPUNIT_ASSERT( s
== "foo 42" );
69 s
.Printf("%s %s %i", _T("bar"), "=", 11);
71 // test passing c_str():
72 CPPUNIT_ASSERT( s
== "bar = 11" );
73 s2
.Printf("(%s)", s
.c_str());
74 CPPUNIT_ASSERT( s2
== "(bar = 11)" );
75 s2
.Printf(_T("[%s](%s)"), s
.c_str(), "str");
76 CPPUNIT_ASSERT( s2
== "[bar = 11](str)" );
78 s2
.Printf("%s mailbox", wxString("Opening").c_str());
79 CPPUNIT_ASSERT( s2
== "Opening mailbox" );
81 // test passing wxString directly:
82 s2
.Printf(_T("[%s](%s)"), s
, "str");
83 CPPUNIT_ASSERT( s2
== "[bar = 11](str)" );
85 // test passing wxCharBufferType<T>:
87 s2
.Printf(_T("(%s)"), s
.mb_str());
88 CPPUNIT_ASSERT( s2
== "(FooBar)" );
89 s2
.Printf(_T("value=%s;"), s
.wc_str());
90 CPPUNIT_ASSERT( s2
== "value=FooBar;" );
92 // this tests correct passing of wxCStrData constructed from string
95 s2
.Printf(_T("foo %s"), !cond
? s
.c_str() : _T("bar"));
98 void VarArgTestCase::CharPrintf()
103 // test using wchar_t:
104 s
.Printf("char=%c", L
'c');
105 WX_ASSERT_STR_EQUAL( "char=c", s
);
107 // test wxUniCharRef:
108 s
.Printf("string[1] is %c", foo
[1]);
109 WX_ASSERT_STR_EQUAL( "string[1] is o", s
);
113 s
.Printf("%c to %c", 'a', c
);
114 WX_ASSERT_STR_EQUAL( "a to z", s
);
116 // test char used as integer:
118 #pragma warning(disable:4309) // truncation of constant value
122 #pragma warning(default:4309)
124 s
.Printf("value is %i (int)", c
);
125 WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s
);
127 unsigned char u
= 240;
128 s
.Printf("value is %i (int)", u
);
129 WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s
);
133 void VarArgTestCase::StdString()
135 // test passing std::[w]string
138 std::string
mb("multi-byte");
139 std::string
wc("widechar");
141 s
.Printf("string %s(%i).", mb
, 1);
142 CPPUNIT_ASSERT( s
== "string multi-byte(1)." );
144 s
.Printf("string %s(%i).", wc
, 2);
145 CPPUNIT_ASSERT( s
== "string widechar(2)." );
147 #endif // wxUSE_STD_STRING
149 void VarArgTestCase::Sscanf()
155 wxString
input("42 test");
157 wxSscanf(input
, "%d %s", &i
, &str
);
158 CPPUNIT_ASSERT( i
== 42 );
159 CPPUNIT_ASSERT( wxStrcmp(str
, "test") == 0 );
162 wxSscanf(input
, L
"%d %s", &i
, &wstr
);
163 CPPUNIT_ASSERT( i
== 42 );
164 CPPUNIT_ASSERT( wxStrcmp(wstr
, "test") == 0 );