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