X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/78648f606dac99c2af5f8d5bfec3635d9d7031fd..3c01c5951189e13b8b4a5d7b288b54d4a57a30f8:/tests/mbconv/mbconvtest.cpp diff --git a/tests/mbconv/mbconvtest.cpp b/tests/mbconv/mbconvtest.cpp index e1db40d81d..5260887d7c 100644 --- a/tests/mbconv/mbconvtest.cpp +++ b/tests/mbconv/mbconvtest.cpp @@ -78,7 +78,9 @@ private: CPPUNIT_TEST( CP1252Tests ); // depends on UTF8 Decoder functioning correctly CPPUNIT_TEST( LibcTests ); CPPUNIT_TEST( IconvTests ); + CPPUNIT_TEST( Latin1Tests ); CPPUNIT_TEST( FontmapTests ); + CPPUNIT_TEST( BufSize ); #ifdef HAVE_WCHAR_H CPPUNIT_TEST( UTF8_41 ); CPPUNIT_TEST( UTF8_7f ); @@ -112,7 +114,9 @@ private: void CP1252Tests(); void LibcTests(); void FontmapTests(); + void BufSize(); void IconvTests(); + void Latin1Tests(); // verifies that the specified multibyte sequence decodes to the specified wchar_t sequence void TestDecoder( @@ -250,7 +254,7 @@ 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( _T("static const unsigned char %s[%i] = \n{"), name, (int)len ); for ( size_t i = 0; i < len; i++ ) { @@ -273,7 +277,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, @@ -283,7 +287,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, @@ -294,7 +298,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, @@ -304,7 +308,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, @@ -315,7 +319,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, @@ -326,7 +330,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, @@ -344,7 +348,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, @@ -362,7 +366,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, @@ -672,6 +676,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] = { @@ -720,6 +727,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 @@ -805,6 +813,56 @@ void MBConvTestCase::FontmapTests() #endif } +void MBConvTestCase::BufSize() +{ + wxCSConv conv1251(_T("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(_T("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] ); +} + WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_iconv( const char* name ); @@ -828,6 +886,24 @@ void MBConvTestCase::IconvTests() #endif } +void MBConvTestCase::Latin1Tests() +{ + TestCoder( + (const char*)iso8859_1, + sizeof(iso8859_1), + (const char*)iso8859_1_utf8, + sizeof(iso8859_1_utf8), + wxConvISO8859_1, + 1 + ); + + static const char nulstr[] = "foo\0bar\0"; + static const size_t mbLen = WXSIZEOF(nulstr) - 1; + size_t wcLen; + wxConvISO8859_1.cMB2WC(nulstr, mbLen, &wcLen); + CPPUNIT_ASSERT_EQUAL( mbLen, wcLen ); +} + void MBConvTestCase::CP1252Tests() { wxCSConv convCP1252( wxFONTENCODING_CP1252 ); @@ -1018,7 +1094,11 @@ void MBConvTestCase::TestStreamDecoder( for ( size_t i = 0; i < wideChars; i++ ) { wxChar wc = textInputStream.GetChar(); - CPPUNIT_ASSERT( wc == wideBuffer[i] ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + std::string(wxString::Format("At position %lu", (unsigned long)i)), + wideBuffer[i], + wc + ); } CPPUNIT_ASSERT( 0 == textInputStream.GetChar() ); CPPUNIT_ASSERT( memoryInputStream.Eof() ); @@ -1046,7 +1126,10 @@ void MBConvTestCase::TestStreamEncoder( { textOutputStream.PutChar( wideBuffer[i] ); } - CPPUNIT_ASSERT_EQUAL( (wxFileOffset)multiBytes, memoryOutputStream.TellO() ); + + textOutputStream.Flush(); + + 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 ) );