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