| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: tests/strings/unicode.cpp |
| 3 | // Purpose: Unicode unit test |
| 4 | // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba |
| 5 | // Created: 2004-04-28 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // ---------------------------------------------------------------------------- |
| 11 | // headers |
| 12 | // ---------------------------------------------------------------------------- |
| 13 | |
| 14 | #include "testprec.h" |
| 15 | |
| 16 | #ifdef __BORLANDC__ |
| 17 | #pragma hdrstop |
| 18 | #endif |
| 19 | |
| 20 | #ifndef WX_PRECOMP |
| 21 | #include "wx/wx.h" |
| 22 | #endif // WX_PRECOMP |
| 23 | |
| 24 | // helper class holding the matching MB and WC strings |
| 25 | // |
| 26 | // either str or wcs (but not both) may be NULL, this means that the conversion |
| 27 | // to it should fail |
| 28 | struct StringConversionData |
| 29 | { |
| 30 | StringConversionData(const char *str_, const wchar_t *wcs_, int flags_ = 0) |
| 31 | : str(str_), wcs(wcs_), flags(flags_) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | const char * const str; |
| 36 | const wchar_t * const wcs; |
| 37 | |
| 38 | enum |
| 39 | { |
| 40 | TEST_BOTH = 0, // test both str -> wcs and wcs -> str |
| 41 | ONLY_MB2WC = 1 // only test str -> wcs conversion |
| 42 | }; |
| 43 | |
| 44 | const int flags; |
| 45 | |
| 46 | // test that the conversion between str and wcs (subject to flags) succeeds |
| 47 | // |
| 48 | // the first argument is the index in the test array and is used solely for |
| 49 | // diagnostics |
| 50 | void Test(size_t n, wxMBConv& conv) const |
| 51 | { |
| 52 | if ( str ) |
| 53 | { |
| 54 | wxWCharBuffer wbuf = conv.cMB2WC(str); |
| 55 | |
| 56 | if ( wcs ) |
| 57 | { |
| 58 | CPPUNIT_ASSERT_MESSAGE |
| 59 | ( |
| 60 | Message(n, "MB2WC failed"), |
| 61 | wbuf.data() |
| 62 | ); |
| 63 | |
| 64 | CPPUNIT_ASSERT_MESSAGE |
| 65 | ( |
| 66 | Message(n, "MB2WC", wbuf, wcs), |
| 67 | wxStrcmp(wbuf, wcs) == 0 |
| 68 | ); |
| 69 | } |
| 70 | else // conversion is supposed to fail |
| 71 | { |
| 72 | CPPUNIT_ASSERT_MESSAGE |
| 73 | ( |
| 74 | Message(n, "MB2WC succeeded"), |
| 75 | !wbuf.data() |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if ( wcs && !(flags & ONLY_MB2WC) ) |
| 81 | { |
| 82 | wxCharBuffer buf = conv.cWC2MB(wcs); |
| 83 | |
| 84 | if ( str ) |
| 85 | { |
| 86 | CPPUNIT_ASSERT_MESSAGE |
| 87 | ( |
| 88 | Message(n, "WC2MB failed"), |
| 89 | buf.data() |
| 90 | ); |
| 91 | |
| 92 | CPPUNIT_ASSERT_MESSAGE |
| 93 | ( |
| 94 | Message(n, "WC2MB", buf, str), |
| 95 | strcmp(buf, str) == 0 |
| 96 | ); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | CPPUNIT_ASSERT_MESSAGE |
| 101 | ( |
| 102 | Message(n, "WC2MB succeeded"), |
| 103 | !buf.data() |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | static std::string |
| 111 | Message(size_t n, const wxString& msg) |
| 112 | { |
| 113 | return std::string(wxString::Format("#%lu: %s", (unsigned long)n, msg)); |
| 114 | } |
| 115 | |
| 116 | template <typename T> |
| 117 | static std::string |
| 118 | Message(size_t n, |
| 119 | const char *func, |
| 120 | const wxCharTypeBuffer<T>& actual, |
| 121 | const T *expected) |
| 122 | { |
| 123 | return Message(n, |
| 124 | wxString::Format("%s returned \"%s\", expected \"%s\"", |
| 125 | func, actual.data(), expected)); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | // test class |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | |
| 133 | class UnicodeTestCase : public CppUnit::TestCase |
| 134 | { |
| 135 | public: |
| 136 | UnicodeTestCase(); |
| 137 | |
| 138 | private: |
| 139 | CPPUNIT_TEST_SUITE( UnicodeTestCase ); |
| 140 | CPPUNIT_TEST( ToFromAscii ); |
| 141 | CPPUNIT_TEST( ConstructorsWithConversion ); |
| 142 | CPPUNIT_TEST( ConversionEmpty ); |
| 143 | CPPUNIT_TEST( ConversionWithNULs ); |
| 144 | CPPUNIT_TEST( ConversionUTF7 ); |
| 145 | CPPUNIT_TEST( ConversionUTF8 ); |
| 146 | CPPUNIT_TEST( ConversionUTF16 ); |
| 147 | CPPUNIT_TEST( ConversionUTF32 ); |
| 148 | CPPUNIT_TEST( IsConvOk ); |
| 149 | #if wxUSE_UNICODE |
| 150 | CPPUNIT_TEST( Iteration ); |
| 151 | #endif |
| 152 | CPPUNIT_TEST_SUITE_END(); |
| 153 | |
| 154 | void ToFromAscii(); |
| 155 | void ConstructorsWithConversion(); |
| 156 | void ConversionEmpty(); |
| 157 | void ConversionWithNULs(); |
| 158 | void ConversionUTF7(); |
| 159 | void ConversionUTF8(); |
| 160 | void ConversionUTF16(); |
| 161 | void ConversionUTF32(); |
| 162 | void IsConvOk(); |
| 163 | #if wxUSE_UNICODE |
| 164 | void Iteration(); |
| 165 | #endif |
| 166 | |
| 167 | DECLARE_NO_COPY_CLASS(UnicodeTestCase) |
| 168 | }; |
| 169 | |
| 170 | // register in the unnamed registry so that these tests are run by default |
| 171 | CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase ); |
| 172 | |
| 173 | // also include in it's own registry so that these tests can be run alone |
| 174 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "UnicodeTestCase" ); |
| 175 | |
| 176 | UnicodeTestCase::UnicodeTestCase() |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | void UnicodeTestCase::ToFromAscii() |
| 181 | { |
| 182 | |
| 183 | #define TEST_TO_FROM_ASCII(txt) \ |
| 184 | { \ |
| 185 | static const char *msg = txt; \ |
| 186 | wxString s = wxString::FromAscii(msg); \ |
| 187 | CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \ |
| 188 | } |
| 189 | |
| 190 | TEST_TO_FROM_ASCII( "Hello, world!" ); |
| 191 | TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" ); |
| 192 | } |
| 193 | |
| 194 | void UnicodeTestCase::ConstructorsWithConversion() |
| 195 | { |
| 196 |