]>
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
54 void TestFirstChar(const char *src
, wchar_t wch
);
65 // test whether two lines of text are converted properly from a stream
66 void TestTextStream(const char *src
,
68 const wxString
& line1
,
69 const wxString
& line2
);
71 void StreamUTF8NoBOM();
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase
);
82 // also include in it's own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase
, "ConvAutoTestCase");
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 void ConvAutoTestCase::TestFirstChar(const char *src
, wchar_t wch
)
91 wxWCharBuffer wbuf
= wxConvAuto().cMB2WC(src
);
92 CPPUNIT_ASSERT( wbuf
);
93 CPPUNIT_ASSERT_EQUAL( wch
, *wbuf
);
96 void ConvAutoTestCase::Empty()
98 TestFirstChar("", wxT('\0'));
101 void ConvAutoTestCase::Short()
103 TestFirstChar("1", wxT('1'));
106 void ConvAutoTestCase::None()
108 TestFirstChar("Hello world", wxT('H'));
111 void ConvAutoTestCase::UTF32LE()
113 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'));
116 void ConvAutoTestCase::UTF32BE()
118 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'));
121 void ConvAutoTestCase::UTF16LE()
123 TestFirstChar("\xff\xfeZ\0", wxT('Z'));
126 void ConvAutoTestCase::UTF16BE()
128 TestFirstChar("\xfe\xff\0Y", wxT('Y'));
131 void ConvAutoTestCase::UTF8()
133 #ifdef wxHAVE_U_ESCAPE
134 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L
'\u041f');
138 void ConvAutoTestCase::TestTextStream(const char *src
,
140 const wxString
& line1
,
141 const wxString
& line2
)
143 wxMemoryInputStream
instream(src
, srclength
);
144 wxTextInputStream
text(instream
);
146 CPPUNIT_ASSERT_EQUAL( line1
, text
.ReadLine() );
147 CPPUNIT_ASSERT_EQUAL( line2
, text
.ReadLine() );
150 // the first line of the teststring used in the following functions is an
151 // 'a' followed by a Japanese hiragana A (u+3042).
152 // The second line is a single Greek beta (u+03B2). There is no blank line
158 const wxString line1
= wxString::FromUTF8("a\xe3\x81\x82");
159 const wxString line2
= wxString::FromUTF8("\xce\xb2");
161 } // anonymous namespace
163 void ConvAutoTestCase::StreamUTF8NoBOM()
165 // currently this test doesn't work because without the BOM wxConvAuto
166 // decides that the string is in Latin-1 after finding the first (but not
167 // the two subsequent ones which are part of the same UTF-8 sequence!)
170 // FIXME: we need to fix this at wxTextInputStream level, see #11570
172 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
177 void ConvAutoTestCase::StreamUTF8()
179 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
183 void ConvAutoTestCase::StreamUTF16LE()
185 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
189 void ConvAutoTestCase::StreamUTF16BE()
191 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
195 void ConvAutoTestCase::StreamUTF32LE()
197 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
198 "\x00\0\0\xB2\x03\0\0",
202 void ConvAutoTestCase::StreamUTF32BE()
204 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"