]>
git.saurik.com Git - wxWidgets.git/blob - tests/mbconv/convautotest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/mbconv/convauto.cpp
3 // Purpose: wxConvAuto unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
22 #include "wx/convauto.h"
24 #include "wx/mstream.h"
25 #include "wx/txtstrm.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class ConvAutoTestCase
: public CppUnit::TestCase
34 ConvAutoTestCase() { }
37 CPPUNIT_TEST_SUITE( ConvAutoTestCase
);
38 CPPUNIT_TEST( Empty
);
39 CPPUNIT_TEST( Short
);
41 CPPUNIT_TEST( UTF32LE
);
42 CPPUNIT_TEST( UTF32BE
);
43 CPPUNIT_TEST( UTF16LE
);
44 CPPUNIT_TEST( UTF16BE
);
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();
54 // real test function: check that converting the src multibyte string to
55 // wide char using wxConvAuto yields wch as the first result
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
);
70 // test whether two lines of text are converted properly from a stream
71 void TestTextStream(const char *src
,
73 const wxString
& line1
,
74 const wxString
& line2
);
76 void StreamUTF8NoBOM();
84 // register in the unnamed registry so that these tests are run by default
85 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase
);
87 // also include in it's own registry so that these tests can be run alone
88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase
, "ConvAutoTestCase");
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 void ConvAutoTestCase::TestFirstChar(const char *src
, wchar_t wch
, size_t len
)
96 wxWCharBuffer wbuf
= wxConvAuto().cMB2WC(src
, len
, NULL
);
97 CPPUNIT_ASSERT( wbuf
);
98 CPPUNIT_ASSERT_EQUAL( wch
, *wbuf
);
101 void ConvAutoTestCase::Empty()
103 CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
106 void ConvAutoTestCase::Short()
108 TestFirstChar("1", wxT('1'));
111 void ConvAutoTestCase::None()
113 TestFirstChar("Hello world", wxT('H'));
116 void ConvAutoTestCase::UTF32LE()
118 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
121 void ConvAutoTestCase::UTF32BE()
123 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
126 void ConvAutoTestCase::UTF16LE()
128 TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
131 void ConvAutoTestCase::UTF16BE()
133 TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
136 void ConvAutoTestCase::UTF8()
138 #ifdef wxHAVE_U_ESCAPE
139 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L
'\u041f');
143 void ConvAutoTestCase::TestTextStream(const char *src
,
145 const wxString
& line1
,
146 const wxString
& line2
)
148 wxMemoryInputStream
instream(src
, srclength
);
149 wxTextInputStream
text(instream
);
151 CPPUNIT_ASSERT_EQUAL( line1
, text
.ReadLine() );
152 CPPUNIT_ASSERT_EQUAL( line2
, text
.ReadLine() );
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
163 const wxString line1
= wxString::FromUTF8("a\xe3\x81\x82");
164 const wxString line2
= wxString::FromUTF8("\xce\xb2");
166 } // anonymous namespace
168 void ConvAutoTestCase::StreamUTF8NoBOM()
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!)
175 // FIXME: we need to fix this at wxTextInputStream level, see #11570
177 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
182 void ConvAutoTestCase::StreamUTF8()
184 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
188 void ConvAutoTestCase::StreamUTF16LE()
190 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
194 void ConvAutoTestCase::StreamUTF16BE()
196 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
200 void ConvAutoTestCase::StreamUTF32LE()
202 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
203 "\x00\0\0\xB2\x03\0\0",
207 void ConvAutoTestCase::StreamUTF32BE()
209 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
214 #endif // wxUSE_UNICODE