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