]>
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
6 // Copyright: (c) 2006 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
21 #include "wx/convauto.h"
23 #include "wx/mstream.h"
24 #include "wx/txtstrm.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class ConvAutoTestCase
: public CppUnit::TestCase
33 ConvAutoTestCase() { }
36 CPPUNIT_TEST_SUITE( ConvAutoTestCase
);
37 CPPUNIT_TEST( Empty
);
38 CPPUNIT_TEST( Short
);
40 CPPUNIT_TEST( UTF32LE
);
41 CPPUNIT_TEST( UTF32BE
);
42 CPPUNIT_TEST( UTF16LE
);
43 CPPUNIT_TEST( UTF16BE
);
45 CPPUNIT_TEST( StreamUTF8NoBOM
);
46 CPPUNIT_TEST( StreamUTF8
);
47 CPPUNIT_TEST( StreamUTF16LE
);
48 CPPUNIT_TEST( StreamUTF16BE
);
49 CPPUNIT_TEST( StreamUTF32LE
);
50 CPPUNIT_TEST( StreamUTF32BE
);
51 CPPUNIT_TEST_SUITE_END();
53 // real test function: check that converting the src multibyte string to
54 // wide char using wxConvAuto yields wch as the first result
56 // the length of the string may need to be passed explicitly if it has
57 // embedded NULs, otherwise it's not necessary
58 void TestFirstChar(const char *src
, wchar_t wch
, size_t len
= wxNO_LEN
);
69 // test whether two lines of text are converted properly from a stream
70 void TestTextStream(const char *src
,
72 const wxString
& line1
,
73 const wxString
& line2
);
75 void StreamUTF8NoBOM();
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase
);
86 // also include in its own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase
, "ConvAutoTestCase");
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 void ConvAutoTestCase::TestFirstChar(const char *src
, wchar_t wch
, size_t len
)
95 wxWCharBuffer wbuf
= wxConvAuto().cMB2WC(src
, len
, NULL
);
96 CPPUNIT_ASSERT( wbuf
);
97 CPPUNIT_ASSERT_EQUAL( wch
, *wbuf
);
100 void ConvAutoTestCase::Empty()
102 CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
105 void ConvAutoTestCase::Short()
107 TestFirstChar("1", wxT('1'));
110 void ConvAutoTestCase::None()
112 TestFirstChar("Hello world", wxT('H'));
115 void ConvAutoTestCase::UTF32LE()
117 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
120 void ConvAutoTestCase::UTF32BE()
122 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
125 void ConvAutoTestCase::UTF16LE()
127 TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
130 void ConvAutoTestCase::UTF16BE()
132 TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
135 void ConvAutoTestCase::UTF8()
137 #ifdef wxHAVE_U_ESCAPE
138 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L
'\u041f');
142 void ConvAutoTestCase::TestTextStream(const char *src
,
144 const wxString
& line1
,
145 const wxString
& line2
)
147 wxMemoryInputStream
instream(src
, srclength
);
148 wxTextInputStream
text(instream
);
150 CPPUNIT_ASSERT_EQUAL( line1
, text
.ReadLine() );
151 CPPUNIT_ASSERT_EQUAL( line2
, text
.ReadLine() );
154 // the first line of the teststring used in the following functions is an
155 // 'a' followed by a Japanese hiragana A (u+3042).
156 // The second line is a single Greek beta (u+03B2). There is no blank line
162 const wxString line1
= wxString::FromUTF8("a\xe3\x81\x82");
163 const wxString line2
= wxString::FromUTF8("\xce\xb2");
165 } // anonymous namespace
167 void ConvAutoTestCase::StreamUTF8NoBOM()
169 // currently this test doesn't work because without the BOM wxConvAuto
170 // decides that the string is in Latin-1 after finding the first (but not
171 // the two subsequent ones which are part of the same UTF-8 sequence!)
174 // FIXME: we need to fix this at wxTextInputStream level, see #11570
176 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
181 void ConvAutoTestCase::StreamUTF8()
183 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
187 void ConvAutoTestCase::StreamUTF16LE()
189 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
193 void ConvAutoTestCase::StreamUTF16BE()
195 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
199 void ConvAutoTestCase::StreamUTF32LE()
201 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
202 "\x00\0\0\xB2\x03\0\0",
206 void ConvAutoTestCase::StreamUTF32BE()
208 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
213 #endif // wxUSE_UNICODE