]>
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 ); | |
76fc401a VS |
39 | #if wxUSE_STD_STRING |
40 | CPPUNIT_TEST( StdString ); | |
41 | #endif | |
eb6cb207 | 42 | CPPUNIT_TEST( Sscanf ); |
c9f78968 VS |
43 | CPPUNIT_TEST_SUITE_END(); |
44 | ||
45 | void StringPrintf(); | |
76fc401a VS |
46 | #if wxUSE_STD_STRING |
47 | void StdString(); | |
48 | #endif | |
eb6cb207 | 49 | void Sscanf(); |
c9f78968 VS |
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 | ||
359bd4d1 | 64 | // test passing literals: |
c9f78968 VS |
65 | s.Printf("%s %i", "foo", 42); |
66 | CPPUNIT_ASSERT( s == "foo 42" ); | |
67 | s.Printf("%s %s %i", _T("bar"), "=", 11); | |
359bd4d1 VS |
68 | |
69 | // test passing c_str(): | |
c9f78968 VS |
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 | ||
0dc8d224 VS |
76 | s2.Printf("%s mailbox", wxString("Opening").c_str()); |
77 | CPPUNIT_ASSERT( s2 == "Opening mailbox" ); | |
78 | ||
359bd4d1 | 79 | // test passing wxString directly: |
c9f78968 VS |
80 | s2.Printf(_T("[%s](%s)"), s, "str"); |
81 | CPPUNIT_ASSERT( s2 == "[bar = 11](str)" ); | |
359bd4d1 VS |
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;" ); | |
baf3d1dc VS |
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")); | |
76fc401a VS |
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)." ); | |
c9f78968 | 111 | } |
76fc401a | 112 | #endif // wxUSE_STD_STRING |
eb6cb207 VS |
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 | } |