]> git.saurik.com Git - wxWidgets.git/blame - tests/strings/crt.cpp
paint events cannot be constructed outside a native redraw in OSX
[wxWidgets.git] / tests / strings / crt.cpp
CommitLineData
cf8ee08b
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/strings/crt.cpp
3// Purpose: Test for wx C runtime functions wrappers
4// Author: Vaclav Slavik
5// Created: 2004-06-03
6// RCS-ID: $Id$
7c5ac499 7// Copyright: (c) 2004 Vaclav Slavik
cf8ee08b
VS
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ----------------------------------------------------------------------------
12// headers
13// ----------------------------------------------------------------------------
14
8899b155 15#include "testprec.h"
cf8ee08b
VS
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/textfile.h"
26
af9c764a
VZ
27// ----------------------------------------------------------------------------
28// constants
29// ----------------------------------------------------------------------------
30
31static const char *strMB = "hello, world";
32static const wchar_t *strWC = L"hello, world";
33static const wxString strWX("hello, world");
34
cf8ee08b
VS
35// ----------------------------------------------------------------------------
36// test class
37// ----------------------------------------------------------------------------
38
39class CrtTestCase : public CppUnit::TestCase
40{
41public:
42 CrtTestCase() {}
43
44private:
45 CPPUNIT_TEST_SUITE( CrtTestCase );
46 CPPUNIT_TEST( SetGetEnv );
874dbd3a 47 CPPUNIT_TEST( Strchr );
52de37c7
VS
48 CPPUNIT_TEST( Strcmp );
49 CPPUNIT_TEST( Strspn );
50 CPPUNIT_TEST( Strcspn );
105260e8 51 CPPUNIT_TEST( Strpbrk );
7c5ac499 52 CPPUNIT_TEST( Strnlen );
cf8ee08b
VS
53 CPPUNIT_TEST_SUITE_END();
54
55 void SetGetEnv();
874dbd3a 56 void Strchr();
52de37c7
VS
57 void Strcmp();
58 void Strspn();
59 void Strcspn();
105260e8 60 void Strpbrk();
7c5ac499 61 void Strnlen();
cf8ee08b
VS
62
63 DECLARE_NO_COPY_CLASS(CrtTestCase)
64};
65
66// register in the unnamed registry so that these tests are run by default
67CPPUNIT_TEST_SUITE_REGISTRATION( CrtTestCase );
68
69// also include in it's own registry so that these tests can be run alone
70CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" );
71
72void CrtTestCase::SetGetEnv()
73{
9a83f860 74#define TESTVAR_NAME wxT("WXTESTVAR")
787de19a 75
cf8ee08b 76 wxString val;
9a83f860 77 wxSetEnv(TESTVAR_NAME, wxT("value"));
787de19a
VZ
78 CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
79 CPPUNIT_ASSERT_EQUAL( "value", val );
80 CPPUNIT_ASSERT_EQUAL( "value", wxString(wxGetenv(TESTVAR_NAME)) );
81
9a83f860 82 wxSetEnv(TESTVAR_NAME, wxT("something else"));
787de19a
VZ
83 CPPUNIT_ASSERT( wxGetEnv(TESTVAR_NAME, &val) );
84 CPPUNIT_ASSERT_EQUAL( "something else", val );
85 CPPUNIT_ASSERT_EQUAL( "something else", wxString(wxGetenv(TESTVAR_NAME)) );
86
87 CPPUNIT_ASSERT( wxUnsetEnv(TESTVAR_NAME) );
88 CPPUNIT_ASSERT( !wxGetEnv(TESTVAR_NAME, NULL) );
89 CPPUNIT_ASSERT( !wxGetenv(TESTVAR_NAME) );
90
91#undef TESTVAR_NAME
cf8ee08b 92}
52de37c7 93
874dbd3a
VZ
94void CrtTestCase::Strchr()
95{
96 // test that searching for a wide character in a narrow string simply
97 // doesn't find it but doesn't fail with an assert (#11487)
98 const wxUniChar smiley = *wxString::FromUTF8("\xe2\x98\xba").begin();
99
100 CPPUNIT_ASSERT( !wxStrchr("hello", smiley) );
101
102 // but searching for an explicitly wide character does find it
103 CPPUNIT_ASSERT( wxStrchr(wxString::FromUTF8(":-) == \xe2\x98\xba"),
104 static_cast<wchar_t>(smiley)) );
105}
106
52de37c7
VS
107void CrtTestCase::Strcmp()
108{
109 // this code tests if all possible ways of calling wxStrcmp() compile:
110 const char * const char1 = "first";
111 const wchar_t * const wchar1 = L"first";
112 wxString str1("first");
113 wxCStrData cstr1(str1.c_str());
114 wxCharBuffer charbuf1(char1);
115 wxWCharBuffer wcharbuf1(wchar1);
116
117 const char * const char2 = "last";
118 const wchar_t * const wchar2 = L"last";
119 wxString str2("last");
120 wxCStrData cstr2(str2.c_str());
121 wxCharBuffer charbuf2(char2);
122 wxWCharBuffer wcharbuf2(wchar2);
123
124 CPPUNIT_ASSERT( wxStrcmp(char1, char2) < 0 );
125 CPPUNIT_ASSERT( wxStrcmp(char1, wchar2) < 0 );
126 CPPUNIT_ASSERT( wxStrcmp(char1, str2) < 0 );
127 CPPUNIT_ASSERT( wxStrcmp(char1, cstr2) < 0 );
128 CPPUNIT_ASSERT( wxStrcmp(char1, charbuf2) < 0 );
129 CPPUNIT_ASSERT( wxStrcmp(char1, wcharbuf2) < 0 );
130
131 CPPUNIT_ASSERT( wxStrcmp(wchar1, char2) < 0 );
132 CPPUNIT_ASSERT( wxStrcmp(wchar1, wchar2) < 0 );
133 CPPUNIT_ASSERT( wxStrcmp(wchar1, str2) < 0 );
134 CPPUNIT_ASSERT( wxStrcmp(wchar1, cstr2) < 0 );
135 CPPUNIT_ASSERT( wxStrcmp(wchar1, charbuf2) < 0 );
136 CPPUNIT_ASSERT( wxStrcmp(wchar1, wcharbuf2) < 0 );
137
138 CPPUNIT_ASSERT( wxStrcmp(str1, char2) < 0 );
139 CPPUNIT_ASSERT( wxStrcmp(str1, wchar2) < 0 );
140 CPPUNIT_ASSERT( wxStrcmp(str1, str2) < 0 );
141 CPPUNIT_ASSERT( wxStrcmp(str1, cstr2) < 0 );
142 CPPUNIT_ASSERT( wxStrcmp(str1, charbuf2) < 0 );
143 CPPUNIT_ASSERT( wxStrcmp(str1, wcharbuf2) < 0 );
144
145 CPPUNIT_ASSERT( wxStrcmp(cstr1, char2) < 0 );
146 CPPUNIT_ASSERT( wxStrcmp(cstr1, wchar2) < 0 );
147 CPPUNIT_ASSERT( wxStrcmp(cstr1, str2) < 0 );
148 CPPUNIT_ASSERT( wxStrcmp(cstr1, cstr2) < 0 );
149 CPPUNIT_ASSERT( wxStrcmp(cstr1, charbuf2) < 0 );
150 CPPUNIT_ASSERT( wxStrcmp(cstr1, wcharbuf2) < 0 );
151
152 CPPUNIT_ASSERT( wxStrcmp(charbuf1, char2) < 0 );
153 CPPUNIT_ASSERT( wxStrcmp(charbuf1, wchar2) < 0 );
154 CPPUNIT_ASSERT( wxStrcmp(charbuf1, str2) < 0 );
155 CPPUNIT_ASSERT( wxStrcmp(charbuf1, cstr2) < 0 );
156 CPPUNIT_ASSERT( wxStrcmp(charbuf1, charbuf2) < 0 );
157 CPPUNIT_ASSERT( wxStrcmp(charbuf1, wcharbuf2) < 0 );
158
159 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, char2) < 0 );
160 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wchar2) < 0 );
161 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, str2) < 0 );
162 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, cstr2) < 0 );
163 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, charbuf2) < 0 );
164 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wcharbuf2) < 0 );
165}
166
167void CrtTestCase::Strspn()
168{
f291c98e
VZ
169 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "xyz") );
170 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "xyz") );
171 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "xyz") );
172 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, L"xyz") );
173 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, L"xyz") );
174 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, L"xyz") );
175
176 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strMB, "hleo") );
177 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWC, "hleo") );
178 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWX, "hleo") );
179
180 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "ld") );
181 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "ld") );
182 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "ld") );
183
184 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strMB, strWC) );
185 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWC, strWX) );
186 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWX, strMB) );
52de37c7
VS
187}
188
189void CrtTestCase::Strcspn()
190{
f291c98e
VZ
191 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, strWX) );
192 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, strMB) );
193 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, strWC) );
52de37c7 194
f291c98e
VZ
195 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strMB, ", ") );
196 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWC, ", ") );
197 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWX, ", ") );
52de37c7 198
f291c98e
VZ
199 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, "hel") );
200 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, "hel") );
201 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, "hel") );
52de37c7 202
f291c98e
VZ
203 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strMB, "xy") );
204 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWC, "xy") );
205 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWX, "xy") );
52de37c7 206}
af9c764a 207
105260e8
VZ
208void CrtTestCase::Strpbrk()
209{
210 const wxString s(", ");
211
212 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, ", ") );
213 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, L", ") );
214 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, ", ") );
215 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, L", ") );
216
217 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s) );
218 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s) );
219 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s) );
220 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s) );
221
222 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.c_str()) );
223 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.c_str()) );
224 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.c_str()) );
225 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.c_str()) );
226
227 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.mb_str()) );
228 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.wc_str()) );
229 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.mb_str()) );
230 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, s.wc_str()) );
231 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.mb_str()) );
232 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX.c_str(), s.wc_str()) );
233
390b8241
VZ
234 CPPUNIT_ASSERT( !wxStrpbrk(strWX, "xyz") );
235 CPPUNIT_ASSERT( !wxStrpbrk(strWX.c_str(), L"xyz") );
105260e8
VZ
236}
237
7c5ac499
FM
238void CrtTestCase::Strnlen()
239{
7c5ac499
FM
240 // other misc tests for wxStrnlen(const char*, size_t)
241
242 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 0) );
243 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 8) );
244 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("1234", 0) );
245 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen("1234", 2) );
246 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234", 8) );
247 CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen("1234567", 8) );
248 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("12345678", 8) );
249 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("123456789", 8) );
250 CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen("123456789", 12) );
251
252 // other misc tests for wxStrnlen(const wchar_t*, size_t)
253
254 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 0) );
255 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 8) );
256 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"1234", 0) );
257 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen(L"1234", 2) );
258 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234", 8) );
259 CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen(L"1234567", 8) );
260 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"12345678", 8) );
261 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"123456789", 8) );
262 CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen(L"123456789", 12) );
263
264 // wxStrlen() is only for NULL-terminated strings:
265 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234" "\0" "78", 12) );
266 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234" L"\0" L"5678", 12) );
267}
268
269
270