]>
Commit | Line | Data |
---|---|---|
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$ | |
7 | // Copyright: (c) 2004 Vaclav Slavik | |
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 | ||
31 | static const char *strMB = "hello, world"; | |
32 | static const wchar_t *strWC = L"hello, world"; | |
33 | static const wxString strWX("hello, world"); | |
34 | ||
cf8ee08b VS |
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 ); | |
52de37c7 VS |
47 | CPPUNIT_TEST( Strcmp ); |
48 | CPPUNIT_TEST( Strspn ); | |
49 | CPPUNIT_TEST( Strcspn ); | |
cf8ee08b VS |
50 | CPPUNIT_TEST_SUITE_END(); |
51 | ||
52 | void SetGetEnv(); | |
52de37c7 VS |
53 | void Strcmp(); |
54 | void Strspn(); | |
55 | void Strcspn(); | |
cf8ee08b VS |
56 | |
57 | DECLARE_NO_COPY_CLASS(CrtTestCase) | |
58 | }; | |
59 | ||
60 | // register in the unnamed registry so that these tests are run by default | |
61 | CPPUNIT_TEST_SUITE_REGISTRATION( CrtTestCase ); | |
62 | ||
63 | // also include in it's own registry so that these tests can be run alone | |
64 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CrtTestCase, "CrtTestCase" ); | |
65 | ||
66 | void CrtTestCase::SetGetEnv() | |
67 | { | |
68 | wxString val; | |
69 | wxSetEnv(_T("TESTVAR"), _T("value")); | |
70 | CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) == true ); | |
71 | CPPUNIT_ASSERT( val == _T("value") ); | |
72 | wxSetEnv(_T("TESTVAR"), _T("something else")); | |
73 | CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), &val) ); | |
74 | CPPUNIT_ASSERT( val == _T("something else") ); | |
75 | CPPUNIT_ASSERT( wxUnsetEnv(_T("TESTVAR")) ); | |
76 | CPPUNIT_ASSERT( wxGetEnv(_T("TESTVAR"), NULL) == false ); | |
77 | } | |
52de37c7 VS |
78 | |
79 | void CrtTestCase::Strcmp() | |
80 | { | |
81 | // this code tests if all possible ways of calling wxStrcmp() compile: | |
82 | const char * const char1 = "first"; | |
83 | const wchar_t * const wchar1 = L"first"; | |
84 | wxString str1("first"); | |
85 | wxCStrData cstr1(str1.c_str()); | |
86 | wxCharBuffer charbuf1(char1); | |
87 | wxWCharBuffer wcharbuf1(wchar1); | |
88 | ||
89 | const char * const char2 = "last"; | |
90 | const wchar_t * const wchar2 = L"last"; | |
91 | wxString str2("last"); | |
92 | wxCStrData cstr2(str2.c_str()); | |
93 | wxCharBuffer charbuf2(char2); | |
94 | wxWCharBuffer wcharbuf2(wchar2); | |
95 | ||
96 | CPPUNIT_ASSERT( wxStrcmp(char1, char2) < 0 ); | |
97 | CPPUNIT_ASSERT( wxStrcmp(char1, wchar2) < 0 ); | |
98 | CPPUNIT_ASSERT( wxStrcmp(char1, str2) < 0 ); | |
99 | CPPUNIT_ASSERT( wxStrcmp(char1, cstr2) < 0 ); | |
100 | CPPUNIT_ASSERT( wxStrcmp(char1, charbuf2) < 0 ); | |
101 | CPPUNIT_ASSERT( wxStrcmp(char1, wcharbuf2) < 0 ); | |
102 | ||
103 | CPPUNIT_ASSERT( wxStrcmp(wchar1, char2) < 0 ); | |
104 | CPPUNIT_ASSERT( wxStrcmp(wchar1, wchar2) < 0 ); | |
105 | CPPUNIT_ASSERT( wxStrcmp(wchar1, str2) < 0 ); | |
106 | CPPUNIT_ASSERT( wxStrcmp(wchar1, cstr2) < 0 ); | |
107 | CPPUNIT_ASSERT( wxStrcmp(wchar1, charbuf2) < 0 ); | |
108 | CPPUNIT_ASSERT( wxStrcmp(wchar1, wcharbuf2) < 0 ); | |
109 | ||
110 | CPPUNIT_ASSERT( wxStrcmp(str1, char2) < 0 ); | |
111 | CPPUNIT_ASSERT( wxStrcmp(str1, wchar2) < 0 ); | |
112 | CPPUNIT_ASSERT( wxStrcmp(str1, str2) < 0 ); | |
113 | CPPUNIT_ASSERT( wxStrcmp(str1, cstr2) < 0 ); | |
114 | CPPUNIT_ASSERT( wxStrcmp(str1, charbuf2) < 0 ); | |
115 | CPPUNIT_ASSERT( wxStrcmp(str1, wcharbuf2) < 0 ); | |
116 | ||
117 | CPPUNIT_ASSERT( wxStrcmp(cstr1, char2) < 0 ); | |
118 | CPPUNIT_ASSERT( wxStrcmp(cstr1, wchar2) < 0 ); | |
119 | CPPUNIT_ASSERT( wxStrcmp(cstr1, str2) < 0 ); | |
120 | CPPUNIT_ASSERT( wxStrcmp(cstr1, cstr2) < 0 ); | |
121 | CPPUNIT_ASSERT( wxStrcmp(cstr1, charbuf2) < 0 ); | |
122 | CPPUNIT_ASSERT( wxStrcmp(cstr1, wcharbuf2) < 0 ); | |
123 | ||
124 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, char2) < 0 ); | |
125 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, wchar2) < 0 ); | |
126 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, str2) < 0 ); | |
127 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, cstr2) < 0 ); | |
128 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, charbuf2) < 0 ); | |
129 | CPPUNIT_ASSERT( wxStrcmp(charbuf1, wcharbuf2) < 0 ); | |
130 | ||
131 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, char2) < 0 ); | |
132 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wchar2) < 0 ); | |
133 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, str2) < 0 ); | |
134 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, cstr2) < 0 ); | |
135 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, charbuf2) < 0 ); | |
136 | CPPUNIT_ASSERT( wxStrcmp(wcharbuf1, wcharbuf2) < 0 ); | |
137 | } | |
138 | ||
139 | void CrtTestCase::Strspn() | |
140 | { | |
f291c98e VZ |
141 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "xyz") ); |
142 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "xyz") ); | |
143 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "xyz") ); | |
144 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, L"xyz") ); | |
145 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, L"xyz") ); | |
146 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, L"xyz") ); | |
147 | ||
148 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strMB, "hleo") ); | |
149 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWC, "hleo") ); | |
150 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrspn(strWX, "hleo") ); | |
151 | ||
152 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strMB, "ld") ); | |
153 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWC, "ld") ); | |
154 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrspn(strWX, "ld") ); | |
155 | ||
156 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strMB, strWC) ); | |
157 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWC, strWX) ); | |
158 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrspn(strWX, strMB) ); | |
52de37c7 VS |
159 | } |
160 | ||
161 | void CrtTestCase::Strcspn() | |
162 | { | |
f291c98e VZ |
163 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, strWX) ); |
164 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, strMB) ); | |
165 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, strWC) ); | |
52de37c7 | 166 | |
f291c98e VZ |
167 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strMB, ", ") ); |
168 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWC, ", ") ); | |
169 | CPPUNIT_ASSERT_EQUAL( (size_t)5, wxStrcspn(strWX, ", ") ); | |
52de37c7 | 170 | |
f291c98e VZ |
171 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strMB, "hel") ); |
172 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWC, "hel") ); | |
173 | CPPUNIT_ASSERT_EQUAL( (size_t)0, wxStrcspn(strWX, "hel") ); | |
52de37c7 | 174 | |
f291c98e VZ |
175 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strMB, "xy") ); |
176 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWC, "xy") ); | |
177 | CPPUNIT_ASSERT_EQUAL( strWX.length(), wxStrcspn(strWX, "xy") ); | |
52de37c7 | 178 | } |
af9c764a | 179 |