]> git.saurik.com Git - wxWidgets.git/blob - tests/mbconv/convautotest.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / tests / mbconv / convautotest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/mbconv/convauto.cpp
3 // Purpose: wxConvAuto unit test
4 // Author: Vadim Zeitlin
5 // Created: 2006-04-04
6 // Copyright: (c) 2006 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_UNICODE
20
21 #include "wx/convauto.h"
22
23 #include "wx/mstream.h"
24 #include "wx/txtstrm.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class ConvAutoTestCase : public CppUnit::TestCase
31 {
32 public:
33 ConvAutoTestCase() { }
34
35 private:
36 CPPUNIT_TEST_SUITE( ConvAutoTestCase );
37 CPPUNIT_TEST( Empty );
38 CPPUNIT_TEST( Short );
39 CPPUNIT_TEST( None );
40 CPPUNIT_TEST( UTF32LE );
41 CPPUNIT_TEST( UTF32BE );
42 CPPUNIT_TEST( UTF16LE );
43 CPPUNIT_TEST( UTF16BE );
44 CPPUNIT_TEST( UTF8 );
45 CPPUNIT_TEST( StreamUTF8NoBOM );
46 CPPUNIT_TEST( StreamUTF8 );
47 CPPUNIT_TEST( StreamUTF16LE );
48 CPPUNIT_TEST( StreamUTF16BE );
49 CPPUNIT_TEST( StreamUTF32LE );
50 CPPUNIT_TEST( StreamUTF32BE );
51 CPPUNIT_TEST_SUITE_END();
52
53 // real test function: check that converting the src multibyte string to
54 // wide char using wxConvAuto yields wch as the first result
55 //
56 // the length of the string may need to be passed explicitly if it has
57 // embedded NULs, otherwise it's not necessary
58 void TestFirstChar(const char *src, wchar_t wch, size_t len = wxNO_LEN);
59
60 void Empty();
61 void Short();
62 void None();
63 void UTF32LE();
64 void UTF32BE();
65 void UTF16LE();
66 void UTF16BE();
67 void UTF8();
68
69 // test whether two lines of text are converted properly from a stream
70 void TestTextStream(const char *src,
71 size_t srclength,
72 const wxString& line1,
73 const wxString& line2);
74
75 void StreamUTF8NoBOM();
76 void StreamUTF8();
77 void StreamUTF16LE();
78 void StreamUTF16BE();
79 void StreamUTF32LE();
80 void StreamUTF32BE();
81 };
82
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase);
85
86 // also include in its own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase, "ConvAutoTestCase");
88
89 // ----------------------------------------------------------------------------
90 // tests
91 // ----------------------------------------------------------------------------
92
93 void ConvAutoTestCase::TestFirstChar(const char *src, wchar_t wch, size_t len)
94 {
95 wxWCharBuffer wbuf = wxConvAuto().cMB2WC(src, len, NULL);
96 CPPUNIT_ASSERT( wbuf );
97 CPPUNIT_ASSERT_EQUAL( wch, *wbuf );
98 }
99
100 void ConvAutoTestCase::Empty()
101 {
102 CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
103 }
104
105 void ConvAutoTestCase::Short()
106 {
107 TestFirstChar("1", wxT('1'));
108 }
109
110 void ConvAutoTestCase::None()
111 {
112 TestFirstChar("Hello world", wxT('H'));
113 }
114
115 void ConvAutoTestCase::UTF32LE()
116 {
117 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
118 }
119
120 void ConvAutoTestCase::UTF32BE()
121 {
122 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
123 }
124
125 void ConvAutoTestCase::UTF16LE()
126 {
127 TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
128 }
129
130 void ConvAutoTestCase::UTF16BE()
131 {
132 TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
133 }
134
135 void ConvAutoTestCase::UTF8()
136 {
137 #ifdef wxHAVE_U_ESCAPE
138 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L'\u041f');
139 #endif
140 }
141
142 void ConvAutoTestCase::TestTextStream(const char *src,
143 size_t srclength,
144 const wxString& line1,
145 const wxString& line2)
146 {
147 wxMemoryInputStream instream(src, srclength);
148 wxTextInputStream text(instream);
149
150 CPPUNIT_ASSERT_EQUAL( line1, text.ReadLine() );
151 CPPUNIT_ASSERT_EQUAL( line2, text.ReadLine() );
152 }
153
154 // the first line of the teststring used in the following functions is an
155 // 'a' followed by a Japanese hiragana A (u+3042).
156 // The second line is a single Greek beta (u+03B2). There is no blank line
157 // at the end.
158
159 namespace
160 {
161
162 const wxString line1 = wxString::FromUTF8("a\xe3\x81\x82");
163 const wxString line2 = wxString::FromUTF8("\xce\xb2");
164
165 } // anonymous namespace
166
167 void ConvAutoTestCase::StreamUTF8NoBOM()
168 {
169 // currently this test doesn't work because without the BOM wxConvAuto
170 // decides that the string is in Latin-1 after finding the first (but not
171 // the two subsequent ones which are part of the same UTF-8 sequence!)
172 // 8-bit character
173 //
174 // FIXME: we need to fix this at wxTextInputStream level, see #11570
175 #if 0
176 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
177 7, line1, line2);
178 #endif
179 }
180
181 void ConvAutoTestCase::StreamUTF8()
182 {
183 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
184 10, line1, line2);
185 }
186
187 void ConvAutoTestCase::StreamUTF16LE()
188 {
189 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
190 10, line1, line2);
191 }
192
193 void ConvAutoTestCase::StreamUTF16BE()
194 {
195 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
196 10, line1, line2);
197 }
198
199 void ConvAutoTestCase::StreamUTF32LE()
200 {
201 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
202 "\x00\0\0\xB2\x03\0\0",
203 20, line1, line2);
204 }
205
206 void ConvAutoTestCase::StreamUTF32BE()
207 {
208 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
209 "\0\0\x03\xB2",
210 20, line1, line2);
211 }
212
213 #endif // wxUSE_UNICODE