X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fec42c21ef127a74a13564de7736b6a48479f6c4..5dec941a5ba3c08870ab4b415dd6e5be368fa0f0:/tests/mbconv/mbconvtest.cpp?ds=inline diff --git a/tests/mbconv/mbconvtest.cpp b/tests/mbconv/mbconvtest.cpp index b5d0185467..93f3ea020f 100644 --- a/tests/mbconv/mbconvtest.cpp +++ b/tests/mbconv/mbconvtest.cpp @@ -80,6 +80,8 @@ private: CPPUNIT_TEST( IconvTests ); CPPUNIT_TEST( Latin1Tests ); CPPUNIT_TEST( FontmapTests ); + CPPUNIT_TEST( BufSize ); + CPPUNIT_TEST( FromWCharTests ); #ifdef HAVE_WCHAR_H CPPUNIT_TEST( UTF8_41 ); CPPUNIT_TEST( UTF8_7f ); @@ -113,6 +115,8 @@ private: void CP1252Tests(); void LibcTests(); void FontmapTests(); + void BufSize(); + void FromWCharTests(); void IconvTests(); void Latin1Tests(); @@ -213,7 +217,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( MBConvTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MBConvTestCase, "MBConvTestCase" ); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MBConvTestCase, "MBConv" ); @@ -252,22 +256,22 @@ wxString CByteArrayFormat( const void* data, size_t len, const wxChar* name ) const unsigned char* bytes = (unsigned char*)data; wxString result; - result.Printf( _T("const static unsigned char %s[%i] = \n{"), name, (int)len ); + result.Printf( wxT("static const unsigned char %s[%i] = \n{"), name, (int)len ); for ( size_t i = 0; i < len; i++ ) { if ( i != 0 ) { - result.append( _T(",") ); + result.append( wxT(",") ); } if ((i%16)==0) { - result.append( _T("\n ") ); + result.append( wxT("\n ") ); } - wxString byte = wxString::Format( _T("0x%02x"), bytes[i] ); + wxString byte = wxString::Format( wxT("0x%02x"), bytes[i] ); result.append(byte); } - result.append( _T("\n};\n") ); + result.append( wxT("\n};\n") ); return result; } @@ -275,7 +279,7 @@ wxString CByteArrayFormat( const void* data, size_t len, const wxChar* name ) // characters, encoded in several different formats. // encoded by iconv -const static unsigned char welcome_utf7_iconv[84] = +static const unsigned char welcome_utf7_iconv[84] = { 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63, 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65, @@ -285,7 +289,7 @@ const static unsigned char welcome_utf7_iconv[84] = 0x57,0x54,0x41,0x43 }; // encoded by wxWindows (iconv can decode this successfully) -const static unsigned char welcome_utf7_wx[109] = +static const unsigned char welcome_utf7_wx[109] = { 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x2b,0x41,0x43,0x41,0x2d,0x74,0x6f,0x2b,0x41, 0x43,0x41,0x2d,0x6f,0x75,0x72,0x2b,0x41,0x43,0x41,0x2d,0x63,0x79,0x62,0x65,0x72, @@ -296,7 +300,7 @@ const static unsigned char welcome_utf7_wx[109] = 0x51,0x77,0x52,0x44,0x42,0x6e,0x4d,0x46,0x6b,0x77,0x41,0x67,0x2d }; // encoded by iconv -const static unsigned char welcome_utf8[89] = +static const unsigned char welcome_utf8[89] = { 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63, 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65, @@ -306,7 +310,7 @@ const static unsigned char welcome_utf8[89] = 0xe3,0x81,0xa7,0xe3,0x81,0x99,0xe3,0x80,0x82 }; // encoded by iconv -const static unsigned char welcome_utf16le[106] = +static const unsigned char welcome_utf16le[106] = { 0x57,0x00,0x65,0x00,0x6c,0x00,0x63,0x00,0x6f,0x00,0x6d,0x00,0x65,0x00,0x20,0x00, 0x74,0x00,0x6f,0x00,0x20,0x00,0x6f,0x00,0x75,0x00,0x72,0x00,0x20,0x00,0x63,0x00, @@ -317,7 +321,7 @@ const static unsigned char welcome_utf16le[106] = 0x44,0x30,0x44,0x30,0x67,0x30,0x59,0x30,0x02,0x30 }; // encoded by iconv -const static unsigned char welcome_utf16be[106] = +static const unsigned char welcome_utf16be[106] = { 0x00,0x57,0x00,0x65,0x00,0x6c,0x00,0x63,0x00,0x6f,0x00,0x6d,0x00,0x65,0x00,0x20, 0x00,0x74,0x00,0x6f,0x00,0x20,0x00,0x6f,0x00,0x75,0x00,0x72,0x00,0x20,0x00,0x63, @@ -328,7 +332,7 @@ const static unsigned char welcome_utf16be[106] = 0x30,0x44,0x30,0x44,0x30,0x67,0x30,0x59,0x30,0x02 }; // encoded by iconv -const static unsigned char welcome_utf32le[212] = +static const unsigned char welcome_utf32le[212] = { 0x57,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x63,0x00,0x00,0x00, 0x6f,0x00,0x00,0x00,0x6d,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20,0x00,0x00,0x00, @@ -346,7 +350,7 @@ const static unsigned char welcome_utf32le[212] = 0x02,0x30,0x00,0x00 }; // encoded by iconv -const static unsigned char welcome_utf32be[212] = +static const unsigned char welcome_utf32be[212] = { 0x00,0x00,0x00,0x57,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x6c,0x00,0x00,0x00,0x63, 0x00,0x00,0x00,0x6f,0x00,0x00,0x00,0x6d,0x00,0x00,0x00,0x65,0x00,0x00,0x00,0x20, @@ -364,7 +368,7 @@ const static unsigned char welcome_utf32be[212] = 0x00,0x00,0x30,0x02 }; // encoded by iconv -const static unsigned char welcome_cp932[71] = +static const unsigned char welcome_cp932[71] = { 0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6f,0x75,0x72,0x20,0x63, 0x79,0x62,0x65,0x72,0x20,0x73,0x70,0x61,0x63,0x65,0x20,0x66,0x6f,0x72,0x63,0x65, @@ -674,6 +678,9 @@ static const unsigned char CP1252_utf8[386] = 0x84,0xa2 }; +// this is unused currently so avoid warnings about this +#if 0 + // a character sequence encoded as iso8859-5 (iconv) static const unsigned char iso8859_5[251] = { @@ -722,6 +729,7 @@ static const unsigned char iso8859_5_utf8[380] = 0x92,0xd1,0x93,0xd1,0x94,0xd1,0x95,0xd1,0x96,0xd1,0x97,0xd1,0x98,0xd1,0x99,0xd1, 0x9a,0xd1,0x9b,0xd1,0x9c,0xd1,0x9e,0xd1,0x9f,0xe2,0x84,0x96 }; +#endif // 0 // DecodeUTF8 // decodes the specified *unterminated* UTF-8 byte array @@ -760,15 +768,14 @@ void MBConvTestCase::TestCoder( // so we should store the wide character version as UTF-8 and depend on // the UTF-8 converter's ability to decode it to platform specific wide characters // this test is invalid if the UTF-8 converter can't decode - wxWCharBuffer wideBuffer((size_t)0); - wideBuffer = DecodeUTF8( utf8Buffer, utf8Bytes ); - size_t wideChars = wxWcslen( wideBuffer.data() ); + const wxWCharBuffer wideBuffer(DecodeUTF8(utf8Buffer, utf8Bytes)); + const size_t wideChars = wxWcslen(wideBuffer); TestDecoder ( wideBuffer.data(), wideChars, - (const char*)multiBuffer, + multiBuffer, multiBytes, converter, sizeofNull @@ -777,7 +784,7 @@ void MBConvTestCase::TestCoder( ( wideBuffer.data(), wideChars, - (const char*)multiBuffer, + multiBuffer, multiBytes, converter, sizeofNull @@ -807,6 +814,120 @@ void MBConvTestCase::FontmapTests() #endif } +void MBConvTestCase::BufSize() +{ + wxCSConv conv1251(wxT("CP1251")); + CPPUNIT_ASSERT( conv1251.IsOk() ); + const char *cp1251text = + "\313\301\326\305\324\323\321 \325\304\301\336\316\331\315"; + + const size_t lenW = conv1251.MB2WC(NULL, cp1251text, 0); + CPPUNIT_ASSERT_EQUAL( strlen(cp1251text), lenW ); + wxWCharBuffer wbuf(lenW + 1); // allocates lenW + 2 characters + wbuf.data()[lenW + 1] = L'!'; + + // lenW is not enough because it's the length and we need the size + CPPUNIT_ASSERT_EQUAL( + wxCONV_FAILED, conv1251.MB2WC(wbuf.data(), cp1251text, lenW) ); + + // lenW+1 is just fine + CPPUNIT_ASSERT( + conv1251.MB2WC(wbuf.data(), cp1251text, lenW + 1) != wxCONV_FAILED ); + + // of course, greater values work too + CPPUNIT_ASSERT( + conv1251.MB2WC(wbuf.data(), cp1251text, lenW + 2) != wxCONV_FAILED ); + + // but they shouldn't write more stuff to the buffer + CPPUNIT_ASSERT_EQUAL( L'!', wbuf[lenW + 1] ); + + + // test in the other direction too, using an encoding with multibyte NUL + wxCSConv convUTF16(wxT("UTF-16LE")); + CPPUNIT_ASSERT( convUTF16.IsOk() ); + const wchar_t *utf16text = L"Hello"; + + const size_t lenMB = convUTF16.WC2MB(NULL, utf16text, 0); + CPPUNIT_ASSERT_EQUAL( wcslen(utf16text)*2, lenMB ); + wxCharBuffer buf(lenMB + 2); // it only adds 1 for NUL on its own, we need 2 + // for NUL and an extra one for the guard byte + buf.data()[lenMB + 2] = '?'; + + CPPUNIT_ASSERT_EQUAL( + wxCONV_FAILED, convUTF16.WC2MB(buf.data(), utf16text, lenMB) ); + CPPUNIT_ASSERT_EQUAL( + wxCONV_FAILED, convUTF16.WC2MB(buf.data(), utf16text, lenMB + 1) ); + CPPUNIT_ASSERT( + convUTF16.WC2MB(buf.data(), utf16text, lenMB + 2) != wxCONV_FAILED ); + CPPUNIT_ASSERT( + convUTF16.WC2MB(buf.data(), utf16text, lenMB + 3) != wxCONV_FAILED ); + CPPUNIT_ASSERT_EQUAL( '?', buf[lenMB + 2] ); +} + +void MBConvTestCase::FromWCharTests() +{ + wxCSConv conv950("CP950"); + char mbuf[10]; + // U+4e00 is 2 bytes (0xa4 0x40) in cp950 + wchar_t wbuf[] = { 0x4e00, 0, 0x4e00, 0 }; + + // test simple ASCII text + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 0, L"a", 1)); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[0]); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( 1, conv950.FromWChar(mbuf, 1, L"a", 1)); + CPPUNIT_ASSERT_EQUAL( 'a', mbuf[0]); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[1]); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 1, L"a", 2)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( 2, conv950.FromWChar(mbuf, 2, L"a", 2)); + CPPUNIT_ASSERT_EQUAL( 'a', mbuf[0]); + CPPUNIT_ASSERT_EQUAL( '\0', mbuf[1]); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[2]); + + // test non-ASCII text, 1 wchar -> 2 char + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 0, wbuf, 1)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 1, wbuf, 1)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( 2, conv950.FromWChar(mbuf, 2, wbuf, 1)); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[2]); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 2, wbuf, 2)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( 3, conv950.FromWChar(mbuf, 3, wbuf, 2)); + CPPUNIT_ASSERT_EQUAL( '\0', mbuf[2]); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[3]); + + // test text with embedded NUL-character and srcLen specified + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 3, wbuf, 3)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 4, wbuf, 3)); + CPPUNIT_ASSERT_EQUAL( 5, conv950.FromWChar(mbuf, 5, wbuf, 3)); + CPPUNIT_ASSERT_EQUAL( '\0', mbuf[2]); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[5]); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, conv950.FromWChar(mbuf, 5, wbuf, 4)); + + memset(mbuf, '!', sizeof(mbuf)); + CPPUNIT_ASSERT_EQUAL( 6, conv950.FromWChar(mbuf, 6, wbuf, 4)); + CPPUNIT_ASSERT_EQUAL( '\0', mbuf[2]); + CPPUNIT_ASSERT_EQUAL( '\0', mbuf[5]); + CPPUNIT_ASSERT_EQUAL( '!', mbuf[6]); +} WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_iconv( const char* name ); @@ -844,7 +965,7 @@ void MBConvTestCase::Latin1Tests() static const char nulstr[] = "foo\0bar\0"; static const size_t mbLen = WXSIZEOF(nulstr) - 1; size_t wcLen; - wxWCharBuffer wbuf(wxConvISO8859_1.cMB2WC(nulstr, mbLen, &wcLen)); + wxConvISO8859_1.cMB2WC(nulstr, mbLen, &wcLen); CPPUNIT_ASSERT_EQUAL( mbLen, wcLen ); } @@ -863,35 +984,23 @@ void MBConvTestCase::CP1252Tests() void MBConvTestCase::LibcTests() { - // There isn't a locale that all systems support (except "C"), so leave - // this one disabled for non-Windows systems for the moment, until - // a solution can be found. -#ifdef __WXMSW__ - -#ifdef __WXMSW__ - setlocale( LC_ALL, "English_United States.1252" ); - const unsigned char* systemMB = CP1252; - size_t systemMB_size = sizeof(CP1252); - const unsigned char* systemMB_utf8 = CP1252_utf8; - size_t systemMB_utf8_size = sizeof(CP1252_utf8); -#else - setlocale( LC_ALL, "en_US.iso8859-1" ); - const unsigned char* systemMB = iso8859_1; - size_t systemMB_size = sizeof(iso8859_1); - const unsigned char* systemMB_utf8 = iso8859_1_utf8; - size_t systemMB_utf8_size = sizeof(iso8859_1_utf8); -#endif + // The locale name are OS-dependent so this test is done only under Windows + // when using MSVC (surprisingly it fails with MinGW, even though it's + // supposed to use the same CRT -- no idea why and unfortunately gdb is too + // flaky to debug it) +#ifdef __VISUALC__ + LocaleSetter loc("English_United States.1252"); + wxMBConvLibc convLibc; TestCoder( - (const char*)systemMB, - systemMB_size, - (const char*)systemMB_utf8, - systemMB_utf8_size, + (const char*)CP1252, + sizeof(CP1252), + (const char*)CP1252_utf8, + sizeof(CP1252_utf8), convLibc, 1 ); - -#endif // __WXMSW__ +#endif // __VISUALC__ } // verifies that the specified mb sequences decode to the specified wc sequence @@ -919,7 +1028,12 @@ void MBConvTestCase::TestDecoder( 0 ); // make sure the correct output length was calculated - CPPUNIT_ASSERT_EQUAL( wideChars, outputWritten ); + WX_ASSERT_EQUAL_MESSAGE + ( + ("while converting \"%s\"", multiBuffer), + wideChars, + outputWritten + ); // convert the string size_t guardChars = 8; // to make sure we're not overrunning the output buffer @@ -1073,7 +1187,7 @@ void MBConvTestCase::TestStreamEncoder( textOutputStream.Flush(); - CPPUNIT_ASSERT_EQUAL( (wxFileOffset)multiBytes, memoryOutputStream.TellO() ); + CPPUNIT_ASSERT_EQUAL( multiBytes, size_t(memoryOutputStream.TellO()) ); wxCharBuffer copy( memoryOutputStream.TellO() ); memoryOutputStream.CopyTo( copy.data(), memoryOutputStream.TellO()); CPPUNIT_ASSERT_EQUAL( 0, memcmp( copy.data(), multiBuffer, multiBytes ) );