| 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 | #endif // WX_PRECOMP |
| 22 | |
| 23 | // ---------------------------------------------------------------------------- |
| 24 | // local functions |
| 25 | // ---------------------------------------------------------------------------- |
| 26 | |
| 27 | #if wxUSE_WCHAR_T && !wxUSE_UNICODE |
| 28 | |
| 29 | // in case wcscmp is missing |
| 30 | static int wx_wcscmp(const wchar_t *s1, const wchar_t *s2) |
| 31 | { |
| 32 | while (*s1 == *s2 && *s1 != 0) |
| 33 | { |
| 34 | s1++; |
| 35 | s2++; |
| 36 | } |
| 37 | return *s1 - *s2; |
| 38 | } |
| 39 | |
| 40 | #endif // wxUSE_WCHAR_T && !wxUSE_UNICODE |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // test class |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | class UnicodeTestCase : public CppUnit::TestCase |
| 47 | { |
| 48 | public: |
| 49 | UnicodeTestCase(); |
| 50 | |
| 51 | private: |
| 52 | CPPUNIT_TEST_SUITE( UnicodeTestCase ); |
| 53 | CPPUNIT_TEST( ToFromAscii ); |
| 54 | #if wxUSE_WCHAR_T |
| 55 | CPPUNIT_TEST( ConstructorsWithConversion ); |
| 56 | CPPUNIT_TEST( ConversionEmpty ); |
| 57 | CPPUNIT_TEST( ConversionWithNULs ); |
| 58 | CPPUNIT_TEST( ConversionUTF7 ); |
| 59 | CPPUNIT_TEST( ConversionUTF8 ); |
| 60 | CPPUNIT_TEST( ConversionUTF16 ); |
| 61 | CPPUNIT_TEST( ConversionUTF32 ); |
| 62 | #endif // wxUSE_WCHAR_T |
| 63 | CPPUNIT_TEST_SUITE_END(); |
| 64 | |
| 65 | void ToFromAscii(); |
| 66 | #if wxUSE_WCHAR_T |
| 67 | void ConstructorsWithConversion(); |
| 68 | void ConversionEmpty(); |
| 69 | void ConversionWithNULs(); |
| 70 | void ConversionUTF7(); |
| 71 | void ConversionUTF8(); |
| 72 | void ConversionUTF16(); |
| 73 | void ConversionUTF32(); |
| 74 | |
| 75 | // test if converting s using the given encoding gives ws and vice versa |
| 76 | // |
| 77 | // if either of the first 2 arguments is NULL, the conversion is supposed |
| 78 | // to fail |
| 79 | void DoTestConversion(const char *s, const wchar_t *w, wxMBConv& conv); |
| 80 | #endif // wxUSE_WCHAR_T |
| 81 | |
| 82 | |
| 83 | DECLARE_NO_COPY_CLASS(UnicodeTestCase) |
| 84 | }; |
| 85 | |
| 86 | // register in the unnamed registry so that these tests are run by default |
| 87 | CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase ); |
| 88 | |
| 89 | // also include in it's own registry so that these tests can be run alone |
| 90 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "Unicode" ); |
| 91 | |
| 92 | UnicodeTestCase::UnicodeTestCase() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | void UnicodeTestCase::ToFromAscii() |
| 97 | { |
| 98 | |
| 99 | #define TEST_TO_FROM_ASCII(txt) \ |
| 100 | { \ |
| 101 | static const char *msg = txt; \ |
| 102 | wxString s = wxString::FromAscii(msg); \ |
| 103 | CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \ |
| 104 | } |
| 105 | |
| 106 | TEST_TO_FROM_ASCII( "Hello, world!" ); |
| 107 | TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" ); |
| 108 | } |
| 109 | |
| 110 | #if wxUSE_WCHAR_T |
| 111 | void UnicodeTestCase::ConstructorsWithConversion() |
| 112 | { |
| 113 |