]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/vararg.cpp
Use UTF16 for text data object on Mac. Fixes #10902
[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 );
2f539d95 44 CPPUNIT_TEST( RepeatedPrintf );
c9f78968
VS
45 CPPUNIT_TEST_SUITE_END();
46
47 void StringPrintf();
47346406 48 void CharPrintf();
76fc401a
VS
49#if wxUSE_STD_STRING
50 void StdString();
51#endif
eb6cb207 52 void Sscanf();
2f539d95 53 void RepeatedPrintf();
c9f78968
VS
54
55 DECLARE_NO_COPY_CLASS(VarArgTestCase)
56};
57
58// register in the unnamed registry so that these tests are run by default
59CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
60
61// also include in it's own registry so that these tests can be run alone
62CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
63
64void VarArgTestCase::StringPrintf()
65{
66 wxString s, s2;
67
359bd4d1 68 // test passing literals:
c9f78968
VS
69 s.Printf("%s %i", "foo", 42);
70 CPPUNIT_ASSERT( s == "foo 42" );
71 s.Printf("%s %s %i", _T("bar"), "=", 11);
359bd4d1
VS
72
73 // test passing c_str():
c9f78968
VS
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
0dc8d224
VS
80 s2.Printf("%s mailbox", wxString("Opening").c_str());
81 CPPUNIT_ASSERT( s2 == "Opening mailbox" );
82
359bd4d1 83 // test passing wxString directly:
c9f78968
VS
84 s2.Printf(_T("[%s](%s)"), s, "str");
85 CPPUNIT_ASSERT( s2 == "[bar = 11](str)" );
359bd4d1
VS
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;" );
baf3d1dc
VS
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"));
47346406
VS
98}
99
100void VarArgTestCase::CharPrintf()
101{
102 wxString foo("foo");
103 wxString s;
104
105 // test using wchar_t:
106 s.Printf("char=%c", L'c');
1de532f5 107 CPPUNIT_ASSERT_EQUAL( "char=c", s );
47346406
VS
108
109 // test wxUniCharRef:
110 s.Printf("string[1] is %c", foo[1]);
1de532f5 111 CPPUNIT_ASSERT_EQUAL( "string[1] is o", s );
47346406
VS
112
113 // test char
114 char c = 'z';
115 s.Printf("%c to %c", 'a', c);
1de532f5 116 CPPUNIT_ASSERT_EQUAL( "a to z", s );
47346406
VS
117
118 // test char used as integer:
2452025c 119 #ifdef _MSC_VER
5098c258 120 #pragma warning(disable:4305) // truncation of constant value in VC6
2452025c
VZ
121 #pragma warning(disable:4309) // truncation of constant value
122 #endif
47346406 123 c = 240;
2452025c 124 #ifdef _MSC_VER
5098c258 125 #pragma warning(default:4305) // truncation of constant value in VC6
2452025c
VZ
126 #pragma warning(default:4309)
127 #endif
47346406 128 s.Printf("value is %i (int)", c);
1de532f5 129 CPPUNIT_ASSERT_EQUAL( wxString("value is -16 (int)"), s );
76fc401a 130
47346406
VS
131 unsigned char u = 240;
132 s.Printf("value is %i (int)", u);
1de532f5 133 CPPUNIT_ASSERT_EQUAL( "value is 240 (int)", s );
76fc401a
VS
134}
135
136#if wxUSE_STD_STRING
137void 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);
1de532f5 146 CPPUNIT_ASSERT_EQUAL( "string multi-byte(1).", s );
76fc401a
VS
147
148 s.Printf("string %s(%i).", wc, 2);
1de532f5 149 CPPUNIT_ASSERT_EQUAL( "string widechar(2).", s );
c9f78968 150}
76fc401a 151#endif // wxUSE_STD_STRING
eb6cb207
VS
152
153void 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}
2f539d95
VS
170
171void 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}