]> git.saurik.com Git - wxWidgets.git/blob - tests/strings/vararg.cpp
Better fix
[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 CPPUNIT_TEST( CharPrintf );
40 #if wxUSE_STD_STRING
41 CPPUNIT_TEST( StdString );
42 #endif
43 CPPUNIT_TEST( Sscanf );
44 CPPUNIT_TEST_SUITE_END();
45
46 void StringPrintf();
47 void CharPrintf();
48 #if wxUSE_STD_STRING
49 void StdString();
50 #endif
51 void Sscanf();
52
53 DECLARE_NO_COPY_CLASS(VarArgTestCase)
54 };
55
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
58
59 // also include in it's own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
61
62 void VarArgTestCase::StringPrintf()
63 {
64 wxString s, s2;
65
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);
70
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)" );
77
78 s2.Printf("%s mailbox", wxString("Opening").c_str());
79 CPPUNIT_ASSERT( s2 == "Opening mailbox" );
80
81 // test passing wxString directly:
82 s2.Printf(_T("[%s](%s)"), s, "str");
83 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
84
85 // test passing wxCharBufferType<T>:
86 s = "FooBar";
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;" );
91
92 // this tests correct passing of wxCStrData constructed from string
93 // literal:
94 bool cond = true;
95 s2.Printf(_T("foo %s"), !cond ? s.c_str() : _T("bar"));
96 }
97
98 void VarArgTestCase::CharPrintf()
99 {
100 wxString foo("foo");
101 wxString s;
102
103 // test using wchar_t:
104 s.Printf("char=%c", L'c');
105 WX_ASSERT_STR_EQUAL( "char=c", s );
106
107 // test wxUniCharRef:
108 s.Printf("string[1] is %c", foo[1]);
109 WX_ASSERT_STR_EQUAL( "string[1] is o", s );
110
111 // test char
112 char c = 'z';
113 s.Printf("%c to %c", 'a', c);
114 WX_ASSERT_STR_EQUAL( "a to z", s );
115
116 // test char used as integer:
117 #ifdef _MSC_VER
118 #pragma warning(disable:4305) // truncation of constant value in VC6
119 #pragma warning(disable:4309) // truncation of constant value
120 #endif
121 c = 240;
122 #ifdef _MSC_VER
123 #pragma warning(default:4305) // truncation of constant value in VC6
124 #pragma warning(default:4309)
125 #endif
126 s.Printf("value is %i (int)", c);
127 WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s );
128
129 unsigned char u = 240;
130 s.Printf("value is %i (int)", u);
131 WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s );
132 }
133
134 #if wxUSE_STD_STRING
135 void VarArgTestCase::StdString()
136 {
137 // test passing std::[w]string
138 wxString s;
139
140 std::string mb("multi-byte");
141 std::string wc("widechar");
142
143 s.Printf("string %s(%i).", mb, 1);
144 CPPUNIT_ASSERT( s == "string multi-byte(1)." );
145
146 s.Printf("string %s(%i).", wc, 2);
147 CPPUNIT_ASSERT( s == "string widechar(2)." );
148 }
149 #endif // wxUSE_STD_STRING
150
151 void VarArgTestCase::Sscanf()
152 {
153 int i = 0;
154 char str[20];
155 wchar_t wstr[20];
156
157 wxString input("42 test");
158
159 wxSscanf(input, "%d %s", &i, &str);
160 CPPUNIT_ASSERT( i == 42 );
161 CPPUNIT_ASSERT( wxStrcmp(str, "test") == 0 );
162
163 i = 0;
164 wxSscanf(input, L"%d %s", &i, &wstr);
165 CPPUNIT_ASSERT( i == 42 );
166 CPPUNIT_ASSERT( wxStrcmp(wstr, "test") == 0 );
167 }