]>
Commit | Line | Data |
---|---|---|
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( RepeatedPrintf ); | |
45 | CPPUNIT_TEST_SUITE_END(); | |
46 | ||
47 | void StringPrintf(); | |
48 | void CharPrintf(); | |
49 | #if wxUSE_STD_STRING | |
50 | void StdString(); | |
51 | #endif | |
52 | void Sscanf(); | |
53 | void RepeatedPrintf(); | |
54 | ||
55 | DECLARE_NO_COPY_CLASS(VarArgTestCase) | |
56 | }; | |
57 | ||
58 | // register in the unnamed registry so that these tests are run by default | |
59 | CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase ); | |
60 | ||
61 | // also include in it's own registry so that these tests can be run alone | |
62 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" ); | |
63 | ||
64 | void VarArgTestCase::StringPrintf() | |
65 | { | |
66 | wxString s, s2; | |
67 | ||
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); | |
72 | ||
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)" ); | |
79 | ||
80 | s2.Printf("%s mailbox", wxString("Opening").c_str()); | |
81 | CPPUNIT_ASSERT( s2 == "Opening mailbox" ); | |
82 | ||
83 | // test passing wxString directly: | |
84 | s2.Printf(_T("[%s](%s)"), s, "str"); | |
85 | CPPUNIT_ASSERT( s2 == "[bar = 11](str)" ); | |
86 | ||
87 | // test passing wxCharBufferType<T>: | |
88 | s = "FooBar"; | |
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;" ); | |
93 | ||
94 | // this tests correct passing of wxCStrData constructed from string | |
95 | // literal: | |
96 | bool cond = true; | |
97 | s2.Printf(_T("foo %s"), !cond ? s.c_str() : _T("bar")); | |
98 | } | |
99 | ||
100 | void VarArgTestCase::CharPrintf() | |
101 | { | |
102 | wxString foo("foo"); | |
103 | wxString s; | |
104 | ||
105 | // test using wchar_t: | |
106 | s.Printf("char=%c", L'c'); | |
107 | CPPUNIT_ASSERT_EQUAL( "char=c", s ); | |
108 | ||
109 | // test wxUniCharRef: | |
110 | s.Printf("string[1] is %c", foo[1]); | |
111 | CPPUNIT_ASSERT_EQUAL( "string[1] is o", s ); | |
112 | ||
113 | // test char | |
114 | char c = 'z'; | |
115 | s.Printf("%c to %c", 'a', c); | |
116 | CPPUNIT_ASSERT_EQUAL( "a to z", s ); | |
117 | ||
118 | // test char used as integer: | |
119 | #ifdef _MSC_VER | |
120 | #pragma warning(disable:4305) // truncation of constant value in VC6 | |
121 | #pragma warning(disable:4309) // truncation of constant value | |
122 | #endif | |
123 | c = 240; | |
124 | #ifdef _MSC_VER | |
125 | #pragma warning(default:4305) // truncation of constant value in VC6 | |
126 | #pragma warning(default:4309) | |
127 | #endif | |
128 | s.Printf("value is %i (int)", c); | |
129 | CPPUNIT_ASSERT_EQUAL( wxString("value is -16 (int)"), s ); | |
130 | ||
131 | unsigned char u = 240; | |
132 | s.Printf("value is %i (int)", u); | |
133 | CPPUNIT_ASSERT_EQUAL( "value is 240 (int)", s ); | |
134 | } | |
135 | ||
136 | #if wxUSE_STD_STRING | |
137 | void VarArgTestCase::StdString() | |
138 | { | |
139 | // test passing std::[w]string | |
140 | wxString s; | |
141 | ||
142 | std::string mb("multi-byte"); | |
143 | std::string wc("widechar"); | |
144 | ||
145 | s.Printf("string %s(%i).", mb, 1); | |
146 | CPPUNIT_ASSERT_EQUAL( "string multi-byte(1).", s ); | |
147 | ||
148 | s.Printf("string %s(%i).", wc, 2); | |
149 | CPPUNIT_ASSERT_EQUAL( "string widechar(2).", s ); | |
150 | } | |
151 | #endif // wxUSE_STD_STRING | |
152 | ||
153 | void VarArgTestCase::Sscanf() | |
154 | { | |
155 | int i = 0; | |
156 | char str[20]; | |
157 | wchar_t wstr[20]; | |
158 | ||
159 | wxString input("42 test"); | |
160 | ||
161 | wxSscanf(input, "%d %s", &i, &str); | |
162 | CPPUNIT_ASSERT( i == 42 ); | |
163 | CPPUNIT_ASSERT( wxStrcmp(str, "test") == 0 ); | |
164 | ||
165 | i = 0; | |
166 | wxSscanf(input, L"%d %s", &i, &wstr); | |
167 | CPPUNIT_ASSERT( i == 42 ); | |
168 | CPPUNIT_ASSERT( wxStrcmp(wstr, "test") == 0 ); | |
169 | } | |
170 | ||
171 | void VarArgTestCase::RepeatedPrintf() | |
172 | { | |
173 | wxCharBuffer buffer(2); | |
174 | char *p = buffer.data(); | |
175 | *p = 'h'; | |
176 | p++; | |
177 | *p = 'i'; | |
178 | ||
179 | wxString s; | |
180 | s = wxString::Format("buffer %s, len %d", buffer, wxStrlen(buffer)); | |
181 | CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s); | |
182 | ||
183 | s = wxString::Format("buffer %s, len %d", buffer, wxStrlen(buffer)); | |
184 | CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s); | |
185 | } |