Correct wxConvAuto::ToWChar() behaviour with wxNO_LEN input size.
[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 //
55 // the length of the string may need to be passed explicitly if it has
56 // embedded NULs, otherwise it's not necessary
57 void TestFirstChar(const char *src, wchar_t wch, int len = wxNO_LEN);
58
59 void Empty();
60 void Short();
61 void None();
62 void UTF32LE();
63 void UTF32BE();
64 void UTF16LE();
65 void UTF16BE();
66 void UTF8();
67
68 // test whether two lines of text are converted properly from a stream
69 void TestTextStream(const char *src,
70 size_t srclength,
71 const wxString& line1,
72 const wxString& line2);
73
74 void StreamUTF8NoBOM();
75 void StreamUTF8();
76 void StreamUTF16LE();
77 void StreamUTF16BE();
78 void StreamUTF32LE();
79 void StreamUTF32BE();
80 };
81
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase);
84
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase, "ConvAutoTestCase");
87
88 // ----------------------------------------------------------------------------
89 // tests
90 // ----------------------------------------------------------------------------
91
92 void ConvAutoTestCase::TestFirstChar(const char *src, wchar_t wch, int len)
93 {
94 wxWCharBuffer wbuf = wxConvAuto().cMB2WC(src, len, NULL);
95 CPPUNIT_ASSERT( wbuf );
96 CPPUNIT_ASSERT_EQUAL( wch, *wbuf );
97 }
98
99 void ConvAutoTestCase::Empty()
100 {
101 CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
102 }
103
104 void ConvAutoTestCase::Short()
105 {
106 TestFirstChar("1", wxT('1'));
107 }
108
109 void ConvAutoTestCase::None()
110 {
111 TestFirstChar("Hello world", wxT('H'));
112 }
113
114 void ConvAutoTestCase::UTF32LE()
115 {
116 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
117 }
118
119 void ConvAutoTestCase::UTF32BE()
120 {
121 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
122 }
123
124 void ConvAutoTestCase::UTF16LE()
125 {
126 TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
127 }
128
129 void ConvAutoTestCase::UTF16BE()
130 {
131 TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
132 }
133
134 void ConvAutoTestCase::UTF8()
135 {
136 #ifdef wxHAVE_U_ESCAPE
137 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L'\u041f');
138 #endif
139 }
140
141 void ConvAutoTestCase::TestTextStream(const char *src,
142 size_t srclength,
143 const wxString& line1,
144 const wxString& line2)
145 {
146 wxMemoryInputStream instream(src, srclength);
147 wxTextInputStream text(instream);
148
149 CPPUNIT_ASSERT_EQUAL( line1, text.ReadLine() );
150 CPPUNIT_ASSERT_EQUAL( line2, text.ReadLine() );
151 }
152
153 // the first line of the teststring used in the following functions is an
154 // 'a' followed by a Japanese hiragana A (u+3042).
155 // The second line is a single Greek beta (u+03B2). There is no blank line
156 // at the end.
157
158 namespace
159 {
160
161 const wxString line1 = wxString::FromUTF8("a\xe3\x81\x82");
162 const wxString line2 = wxString::FromUTF8("\xce\xb2");
163
164 } // anonymous namespace
165
166 void ConvAutoTestCase::StreamUTF8NoBOM()
167 {
168 // currently this test doesn't work because without the BOM wxConvAuto
169 // decides that the string is in Latin-1 after finding the first (but not
170 // the two subsequent ones which are part of the same UTF-8 sequence!)
171 // 8-bit character
172 //
173 // FIXME: we need to fix this at wxTextInputStream level, see #11570
174 #if 0
175 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
176 7, line1, line2);
177 #endif
178 }
179
180 void ConvAutoTestCase::StreamUTF8()
181 {
182 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
183 10, line1, line2);
184 }
185
186 void ConvAutoTestCase::StreamUTF16LE()
187 {
188 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
189 10, line1, line2);
190 }
191
192 void ConvAutoTestCase::StreamUTF16BE()
193 {
194 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
195 10, line1, line2);
196 }
197
198 void ConvAutoTestCase::StreamUTF32LE()
199 {
200 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
201 "\x00\0\0\xB2\x03\0\0",
202 20, line1, line2);
203 }
204
205 void ConvAutoTestCase::StreamUTF32BE()
206 {
207 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
208 "\0\0\x03\xB2",
209 20, line1, line2);
210 }