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