]> git.saurik.com Git - wxWidgets.git/blame_incremental - tests/strings/crt.cpp
always call SetIcon() on the main frame of the sample; some small cleanup
[wxWidgets.git] / tests / strings / crt.cpp
... / ...
CommitLineData
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
31static const char *strMB = "hello, world";
32static const wchar_t *strWC = L"hello, world";
33static const wxString strWX("hello, world");
34
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 );
47 CPPUNIT_TEST( Strcmp );
48 CPPUNIT_TEST( Strspn );
49 CPPUNIT_TEST( Strcspn );
50 CPPUNIT_TEST( Strpbrk );
51 CPPUNIT_TEST( Strnlen );
52 CPPUNIT_TEST_SUITE_END();
53
54 void SetGetEnv();
55 void Strcmp();
56 void Strspn();
57 void Strcspn();
58 void Strpbrk();
59 void Strnlen();
60
61 DECLARE_NO_COPY_CLASS(CrtTestCase)
62};
63
64// register in the unnamed registry so that these tests are run by default
65CPPUNIT_TEST_SUITE_REGISTRATION( CrtTestCase );
66
67// also include in it's own registry so that these tests can be run alone
68CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" );
69
70void CrtTestCase::SetGetEnv()
71{
72 wxString val;
73 wxSetEnv(_T("TESTVAR"), _T("value"));
74 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) == true );
75 CPPUNIT_ASSERT( val == _T("value") );
76 wxSetEnv(_T("TESTVAR"), _T("something else"));
77 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) );
78 CPPUNIT_ASSERT( val == _T("something else") );
79 CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) );
80 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL) == false );
81}
82
83void CrtTestCase::Strcmp()
84{
85 // this code tests if all possible ways of calling wxStrcmp() compile:
86 const char * const char1 = "first";
87 const wchar_t * const wchar1 = L"first";
88 wxString str1("first");
89 wxCStrData cstr1(str1.c_str());
90 wxCharBuffer charbuf1(char1);
91 wxWCharBuffer wcharbuf1(wchar1);
92
93 const char * const char2 = "last";
94 const wchar_t * const wchar2 = L"last";
95 wxString str2("last");
96 wxCStrData cstr2(str2.c_str());
97 wxCharBuffer charbuf2(char2);
98 wxWCharBuffer wcharbuf2(wchar2);
99
100 CPPUNIT_ASSERT( wxStrcmp(char1, char2) < 0 );
101 CPPUNIT_ASSERT( wxStrcmp(char1, wchar2) < 0 );
102 CPPUNIT_ASSERT( wxStrcmp(char1, str2) < 0 );
103 CPPUNIT_ASSERT( wxStrcmp(char1, cstr2) < 0 );
104 CPPUNIT_ASSERT( wxStrcmp(char1, charbuf2) < 0 );
105 CPPUNIT_ASSERT( wxStrcmp(char1, wcharbuf2) < 0 );
106
107 CPPUNIT_ASSERT( wxStrcmp(wchar1, char2) < 0 );
108 CPPUNIT_ASSERT( wxStrcmp(wchar1, wchar2) < 0 );
109 CPPUNIT_ASSERT( wxStrcmp(wchar1, str2) < 0 );
110 CPPUNIT_ASSERT( wxStrcmp(wchar1, cstr2) < 0 );
111 CPPUNIT_ASSERT( wxStrcmp(wchar1, charbuf2) < 0 );
112 CPPUNIT_ASSERT( wxStrcmp(wchar1, wcharbuf2) < 0 );
113
114 CPPUNIT_ASSERT( wxStrcmp(str1, char2) < 0 );
115 CPPUNIT_ASSERT( wxStrcmp(str1, wchar2) < 0 );
116 CPPUNIT_ASSERT( wxStrcmp(str1, str2) < 0 );
117 CPPUNIT_ASSERT( wxStrcmp(str1, cstr2) < 0 );
118 CPPUNIT_ASSERT( wxStrcmp(str1, charbuf2) < 0 );
119 CPPUNIT_ASSERT( wxStrcmp(str1, wcharbuf2) < 0 );
120
121 CPPUNIT_ASSERT( wxStrcmp(cstr1, char2) < 0 );
122 CPPUNIT_ASSERT( wxStrcmp(cstr1, wchar2) < 0 );
123 CPPUNIT_ASSERT( wxStrcmp(cstr1, str2) < 0 );
124 CPPUNIT_ASSERT( wxStrcmp(cstr1, cstr2) < 0 );
125 CPPUNIT_ASSERT( wxStrcmp(cstr1, charbuf2) < 0 );
126 CPPUNIT_ASSERT( wxStrcmp(cstr1, wcharbuf2) < 0 );
127
128 CPPUNIT_ASSERT( wxStrcmp(charbuf1, char2) < 0 );
129 CPPUNIT_ASSERT( wxStrcmp(charbuf1, wchar2) < 0 );
130 CPPUNIT_ASSERT( wxStrcmp(charbuf1, str2) < 0 );
131 CPPUNIT_ASSERT( wxStrcmp(charbuf1, cstr2) < 0 );
132 CPPUNIT_ASSERT( wxStrcmp(charbuf1, charbuf2) < 0 );
133 CPPUNIT_ASSERT( wxStrcmp(charbuf1, wcharbuf2) < 0 );
134
135 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, char2) < 0 );
136 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wchar2) < 0 );
137 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, str2) < 0 );
138 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, cstr2) < 0 );
139 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, charbuf2) < 0 );
140 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wcharbuf2) < 0 );
141}
142
143void CrtTestCase::Strspn()
144{
145 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "xyz") );
146 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "xyz") );
147 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "xyz") );
148 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, L"xyz") );
149 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, L"xyz") );
150 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, L"xyz") );
151
152 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strMB, "hleo") );
153 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWC, "hleo") );
154 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWX, "hleo") );
155
156 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "ld") );
157 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "ld") );
158 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "ld") );
159
160 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strMB, strWC) );
161 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWC, strWX) );
162 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWX, strMB) );
163}
164
165void CrtTestCase::Strcspn()
166{
167 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, strWX) );
168 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, strMB) );
169 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, strWC) );
170
171 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strMB, ", ") );
172 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWC, ", ") );
173 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWX, ", ") );
174
175 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, "hel") );
176 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, "hel") );
177 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, "hel") );
178
179 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strMB, "xy") );
180 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWC, "xy") );
181 CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWX, "xy") );
182}
183
184void CrtTestCase::Strpbrk()
185{
186 const wxString s(", ");
187
188 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, ", ") );
189 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, L", ") );
190 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, ", ") );
191 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, L", ") );
192
193 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s) );
194 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s) );
195 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s) );
196 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s) );
197
198 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.c_str()) );
199 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.c_str()) );
200 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.c_str()) );
201 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.c_str()) );
202
203 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB, s.mb_str()) );
204 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWC, s.wc_str()) );
205 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX, s.mb_str()) );
206 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX, s.wc_str()) );
207 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX.c_str(), s.mb_str()) );
208 CPPUNIT_ASSERT_EQUAL( L',', *wxStrpbrk(strWX.c_str(), s.wc_str()) );
209
210 CPPUNIT_ASSERT_EQUAL( (char *)NULL, wxStrpbrk(strWX, "xyz") );
211 CPPUNIT_ASSERT_EQUAL( (wchar_t *)NULL, wxStrpbrk(strWX.c_str(), L"xyz") );
212}
213
214void CrtTestCase::Strnlen()
215{
216 // other misc tests for wxStrnlen(const char*, size_t)
217
218 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 0) );
219 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("", 8) );
220 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen("1234", 0) );
221 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen("1234", 2) );
222 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234", 8) );
223 CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen("1234567", 8) );
224 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("12345678", 8) );
225 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen("123456789", 8) );
226 CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen("123456789", 12) );
227
228 // other misc tests for wxStrnlen(const wchar_t*, size_t)
229
230 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 0) );
231 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"", 8) );
232 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrnlen(L"1234", 0) );
233 CPPUNIT_ASSERT_EQUAL( (size_t)2, wxStrnlen(L"1234", 2) );
234 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234", 8) );
235 CPPUNIT_ASSERT_EQUAL( (size_t)7, wxStrnlen(L"1234567", 8) );
236 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"12345678", 8) );
237 CPPUNIT_ASSERT_EQUAL( (size_t)8, wxStrnlen(L"123456789", 8) );
238 CPPUNIT_ASSERT_EQUAL( (size_t)9, wxStrnlen(L"123456789", 12) );
239
240 // wxStrlen() is only for NULL-terminated strings:
241 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen("1234" "\0" "78", 12) );
242 CPPUNIT_ASSERT_EQUAL( (size_t)4, wxStrnlen(L"1234" L"\0" L"5678", 12) );
243}
244
245
246