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