]>
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_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:4309) // truncation of constant value | |
119 | #endif | |
120 | c = 240; | |
121 | #ifdef _MSC_VER | |
122 | #pragma warning(default:4309) | |
123 | #endif | |
124 | s.Printf("value is %i (int)", c); | |
125 | WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s ); | |
126 | ||
127 | unsigned char u = 240; | |
128 | s.Printf("value is %i (int)", u); | |
129 | WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s ); | |
130 | } | |
131 | ||
132 | #if wxUSE_STD_STRING | |
133 | void VarArgTestCase::StdString() | |
134 | { | |
135 | // test passing std::[w]string | |
136 | wxString s; | |
137 | ||
138 | std::string mb("multi-byte"); | |
139 | std::string wc("widechar"); | |
140 | ||
141 | s.Printf("string %s(%i).", mb, 1); | |
142 | CPPUNIT_ASSERT( s == "string multi-byte(1)." ); | |
143 | ||
144 | s.Printf("string %s(%i).", wc, 2); | |
145 | CPPUNIT_ASSERT( s == "string widechar(2)." ); | |
146 | } | |
147 | #endif // wxUSE_STD_STRING | |
148 | ||
149 | void VarArgTestCase::Sscanf() | |
150 | { | |
151 | int i = 0; | |
152 | char str[20]; | |
153 | wchar_t wstr[20]; | |
154 | ||
155 | wxString input("42 test"); | |
156 | ||
157 | wxSscanf(input, "%d %s", &i, &str); | |
158 | CPPUNIT_ASSERT( i == 42 ); | |
159 | CPPUNIT_ASSERT( wxStrcmp(str, "test") == 0 ); | |
160 | ||
161 | i = 0; | |
162 | wxSscanf(input, L"%d %s", &i, &wstr); | |
163 | CPPUNIT_ASSERT( i == 42 ); | |
164 | CPPUNIT_ASSERT( wxStrcmp(wstr, "test") == 0 ); | |
165 | } |