]>
Commit | Line | Data |
---|---|---|
387f829e VS |
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 | ||
8899b155 | 14 | #include "testprec.h" |
387f829e VS |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
31c06391 | 21 | #include "wx/wx.h" |
387f829e VS |
22 | #endif // WX_PRECOMP |
23 | ||
42e8b52f VZ |
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 | const char *str; | |
31 | const wchar_t *wcs; | |
32 | ||
33 | enum | |
34 | { | |
35 | TEST_BOTH = 0, // test both str -> wcs and wcs -> str | |
36 | ONLY_MB2WC = 1 // only test str -> wcs conversion | |
37 | }; | |
38 | ||
39 | int flags; | |
40 | ||
41 | // test that the conversion between str and wcs (subject to flags) succeeds | |
42 | // | |
43 | // the first argument is the index in the test array and is used solely for | |
44 | // diagnostics | |
45 | void Test(size_t n, wxMBConv& conv) const | |
46 | { | |
47 | if ( str ) | |
48 | { | |
49 | wxWCharBuffer wbuf = conv.cMB2WC(str); | |
50 | ||
51 | if ( wcs ) | |
52 | { | |
53 | CPPUNIT_ASSERT_MESSAGE | |
54 | ( | |
55 | Message(n, "MB2WC failed"), | |
56 | wbuf.data() | |
57 | ); | |
58 | ||
59 | CPPUNIT_ASSERT_MESSAGE | |
60 | ( | |
61 | Message(n, "MB2WC", wbuf, wcs), | |
62 | wxStrcmp(wbuf, wcs) == 0 | |
63 | ); | |
64 | } | |
65 | else // conversion is supposed to fail | |
66 | { | |
67 | CPPUNIT_ASSERT_MESSAGE | |
68 | ( | |
69 | Message(n, "MB2WC succeeded"), | |
70 | !wbuf.data() | |
71 | ); | |
72 | } | |
73 | } | |
74 | ||
75 | if ( wcs && !(flags & ONLY_MB2WC) ) | |
76 | { | |
77 | wxCharBuffer buf = conv.cWC2MB(wcs); | |
78 | ||
79 | if ( str ) | |
80 | { | |
81 | CPPUNIT_ASSERT_MESSAGE | |
82 | ( | |
83 | Message(n, "WC2MB failed"), | |
84 | buf.data() | |
85 | ); | |
86 | ||
87 | CPPUNIT_ASSERT_MESSAGE | |
88 | ( | |
89 | Message(n, "WC2MB", buf, str), | |
90 | strcmp(buf, str) == 0 | |
91 | ); | |
92 | } | |
93 | else | |
94 | { | |
95 | CPPUNIT_ASSERT_MESSAGE | |
96 | ( | |
97 | Message(n, "WC2MB succeeded"), | |
98 | !buf.data() | |
99 | ); | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | private: | |
105 | static std::string | |
106 | Message(size_t n, const wxString& msg) | |
107 | { | |
108 | return std::string(wxString::Format("#%lu: %s", (unsigned long)n, msg)); | |
109 | } | |
110 | ||
111 | template <typename T> | |
112 | static std::string | |
113 | Message(size_t n, | |
114 | const char *func, | |
115 | const wxCharTypeBuffer<T>& actual, | |
116 | const T *expected) | |
117 | { | |
118 | return Message(n, | |
119 | wxString::Format("%s returned \"%s\", expected \"%s\"", | |
120 | func, actual.data(), expected)); | |
121 | } | |
122 | }; | |
123 | ||
387f829e VS |
124 | // ---------------------------------------------------------------------------- |
125 | // test class | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | class UnicodeTestCase : public CppUnit::TestCase | |
129 | { | |
130 | public: | |
131 | UnicodeTestCase(); | |
132 | ||
133 | private: | |
134 | CPPUNIT_TEST_SUITE( UnicodeTestCase ); | |
135 | CPPUNIT_TEST( ToFromAscii ); | |
a65ca3e6 | 136 | CPPUNIT_TEST( ConstructorsWithConversion ); |
85d3e5a9 | 137 | CPPUNIT_TEST( ConversionEmpty ); |
5975f198 | 138 | CPPUNIT_TEST( ConversionWithNULs ); |
a65ca3e6 VZ |
139 | CPPUNIT_TEST( ConversionUTF7 ); |
140 | CPPUNIT_TEST( ConversionUTF8 ); | |
5975f198 | 141 | CPPUNIT_TEST( ConversionUTF16 ); |
a7823b26 | 142 | CPPUNIT_TEST( ConversionUTF32 ); |
0f0298b1 | 143 | CPPUNIT_TEST( IsConvOk ); |
b0c4d5d7 VS |
144 | #if wxUSE_UNICODE |
145 | CPPUNIT_TEST( Iteration ); | |
146 | #endif | |
387f829e VS |
147 | CPPUNIT_TEST_SUITE_END(); |
148 | ||
149 | void ToFromAscii(); | |
a65ca3e6 | 150 | void ConstructorsWithConversion(); |
85d3e5a9 | 151 | void ConversionEmpty(); |
5975f198 | 152 | void ConversionWithNULs(); |
a65ca3e6 VZ |
153 | void ConversionUTF7(); |
154 | void ConversionUTF8(); | |
5975f198 | 155 | void ConversionUTF16(); |
a7823b26 | 156 | void ConversionUTF32(); |
0f0298b1 | 157 | void IsConvOk(); |
b0c4d5d7 VS |
158 | #if wxUSE_UNICODE |
159 | void Iteration(); | |
160 | #endif | |
a65ca3e6 | 161 | |
387f829e VS |
162 | DECLARE_NO_COPY_CLASS(UnicodeTestCase) |
163 | }; | |
164 | ||
165 | // register in the unnamed registry so that these tests are run by default | |
166 | CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase ); | |
167 | ||
168 | // also include in it's own registry so that these tests can be run alone | |
81e9dec6 | 169 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "UnicodeTestCase" ); |
387f829e VS |
170 | |
171 | UnicodeTestCase::UnicodeTestCase() | |
172 | { | |
173 | } | |
174 | ||
175 | void UnicodeTestCase::ToFromAscii() | |
176 | { | |
177 | ||
178 | #define TEST_TO_FROM_ASCII(txt) \ | |
179 | { \ | |
180 | static const char *msg = txt; \ | |
181 | wxString s = wxString::FromAscii(msg); \ | |
182 | CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \ | |
183 | } | |
184 | ||
185 | TEST_TO_FROM_ASCII( "Hello, world!" ); | |
186 | TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" ); | |
187 | } | |
188 | ||
a65ca3e6 VZ |
189 | void UnicodeTestCase::ConstructorsWithConversion() |
190 | { | |
191 |