]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/vararg.cpp
return type for "grab-broken-event" is not void; remove some redundant conditionals
[wxWidgets.git] / tests / strings / vararg.cpp
CommitLineData
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
31class VarArgTestCase : public CppUnit::TestCase
32{
33public:
34 VarArgTestCase() {}
35
36private:
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
57CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
58
59// also include in it's own registry so that these tests can be run alone
60CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
61
62void 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
98void 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
VZ
117 #ifdef _MSC_VER
118 #pragma warning(disable:4309) // truncation of constant value
119 #endif
47346406 120 c = 240;
2452025c
VZ
121 #ifdef _MSC_VER
122 #pragma warning(default:4309)
123 #endif
47346406
VS
124 s.Printf("value is %i (int)", c);
125 WX_ASSERT_STR_EQUAL( wxString("value is -16 (int)"), s );
76fc401a 126
47346406
VS
127 unsigned char u = 240;
128 s.Printf("value is %i (int)", u);
129 WX_ASSERT_STR_EQUAL( wxString("value is 240 (int)"), s );
76fc401a
VS
130}
131
132#if wxUSE_STD_STRING
133void 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)." );
c9f78968 146}
76fc401a 147#endif // wxUSE_STD_STRING
eb6cb207
VS
148
149void 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}