]>
Commit | Line | Data |
---|---|---|
1cd53e88 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/strings/strings.cpp | |
3 | // Purpose: wxString unit test | |
4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba | |
5 | // Created: 2004-04-19 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
1cd53e88 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/tokenzr.h" | |
25 | ||
1cd53e88 VS |
26 | // ---------------------------------------------------------------------------- |
27 | // test class | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | class StringTestCase : public CppUnit::TestCase | |
31 | { | |
32 | public: | |
33 | StringTestCase(); | |
34 | ||
35 | private: | |
36 | CPPUNIT_TEST_SUITE( StringTestCase ); | |
37 | CPPUNIT_TEST( String ); | |
38 | CPPUNIT_TEST( PChar ); | |
39 | CPPUNIT_TEST( Format ); | |
40 | CPPUNIT_TEST( Constructors ); | |
ad3fca67 VS |
41 | #if wxUSE_WCHAR_T |
42 | CPPUNIT_TEST( ConstructorsWithConversion ); | |
265d5cce | 43 | CPPUNIT_TEST( Conversion ); |
c20d8682 | 44 | CPPUNIT_TEST( ConversionUTF7 ); |
ec54fe9e VZ |
45 | CPPUNIT_TEST( ConversionUTF8 ); |
46 | #endif // wxUSE_WCHAR_T | |
1cd53e88 VS |
47 | CPPUNIT_TEST( Extraction ); |
48 | CPPUNIT_TEST( Find ); | |
49 | CPPUNIT_TEST( Tokenizer ); | |
76c6fcbc | 50 | CPPUNIT_TEST( TokenizerGetPosition ); |
1cd53e88 VS |
51 | CPPUNIT_TEST( Replace ); |
52 | CPPUNIT_TEST( Match ); | |
bd7f096d | 53 | CPPUNIT_TEST( CaseChanges ); |
dcb68102 RN |
54 | CPPUNIT_TEST( Compare ); |
55 | CPPUNIT_TEST( CompareNoCase ); | |
4f7ee81a VZ |
56 | CPPUNIT_TEST( ToLong ); |
57 | CPPUNIT_TEST( ToULong ); | |
58 | CPPUNIT_TEST( ToDouble ); | |
1cd53e88 VS |
59 | CPPUNIT_TEST_SUITE_END(); |
60 | ||
61 | void String(); | |
62 | void PChar(); | |
63 | void Format(); | |
64 | void Constructors(); | |
ad3fca67 VS |
65 | #if wxUSE_WCHAR_T |
66 | void ConstructorsWithConversion(); | |
265d5cce | 67 | void Conversion(); |
c20d8682 | 68 | void ConversionUTF7(); |
ec54fe9e VZ |
69 | void ConversionUTF8(); |
70 | #endif // wxUSE_WCHAR_T | |
1cd53e88 VS |
71 | void Extraction(); |
72 | void Find(); | |
0a199299 | 73 | void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode ); |
1cd53e88 | 74 | void Tokenizer(); |
76c6fcbc | 75 | void TokenizerGetPosition(); |
1cd53e88 VS |
76 | void Replace(); |
77 | void Match(); | |
bd7f096d | 78 | void CaseChanges(); |
dcb68102 RN |
79 | void Compare(); |
80 | void CompareNoCase(); | |
4f7ee81a VZ |
81 | void ToLong(); |
82 | void ToULong(); | |
83 | void ToDouble(); | |
1cd53e88 | 84 | |
00b34ed7 VZ |
85 | #if wxUSE_WCHAR_T |
86 | // test if converting s using the given encoding gives ws and vice versa | |
87 | // | |
88 | // if either of the first 2 arguments is NULL, the conversion is supposed | |
89 | // to fail | |
90 | void DoTestConversion(const char *s, const wchar_t *w, wxCSConv& conv); | |
91 | #endif // wxUSE_WCHAR_T | |
92 | ||
1cd53e88 VS |
93 | DECLARE_NO_COPY_CLASS(StringTestCase) |
94 | }; | |
95 | ||
96 | // register in the unnamed registry so that these tests are run by default | |
97 | CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); | |
98 | ||
99 | // also include in it's own registry so that these tests can be run alone | |
100 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); | |
101 | ||
102 | StringTestCase::StringTestCase() | |
103 | { | |
104 | } | |
105 | ||
106 | void StringTestCase::String() | |
107 | { | |
108 | wxString a, b, c; | |
109 | ||
110 | a.reserve (128); | |
111 | b.reserve (128); | |
112 | c.reserve (128); | |
113 | ||
114 | for (int i = 0; i < 2; ++i) | |
115 | { | |
116 | a = _T("Hello"); | |
117 | b = _T(" world"); | |
118 | c = _T("! How'ya doin'?"); | |
119 | a += b; | |
120 | a += c; | |
121 | c = _T("Hello world! What's up?"); | |
122 | CPPUNIT_ASSERT( c != a ); | |
123 | } | |
124 | } | |
125 | ||
126 | void StringTestCase::PChar() | |
127 | { | |
128 | wxChar a [128]; | |
129 | wxChar b [128]; | |
130 | wxChar c [128]; | |
131 | ||
132 | for (int i = 0; i < 2; ++i) | |
133 | { | |
134 | wxStrcpy (a, _T("Hello")); | |
135 | wxStrcpy (b, _T(" world")); | |
136 | wxStrcpy (c, _T("! How'ya doin'?")); | |
137 | wxStrcat (a, b); | |
138 | wxStrcat (a, c); | |
139 | wxStrcpy (c, _T("Hello world! What's up?")); | |
140 | CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 ); | |
141 | } | |
142 | } | |
143 | ||
144 | void StringTestCase::Format() | |
145 | { | |
146 | wxString s1,s2; | |
147 | s1.Printf(_T("%03d"), 18); | |
148 | CPPUNIT_ASSERT( s1 == wxString::Format(_T("%03d"), 18) ); | |
149 | s2.Printf(_T("Number 18: %s\n"), s1.c_str()); | |
150 | CPPUNIT_ASSERT( s2 == wxString::Format(_T("Number 18: %s\n"), s1.c_str()) ); | |
151 | } | |
152 | ||
153 | void StringTestCase::Constructors() | |
154 | { | |
155 | #define TEST_CTOR(args, res) \ | |
156 | { \ | |
157 | wxString s args ; \ | |
158 | CPPUNIT_ASSERT( s == res ); \ | |
159 | } | |
160 | ||
161 | TEST_CTOR((_T('Z'), 4), _T("ZZZZ")); | |
162 | TEST_CTOR((_T("Hello"), 4), _T("Hell")); | |
163 | TEST_CTOR((_T("Hello"), 5), _T("Hello")); | |
164 | ||
165 | static const wxChar *s = _T("?really!"); | |
166 | const wxChar *start = wxStrchr(s, _T('r')); | |
167 | const wxChar *end = wxStrchr(s, _T('!')); | |
168 | TEST_CTOR((start, end), _T("really")); | |
169 | } | |
170 | ||
ad3fca67 VS |
171 | #if wxUSE_WCHAR_T |
172 | void StringTestCase::ConstructorsWithConversion() | |
173 | { | |
15e2adbd VZ |
174 |