2006-04-05 10:38:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/mbconv/convauto.cpp
|
|
|
|
// Purpose: wxConvAuto unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2006-04-04
|
|
|
|
// Copyright: (c) 2006 Vadim Zeitlin
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2010-11-10 08:53:49 -05:00
|
|
|
#if wxUSE_UNICODE
|
|
|
|
|
2006-04-05 10:38:42 -04:00
|
|
|
#include "wx/convauto.h"
|
|
|
|
|
2010-01-04 07:22:49 -05:00
|
|
|
#include "wx/mstream.h"
|
|
|
|
#include "wx/txtstrm.h"
|
|
|
|
|
2006-04-05 10:38:42 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ConvAutoTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConvAutoTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( ConvAutoTestCase );
|
|
|
|
CPPUNIT_TEST( Empty );
|
|
|
|
CPPUNIT_TEST( Short );
|
|
|
|
CPPUNIT_TEST( None );
|
|
|
|
CPPUNIT_TEST( UTF32LE );
|
|
|
|
CPPUNIT_TEST( UTF32BE );
|
|
|
|
CPPUNIT_TEST( UTF16LE );
|
|
|
|
CPPUNIT_TEST( UTF16BE );
|
|
|
|
CPPUNIT_TEST( UTF8 );
|
2010-01-04 07:22:49 -05:00
|
|
|
CPPUNIT_TEST( StreamUTF8NoBOM );
|
|
|
|
CPPUNIT_TEST( StreamUTF8 );
|
|
|
|
CPPUNIT_TEST( StreamUTF16LE );
|
|
|
|
CPPUNIT_TEST( StreamUTF16BE );
|
|
|
|
CPPUNIT_TEST( StreamUTF32LE );
|
|
|
|
CPPUNIT_TEST( StreamUTF32BE );
|
2006-04-05 10:38:42 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
// real test function: check that converting the src multibyte string to
|
|
|
|
// wide char using wxConvAuto yields wch as the first result
|
2010-10-03 13:15:18 -04:00
|
|
|
//
|
|
|
|
// the length of the string may need to be passed explicitly if it has
|
|
|
|
// embedded NULs, otherwise it's not necessary
|
2010-10-17 09:59:55 -04:00
|
|
|
void TestFirstChar(const char *src, wchar_t wch, size_t len = wxNO_LEN);
|
2006-04-05 10:38:42 -04:00
|
|
|
|
|
|
|
void Empty();
|
|
|
|
void Short();
|
|
|
|
void None();
|
|
|
|
void UTF32LE();
|
|
|
|
void UTF32BE();
|
|
|
|
void UTF16LE();
|
|
|
|
void UTF16BE();
|
|
|
|
void UTF8();
|
2010-01-04 07:22:49 -05:00
|
|
|
|
|
|
|
// test whether two lines of text are converted properly from a stream
|
|
|
|
void TestTextStream(const char *src,
|
|
|
|
size_t srclength,
|
|
|
|
const wxString& line1,
|
|
|
|
const wxString& line2);
|
|
|
|
|
|
|
|
void StreamUTF8NoBOM();
|
|
|
|
void StreamUTF8();
|
|
|
|
void StreamUTF16LE();
|
|
|
|
void StreamUTF16BE();
|
|
|
|
void StreamUTF32LE();
|
|
|
|
void StreamUTF32BE();
|
2006-04-05 10:38:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase);
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2007-06-28 03:54:15 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase, "ConvAutoTestCase");
|
2006-04-05 10:38:42 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// tests
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-10-17 09:59:55 -04:00
|
|
|
void ConvAutoTestCase::TestFirstChar(const char *src, wchar_t wch, size_t len)
|
2006-04-05 10:38:42 -04:00
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
wxWCharBuffer wbuf = wxConvAuto().cMB2WC(src, len, NULL);
|
2006-04-05 10:38:42 -04:00
|
|
|
CPPUNIT_ASSERT( wbuf );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wch, *wbuf );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::Empty()
|
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::Short()
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
TestFirstChar("1", wxT('1'));
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::None()
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
TestFirstChar("Hello world", wxT('H'));
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::UTF32LE()
|
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::UTF32BE()
|
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::UTF16LE()
|
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::UTF16BE()
|
|
|
|
{
|
2010-10-03 13:15:18 -04:00
|
|
|
TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
|
2006-04-05 10:38:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::UTF8()
|
|
|
|
{
|
|
|
|
#ifdef wxHAVE_U_ESCAPE
|
|
|
|
TestFirstChar("\xef\xbb\xbf\xd0\x9f", L'\u041f');
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-04 07:22:49 -05:00
|
|
|
void ConvAutoTestCase::TestTextStream(const char *src,
|
|
|
|
size_t srclength,
|
|
|
|
const wxString& line1,
|
|
|
|
const wxString& line2)
|
|
|
|
{
|
|
|
|
wxMemoryInputStream instream(src, srclength);
|
|
|
|
wxTextInputStream text(instream);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( line1, text.ReadLine() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( line2, text.ReadLine() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// the first line of the teststring used in the following functions is an
|
|
|
|
// 'a' followed by a Japanese hiragana A (u+3042).
|
|
|
|
// The second line is a single Greek beta (u+03B2). There is no blank line
|
|
|
|
// at the end.
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
const wxString line1 = wxString::FromUTF8("a\xe3\x81\x82");
|
|
|
|
const wxString line2 = wxString::FromUTF8("\xce\xb2");
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF8NoBOM()
|
|
|
|
{
|
|
|
|
// currently this test doesn't work because without the BOM wxConvAuto
|
|
|
|
// decides that the string is in Latin-1 after finding the first (but not
|
|
|
|
// the two subsequent ones which are part of the same UTF-8 sequence!)
|
|
|
|
// 8-bit character
|
|
|
|
//
|
|
|
|
// FIXME: we need to fix this at wxTextInputStream level, see #11570
|
|
|
|
#if 0
|
|
|
|
TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
|
|
|
|
7, line1, line2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF8()
|
|
|
|
{
|
|
|
|
TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
|
|
|
|
10, line1, line2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF16LE()
|
|
|
|
{
|
|
|
|
TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
|
|
|
|
10, line1, line2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF16BE()
|
|
|
|
{
|
|
|
|
TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
|
|
|
|
10, line1, line2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF32LE()
|
|
|
|
{
|
|
|
|
TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
|
|
|
|
"\x00\0\0\xB2\x03\0\0",
|
|
|
|
20, line1, line2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvAutoTestCase::StreamUTF32BE()
|
|
|
|
{
|
|
|
|
TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
|
|
|
|
"\0\0\x03\xB2",
|
|
|
|
20, line1, line2);
|
|
|
|
}
|
2010-11-10 08:53:49 -05:00
|
|
|
|
|
|
|
#endif // wxUSE_UNICODE
|