]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/vararg.cpp
c65018c71ff72eef3130f44db3251f0d0eb20d53
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:4305) // truncation of constant value in VC6
119 #pragma warning(disable:4309) // truncation of constant value
123 #pragma warning(default:4305) // truncation of constant value in VC6
124 #pragma warning(default:4309)
126 s
.Printf("value is %i (int)", c
);
127 WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s
);
129 unsigned char u
= 240;
130 s
.Printf("value is %i (int)", u
);
131 WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s
);
135 void VarArgTestCase::StdString()
137 // test passing std::[w]string
140 std::string
mb("multi-byte");
141 std::string
wc("widechar");
143 s
.Printf("string %s(%i).", mb
, 1);
144 CPPUNIT_ASSERT( s
== "string multi-byte(1)." );
146 s
.Printf("string %s(%i).", wc
, 2);
147 CPPUNIT_ASSERT( s
== "string widechar(2)." );
149 #endif // wxUSE_STD_STRING
151 void VarArgTestCase::Sscanf()
157 wxString
input("42 test");
159 wxSscanf(input
, "%d %s", &i
, &str
);
160 CPPUNIT_ASSERT( i
== 42 );
161 CPPUNIT_ASSERT( wxStrcmp(str
, "test") == 0 );
164 wxSscanf(input
, L
"%d %s", &i
, &wstr
);
165 CPPUNIT_ASSERT( i
== 42 );
166 CPPUNIT_ASSERT( wxStrcmp(wstr
, "test") == 0 );