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