fixed wxScanf() etc. to compile with Visual C++ again
[wxWidgets.git] / tests / strings / vararg.cpp
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
31 class VarArgTestCase : public CppUnit::TestCase
32 {
33 public:
34 VarArgTestCase() {}
35
36 private:
37 CPPUNIT_TEST_SUITE( VarArgTestCase );
38 CPPUNIT_TEST( StringPrintf );
39 #if wxUSE_STD_STRING
40 CPPUNIT_TEST( StdString );
41 #endif
42 CPPUNIT_TEST( Sscanf );
43 CPPUNIT_TEST_SUITE_END();
44
45 void StringPrintf();
46 #if wxUSE_STD_STRING
47 void StdString();
48 #endif
49 void Sscanf();
50
51 DECLARE_NO_COPY_CLASS(VarArgTestCase)
52 };
53
54 // register in the unnamed registry so that these tests are run by default
55 CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
56
57 // also include in it's own registry so that these tests can be run alone
58 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
59
60 void VarArgTestCase::StringPrintf()
61 {
62 wxString s, s2;
63
64 // test passing literals:
65 s.Printf("%s %i", "foo", 42);
66 CPPUNIT_ASSERT( s == "foo 42" );
67 s.Printf("%s %s %i", _T("bar"), "=", 11);
68
69 // test passing c_str():
70 CPPUNIT_ASSERT( s == "bar = 11" );
71 s2.Printf("(%s)", s.c_str());
72 CPPUNIT_ASSERT( s2 == "(bar = 11)" );
73 s2.Printf(_T("[%s](%s)"), s.c_str(), "str");
74 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
75
76 s2.Printf("%s mailbox", wxString("Opening").c_str());
77 CPPUNIT_ASSERT( s2 == "Opening mailbox" );
78
79 // test passing wxString directly:
80 s2.Printf(_T("[%s](%s)"), s, "str");
81 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
82
83 // test passing wxCharBufferType<T>:
84 s = "FooBar";
85 s2.Printf(_T("(%s)"), s.mb_str());
86 CPPUNIT_ASSERT( s2 == "(FooBar)" );
87 s2.Printf(_T("value=%s;"), s.wc_str());
88 CPPUNIT_ASSERT( s2 == "value=FooBar;" );
89
90 // this tests correct passing of wxCStrData constructed from string
91 // literal:
92 bool cond = true;
93 s2.Printf(_T("foo %s"), !cond ? s.c_str() : _T("bar"));
94
95 }
96
97 #if wxUSE_STD_STRING
98 void VarArgTestCase::StdString()
99 {
100 // test passing std::[w]string
101 wxString s;
102
103 std::string mb("multi-byte");
104 std::string wc("widechar");
105
106 s.Printf("string %s(%i).", mb, 1);
107 CPPUNIT_ASSERT( s == "string multi-byte(1)." );
108
109 s.Printf("string %s(%i).", wc, 2);
110 CPPUNIT_ASSERT( s == "string widechar(2)." );
111 }
112 #endif // wxUSE_STD_STRING
113
114 void VarArgTestCase::Sscanf()
115 {
116 int i = 0;
117 char str[20];
118 wchar_t wstr[20];
119
120 wxString input("42 test");
121
122 wxSscanf(input, "%d %s", &i, &str);
123 CPPUNIT_ASSERT( i == 42 );
124 CPPUNIT_ASSERT( wxStrcmp(str, "test") == 0 );
125
126 i = 0;
127 wxSscanf(input, L"%d %s", &i, &wstr);
128 CPPUNIT_ASSERT( i == 42 );
129 CPPUNIT_ASSERT( wxStrcmp(wstr, "test") == 0 );
130 }