]>
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 // ----------------------------------------------------------------------------
20 #include "wx/convauto.h"
22 #include "wx/mstream.h"
23 #include "wx/txtstrm.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 class ConvAutoTestCase
: public CppUnit::TestCase
32 ConvAutoTestCase() { }
35 CPPUNIT_TEST_SUITE( ConvAutoTestCase
);
36 CPPUNIT_TEST( Empty
);
37 CPPUNIT_TEST( Short
);
39 CPPUNIT_TEST( UTF32LE
);
40 CPPUNIT_TEST( UTF32BE
);
41 CPPUNIT_TEST( UTF16LE
);
42 CPPUNIT_TEST( UTF16BE
);
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();
52 // real test function: check that converting the src multibyte string to
53 // wide char using wxConvAuto yields wch as the first result
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
);
68 // test whether two lines of text are converted properly from a stream
69 void TestTextStream(const char *src
,
71 const wxString
& line1
,
72 const wxString
& line2
);
74 void StreamUTF8NoBOM();
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase
);
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase
, "ConvAutoTestCase");
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 void ConvAutoTestCase::TestFirstChar(const char *src
, wchar_t wch
, int len
)
94 wxWCharBuffer wbuf
= wxConvAuto().cMB2WC(src
, len
, NULL
);
95 CPPUNIT_ASSERT( wbuf
);
96 CPPUNIT_ASSERT_EQUAL( wch
, *wbuf
);
99 void ConvAutoTestCase::Empty()
101 CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
104 void ConvAutoTestCase::Short()
106 TestFirstChar("1", wxT('1'));
109 void ConvAutoTestCase::None()
111 TestFirstChar("Hello world", wxT('H'));
114 void ConvAutoTestCase::UTF32LE()
116 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
119 void ConvAutoTestCase::UTF32BE()
121 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
124 void ConvAutoTestCase::UTF16LE()
126 TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
129 void ConvAutoTestCase::UTF16BE()
131 TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
134 void ConvAutoTestCase::UTF8()
136 #ifdef wxHAVE_U_ESCAPE
137 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L
'\u041f');
141 void ConvAutoTestCase::TestTextStream(const char *src
,
143 const wxString
& line1
,
144 const wxString
& line2
)
146 wxMemoryInputStream
instream(src
, srclength
);
147 wxTextInputStream
text(instream
);
149 CPPUNIT_ASSERT_EQUAL( line1
, text
.ReadLine() );
150 CPPUNIT_ASSERT_EQUAL( line2
, text
.ReadLine() );
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
161 const wxString line1
= wxString::FromUTF8("a\xe3\x81\x82");
162 const wxString line2
= wxString::FromUTF8("\xce\xb2");
164 } // anonymous namespace
166 void ConvAutoTestCase::StreamUTF8NoBOM()
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!)
173 // FIXME: we need to fix this at wxTextInputStream level, see #11570
175 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
180 void ConvAutoTestCase::StreamUTF8()
182 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
186 void ConvAutoTestCase::StreamUTF16LE()
188 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
192 void ConvAutoTestCase::StreamUTF16BE()
194 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
198 void ConvAutoTestCase::StreamUTF32LE()
200 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
201 "\x00\0\0\xB2\x03\0\0",
205 void ConvAutoTestCase::StreamUTF32BE()
207 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"