]>
Commit | Line | Data |
---|---|---|
c9f78968 VS |
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 ); | |
47346406 | 39 | CPPUNIT_TEST( CharPrintf ); |
76fc401a VS |
40 | #if wxUSE_STD_STRING |
41 | CPPUNIT_TEST( StdString ); | |
42 | #endif | |
eb6cb207 | 43 | CPPUNIT_TEST( Sscanf ); |
c9f78968 VS |
44 | CPPUNIT_TEST_SUITE_END(); |
45 | ||
46 | void StringPrintf(); | |
47346406 | 47 | void CharPrintf(); |
76fc401a VS |
48 | #if wxUSE_STD_STRING |
49 | void StdString(); | |
50 | #endif | |
eb6cb207 | 51 | void Sscanf(); |
c9f78968 VS |
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 | ||
359bd4d1 | 66 | // test passing literals: |
c9f78968 VS |
67 | s.Printf("%s %i", "foo", 42); |
68 | CPPUNIT_ASSERT( s == "foo 42" ); | |
69 | s.Printf("%s %s %i", _T("bar"), "=", 11); | |
359bd4d1 VS |
70 | |
71 | // test passing c_str(): | |
c9f78968 VS |
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 | ||
0dc8d224 VS |
78 | s2.Printf("%s mailbox", wxString("Opening").c_str()); |
79 | CPPUNIT_ASSERT( s2 == "Opening mailbox" ); | |
80 | ||
359bd4d1 | 81 | // test passing wxString directly: |
c9f78968 VS |
82 | s2.Printf(_T("[%s](%s)"), s, "str"); |
83 | CPPUNIT_ASSERT( s2 == "[bar = 11](str)" ); | |
359bd4d1 VS |
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;" ); | |
baf3d1dc VS |
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")); | |
47346406 VS |
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: | |
2452025c | 117 | #ifdef _MSC_VER |
5098c258 | 118 | #pragma warning(disable:4305) // truncation of constant value in VC6 |
2452025c VZ |
119 | #pragma warning(disable:4309) // truncation of constant value |
120 | #endif | |
47346406 | 121 | c = 240; |
2452025c | 122 | #ifdef _MSC_VER |
5098c258 | 123 | #pragma warning(default:4305) // truncation of constant value in VC6 |
2452025c VZ |
124 | #pragma warning(default:4309) |
125 | #endif | |
47346406 VS |
126 | s.Printf("value is %i (int)", c); |
127 | WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s ); | |
76fc401a | 128 | |
47346406 VS |
129 | unsigned char u = 240; |
130 | s.Printf("value is %i (int)", u); | |
131 | WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s ); | |
76fc401a VS |
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)." ); | |
c9f78968 | 148 | } |
76fc401a | 149 | #endif // wxUSE_STD_STRING |
eb6cb207 VS |
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 | } |