+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]);
+}