]>
git.saurik.com Git - wxWidgets.git/blob - tests/mbconv/convautotest.cpp
0c6292f625c83c9741a6943e2aaff5df904c607c
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
56 void TestFirstChar(const char *src
, wchar_t wch
);
67 // test whether two lines of text are converted properly from a stream
68 void TestTextStream(const char *src
,
70 const wxString
& line1
,
71 const wxString
& line2
);
73 void StreamUTF8NoBOM();
81 // register in the unnamed registry so that these tests are run by default
82 CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase
);
84 // also include in it's own registry so that these tests can be run alone
85 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase
, "ConvAutoTestCase");
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 void ConvAutoTestCase::TestFirstChar(const char *src
, wchar_t wch
)
93 wxWCharBuffer wbuf
= wxConvAuto().cMB2WC(src
);
94 CPPUNIT_ASSERT( wbuf
);
95 CPPUNIT_ASSERT_EQUAL( wch
, *wbuf
);
98 void ConvAutoTestCase::Empty()
100 TestFirstChar("", wxT('\0'));
103 void ConvAutoTestCase::Short()
105 TestFirstChar("1", wxT('1'));
108 void ConvAutoTestCase::None()
110 TestFirstChar("Hello world", wxT('H'));
113 void ConvAutoTestCase::UTF32LE()
115 TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'));
118 void ConvAutoTestCase::UTF32BE()
120 TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'));
123 void ConvAutoTestCase::UTF16LE()
125 TestFirstChar("\xff\xfeZ\0", wxT('Z'));
128 void ConvAutoTestCase::UTF16BE()
130 TestFirstChar("\xfe\xff\0Y", wxT('Y'));
133 void ConvAutoTestCase::UTF8()
135 #ifdef wxHAVE_U_ESCAPE
136 TestFirstChar("\xef\xbb\xbf\xd0\x9f", L
'\u041f');
140 void ConvAutoTestCase::TestTextStream(const char *src
,
142 const wxString
& line1
,
143 const wxString
& line2
)
145 wxMemoryInputStream
instream(src
, srclength
);
146 wxTextInputStream
text(instream
);
148 CPPUNIT_ASSERT_EQUAL( line1
, text
.ReadLine() );
149 CPPUNIT_ASSERT_EQUAL( line2
, text
.ReadLine() );
152 // the first line of the teststring used in the following functions is an
153 // 'a' followed by a Japanese hiragana A (u+3042).
154 // The second line is a single Greek beta (u+03B2). There is no blank line
160 const wxString line1
= wxString::FromUTF8("a\xe3\x81\x82");
161 const wxString line2
= wxString::FromUTF8("\xce\xb2");
163 } // anonymous namespace
165 void ConvAutoTestCase::StreamUTF8NoBOM()
167 // currently this test doesn't work because without the BOM wxConvAuto
168 // decides that the string is in Latin-1 after finding the first (but not
169 // the two subsequent ones which are part of the same UTF-8 sequence!)
172 // FIXME: we need to fix this at wxTextInputStream level, see #11570
174 TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
179 void ConvAutoTestCase::StreamUTF8()
181 TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
185 void ConvAutoTestCase::StreamUTF16LE()
187 TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
191 void ConvAutoTestCase::StreamUTF16BE()
193 TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
197 void ConvAutoTestCase::StreamUTF32LE()
199 TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
200 "\x00\0\0\xB2\x03\0\0",
204 void ConvAutoTestCase::StreamUTF32BE()
206 TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
211 #endif // wxUSE_WCHAR_T