]>
git.saurik.com Git - wxWidgets.git/blob - tests/strings/crt.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/crt.cpp
3 // Purpose: Test for wx C runtime functions wrappers
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2004 Vaclav Slavik
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
25 #include "wx/textfile.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 static const char *strMB
= "hello, world";
32 static const wchar_t *strWC
= L
"hello, world";
33 static const wxString
strWX("hello, world");
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class CrtTestCase
: public CppUnit::TestCase
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_SUITE_END();
59 DECLARE_NO_COPY_CLASS(CrtTestCase
)
62 // register in the unnamed registry so that these tests are run by default
63 CPPUNIT_TEST_SUITE_REGISTRATION( CrtTestCase
);
65 // also include in it's own registry so that these tests can be run alone
66 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase
, "CrtTestCase" );
68 void CrtTestCase::SetGetEnv()
71 wxSetEnv(_T("TESTVAR"), _T("value"));
72 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val
) == true );
73 CPPUNIT_ASSERT( val
== _T("value") );
74 wxSetEnv(_T("TESTVAR"), _T("something else"));
75 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val
) );
76 CPPUNIT_ASSERT( val
== _T("something else") );
77 CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) );
78 CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL
) == false );
81 void CrtTestCase::Strcmp()
83 // this code tests if all possible ways of calling wxStrcmp() compile:
84 const char * const char1
= "first";
85 const wchar_t * const wchar1
= L
"first";
86 wxString
str1("first");
87 wxCStrData
cstr1(str1
.c_str());
88 wxCharBuffer
charbuf1(char1
);
89 wxWCharBuffer
wcharbuf1(wchar1
);
91 const char * const char2
= "last";
92 const wchar_t * const wchar2
= L
"last";
93 wxString
str2("last");
94 wxCStrData
cstr2(str2
.c_str());
95 wxCharBuffer
charbuf2(char2
);
96 wxWCharBuffer
wcharbuf2(wchar2
);
98 CPPUNIT_ASSERT( wxStrcmp(char1
, char2
) < 0 );
99 CPPUNIT_ASSERT( wxStrcmp(char1
, wchar2
) < 0 );
100 CPPUNIT_ASSERT( wxStrcmp(char1
, str2
) < 0 );
101 CPPUNIT_ASSERT( wxStrcmp(char1
, cstr2
) < 0 );
102 CPPUNIT_ASSERT( wxStrcmp(char1
, charbuf2
) < 0 );
103 CPPUNIT_ASSERT( wxStrcmp(char1
, wcharbuf2
) < 0 );
105 CPPUNIT_ASSERT( wxStrcmp(wchar1
, char2
) < 0 );
106 CPPUNIT_ASSERT( wxStrcmp(wchar1
, wchar2
) < 0 );
107 CPPUNIT_ASSERT( wxStrcmp(wchar1
, str2
) < 0 );
108 CPPUNIT_ASSERT( wxStrcmp(wchar1
, cstr2
) < 0 );
109 CPPUNIT_ASSERT( wxStrcmp(wchar1
, charbuf2
) < 0 );
110 CPPUNIT_ASSERT( wxStrcmp(wchar1
, wcharbuf2
) < 0 );
112 CPPUNIT_ASSERT( wxStrcmp(str1
, char2
) < 0 );
113 CPPUNIT_ASSERT( wxStrcmp(str1
, wchar2
) < 0 );
114 CPPUNIT_ASSERT( wxStrcmp(str1
, str2
) < 0 );
115 CPPUNIT_ASSERT( wxStrcmp(str1
, cstr2
) < 0 );
116 CPPUNIT_ASSERT( wxStrcmp(str1
, charbuf2
) < 0 );
117 CPPUNIT_ASSERT( wxStrcmp(str1
, wcharbuf2
) < 0 );
119 CPPUNIT_ASSERT( wxStrcmp(cstr1
, char2
) < 0 );
120 CPPUNIT_ASSERT( wxStrcmp(cstr1
, wchar2
) < 0 );
121 CPPUNIT_ASSERT( wxStrcmp(cstr1
, str2
) < 0 );
122 CPPUNIT_ASSERT( wxStrcmp(cstr1
, cstr2
) < 0 );
123 CPPUNIT_ASSERT( wxStrcmp(cstr1
, charbuf2
) < 0 );
124 CPPUNIT_ASSERT( wxStrcmp(cstr1
, wcharbuf2
) < 0 );
126 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, char2
) < 0 );
127 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, wchar2
) < 0 );
128 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, str2
) < 0 );
129 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, cstr2
) < 0 );
130 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, charbuf2
) < 0 );
131 CPPUNIT_ASSERT( wxStrcmp(charbuf1
, wcharbuf2
) < 0 );
133 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, char2
) < 0 );
134 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, wchar2
) < 0 );
135 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, str2
) < 0 );
136 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, cstr2
) < 0 );
137 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, charbuf2
) < 0 );
138 CPPUNIT_ASSERT( wxStrcmp(wcharbuf1
, wcharbuf2
) < 0 );
141 void CrtTestCase::Strspn()
143 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB
, "xyz") );
144 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC
, "xyz") );
145 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX
, "xyz") );
146 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB
, L
"xyz") );
147 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC
, L
"xyz") );
148 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX
, L
"xyz") );
150 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strMB
, "hleo") );
151 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWC
, "hleo") );
152 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWX
, "hleo") );
154 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB
, "ld") );
155 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC
, "ld") );
156 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX
, "ld") );
158 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrspn(strMB
, strWC
) );
159 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrspn(strWC
, strWX
) );
160 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrspn(strWX
, strMB
) );
163 void CrtTestCase::Strcspn()
165 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB
, strWX
) );
166 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC
, strMB
) );
167 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX
, strWC
) );
169 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strMB
, ", ") );
170 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWC
, ", ") );
171 CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWX
, ", ") );
173 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB
, "hel") );
174 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC
, "hel") );
175 CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX
, "hel") );
177 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrcspn(strMB
, "xy") );
178 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrcspn(strWC
, "xy") );
179 CPPUNIT_ASSERT_EQUAL( strWX
.length(), wxStrcspn(strWX
, "xy") );
182 void CrtTestCase::Strpbrk()
184 const wxString
s(", ");
186 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB
, ", ") );
187 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWC
, L
", ") );
188 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
, ", ") );
189 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWX
, L
", ") );
191 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB
, s
) );
192 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWC
, s
) );
193 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
, s
) );
194 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
.c_str(), s
) );
196 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB
, s
.c_str()) );
197 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWC
, s
.c_str()) );
198 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
, s
.c_str()) );
199 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
.c_str(), s
.c_str()) );
201 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strMB
, s
.mb_str()) );
202 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWC
, s
.wc_str()) );
203 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
, s
.mb_str()) );
204 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWX
, s
.wc_str()) );
205 CPPUNIT_ASSERT_EQUAL( ',', *wxStrpbrk(strWX
.c_str(), s
.mb_str()) );
206 CPPUNIT_ASSERT_EQUAL( L
',', *wxStrpbrk(strWX
.c_str(), s
.wc_str()) );
208 CPPUNIT_ASSERT_EQUAL( (const char *)NULL
, wxStrpbrk(strWX
, "xyz") );
209 CPPUNIT_ASSERT_EQUAL( (const wchar_t *)NULL
, wxStrpbrk(strWX
.c_str(), L
"xyz") );