]>
Commit | Line | Data |
---|---|---|
39601a7f VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/any/anytest.cpp | |
3 | // Purpose: Test the wxAny classes | |
4 | // Author: Jaakko Salli | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) the wxWidgets team | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "testprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | # pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #if wxUSE_ANY | |
17 | ||
18 | #include "wx/any.h" | |
19 | #include "wx/datetime.h" | |
97051666 | 20 | #include "wx/object.h" |
39601a7f VZ |
21 | |
22 | #include <math.h> | |
23 | ||
24 | // ---------------------------------------------------------------------------- | |
25 | // test class | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class wxAnyTestCase : public CppUnit::TestCase | |
29 | { | |
30 | public: | |
31 | wxAnyTestCase(); | |
32 | ||
33 | private: | |
34 | CPPUNIT_TEST_SUITE( wxAnyTestCase ); | |
153107b4 | 35 | CPPUNIT_TEST( CheckType ); |
39601a7f VZ |
36 | CPPUNIT_TEST( Equality ); |
37 | CPPUNIT_TEST( As ); | |
38 | CPPUNIT_TEST( GetAs ); | |
39 | CPPUNIT_TEST( Null ); | |
0bf14ab8 | 40 | CPPUNIT_TEST( wxVariantConversions ); |
39601a7f VZ |
41 | CPPUNIT_TEST( CustomTemplateSpecialization ); |
42 | CPPUNIT_TEST_SUITE_END(); | |
43 | ||
153107b4 | 44 | void CheckType(); |
39601a7f VZ |
45 | void Equality(); |
46 | void As(); | |
47 | void GetAs(); | |
48 | void Null(); | |
0bf14ab8 | 49 | void wxVariantConversions(); |
39601a7f VZ |
50 | void CustomTemplateSpecialization(); |
51 | ||
52 | wxDateTime m_testDateTime; | |
53 | ||
54 | wxAny m_anySignedChar1; | |
55 | wxAny m_anySignedShort1; | |
56 | wxAny m_anySignedInt1; | |
57 | wxAny m_anySignedLong1; | |
58 | wxAny m_anySignedLongLong1; | |
59 | wxAny m_anyUnsignedChar1; | |
60 | wxAny m_anyUnsignedShort1; | |
61 | wxAny m_anyUnsignedInt1; | |
62 | wxAny m_anyUnsignedLong1; | |
63 | wxAny m_anyUnsignedLongLong1; | |
64 | wxAny m_anyStringString1; | |
65 | wxAny m_anyCharString1; | |
66 | wxAny m_anyWcharString1; | |
67 | wxAny m_anyBool1; | |
68 | wxAny m_anyFloatDouble1; | |
69 | wxAny m_anyDoubleDouble1; | |
70 | wxAny m_anyWxObjectPtr1; | |
71 | wxAny m_anyVoidPtr1; | |
72 | wxAny m_anyDateTime1; | |
0bf14ab8 | 73 | wxAny m_anyUniChar1; |
39601a7f VZ |
74 | |
75 | wxAny m_anySignedChar2; | |
76 | wxAny m_anySignedShort2; | |
77 | wxAny m_anySignedInt2; | |
78 | wxAny m_anySignedLong2; | |
79 | wxAny m_anySignedLongLong2; | |
80 | wxAny m_anyUnsignedChar2; | |
81 | wxAny m_anyUnsignedShort2; | |
82 | wxAny m_anyUnsignedInt2; | |
83 | wxAny m_anyUnsignedLong2; | |
84 | wxAny m_anyUnsignedLongLong2; | |
85 | wxAny m_anyStringString2; | |
86 | wxAny m_anyCharString2; | |
87 | wxAny m_anyWcharString2; | |
88 | wxAny m_anyBool2; | |
89 | wxAny m_anyFloatDouble2; | |
90 | wxAny m_anyDoubleDouble2; | |
91 | wxAny m_anyWxObjectPtr2; | |
92 | wxAny m_anyVoidPtr2; | |
93 | wxAny m_anyDateTime2; | |
94 | ||
95 | DECLARE_NO_COPY_CLASS(wxAnyTestCase) | |
96 | }; | |
97 | ||
98 | // register in the unnamed registry so that these tests are run by default | |
99 | CPPUNIT_TEST_SUITE_REGISTRATION( wxAnyTestCase ); | |
100 | ||
101 | // also include in it's own registry so that these tests can be run alone | |
102 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( wxAnyTestCase, "wxAnyTestCase" ); | |
103 | ||
104 | // Let's use a number with first digit after decimal dot less than 5, | |
105 | // so that we don't have to worry about whether conversion from float | |
106 | // to int truncates or rounds. | |
d517b606 JS |
107 | const float TEST_FLOAT_CONST = 123.456f; |
108 | const double TEST_DOUBLE_CONST = 123.456; | |
39601a7f VZ |
109 | |
110 | const double FEQ_DELTA = 0.001; | |
111 | ||
112 | wxObject* dummyWxObjectPointer = reinterpret_cast<wxObject*>(1234); | |
113 | void* dummyVoidPointer = reinterpret_cast<void*>(1234); | |
114 | ||
115 | ||
116 | // | |
117 | // Test both 'creation' methods | |
118 | wxAnyTestCase::wxAnyTestCase() | |
119 | : m_anySignedChar1((signed char)15), | |
120 | m_anySignedShort1((signed short)15), | |
121 | m_anySignedInt1((signed int)15), | |
122 | m_anySignedLong1((signed long)15), | |
123 | #ifdef wxLongLong_t | |
124 | m_anySignedLongLong1((wxLongLong_t)15), | |
125 | #endif | |
126 | m_anyUnsignedChar1((unsigned char)15), | |
127 | m_anyUnsignedShort1((unsigned short)15), | |
128 | m_anyUnsignedInt1((unsigned int)15), | |
129 | m_anyUnsignedLong1((unsigned long)15), | |
130 | #ifdef wxLongLong_t | |
131 | m_anyUnsignedLongLong1((wxULongLong_t)15), | |
132 | #endif | |
133 | m_anyStringString1(wxString("abc")), | |
134 | m_anyCharString1("abc"), | |
135 | m_anyWcharString1(L"abc"), | |
136 | m_anyBool1(true), | |
d517b606 JS |
137 | m_anyFloatDouble1(TEST_FLOAT_CONST), |
138 | m_anyDoubleDouble1(TEST_DOUBLE_CONST), | |
39601a7f VZ |
139 | m_anyWxObjectPtr1(dummyWxObjectPointer), |
140 | m_anyVoidPtr1(dummyVoidPointer), | |
141 | m_anyDateTime1(wxDateTime::Now()) | |
142 | { | |
143 | m_testDateTime = wxDateTime::Now(); | |
144 | m_anySignedChar2 = (signed char)15; | |
145 | m_anySignedShort2 = (signed short)15; | |
146 | m_anySignedInt2 = (signed int)15; | |
147 | m_anySignedLong2 = (signed long)15; | |
148 | #ifdef wxLongLong_t | |
149 | m_anySignedLongLong2 = (wxLongLong_t)15; | |
150 | #endif | |
151 | m_anyUnsignedChar2 = (unsigned char)15; | |
152 | m_anyUnsignedShort2 = (unsigned short)15; | |
153 | m_anyUnsignedInt2 = (unsigned int)15; | |
154 | m_anyUnsignedLong2 = (unsigned long)15; | |
155 | #ifdef wxLongLong_t | |
156 | m_anyUnsignedLongLong2 = (wxULongLong_t)15; | |
157 | #endif | |
158 | m_anyStringString2 = wxString("abc"); | |
159 | m_anyCharString2 = "abc"; | |
160 | m_anyWcharString2 = L"abc"; | |
161 | m_anyBool2 = true; | |
d517b606 JS |
162 | m_anyFloatDouble2 = TEST_FLOAT_CONST; |
163 | m_anyDoubleDouble2 = TEST_DOUBLE_CONST; | |
39601a7f | 164 | m_anyDateTime2 = m_testDateTime; |
0bf14ab8 | 165 | m_anyUniChar1 = wxUniChar('A'); |
39601a7f VZ |
166 | m_anyWxObjectPtr2 = dummyWxObjectPointer; |
167 | m_anyVoidPtr2 = dummyVoidPointer; | |
168 | } | |
169 | ||
153107b4 JS |
170 | void wxAnyTestCase::CheckType() |
171 | { | |
172 | wxAny nullAny; | |
173 | CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(nullAny, wxString)); | |
174 | ||
175 | CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyCharString2, const char*)); | |
176 | CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2, wxString)); | |
177 | CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2, const wchar_t*)); | |
178 | CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyWcharString2, const wchar_t*)); | |
179 | CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2, wxString)); | |
180 | CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2, const char*)); | |
181 | } | |
182 | ||
39601a7f VZ |
183 | void wxAnyTestCase::Equality() |
184 | { | |
185 | // | |
186 | // Currently this should work | |
187 | CPPUNIT_ASSERT(m_anyUnsignedLong1 == 15L); | |
188 | CPPUNIT_ASSERT(m_anyUnsignedLong1 != 30L); | |
189 | CPPUNIT_ASSERT(m_anyUnsignedLong1 == 15UL); | |
190 | CPPUNIT_ASSERT(m_anyUnsignedLong1 != 30UL); | |
191 | CPPUNIT_ASSERT(m_anyStringString1 == wxString("abc")); | |
192 | CPPUNIT_ASSERT(m_anyStringString1 != wxString("ABC")); | |
193 | CPPUNIT_ASSERT(m_anyStringString1 == "abc"); | |
194 | CPPUNIT_ASSERT(m_anyStringString1 != "ABC"); | |
195 | CPPUNIT_ASSERT(m_anyStringString1 == L"abc"); | |
196 | CPPUNIT_ASSERT(m_anyStringString1 != L"ABC"); | |
197 | CPPUNIT_ASSERT(m_anyBool1 == true); | |
198 | CPPUNIT_ASSERT(m_anyBool1 != false); | |
199 | CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1, double), | |
200 | wxANY_AS(m_anyDoubleDouble1, double), | |
201 | FEQ_DELTA); | |
202 | CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1, double), | |
203 | TEST_FLOAT_CONST, | |
204 | FEQ_DELTA); | |
205 | CPPUNIT_ASSERT(wxANY_AS(m_anyWxObjectPtr1, wxObject*) | |
206 | == dummyWxObjectPointer); | |
207 | CPPUNIT_ASSERT(wxANY_AS(m_anyVoidPtr1, void*) == dummyVoidPointer); | |
208 | ||
209 | CPPUNIT_ASSERT(m_anySignedLong2 == 15); | |
210 | CPPUNIT_ASSERT(m_anyStringString2 == wxString("abc")); | |
211 | CPPUNIT_ASSERT(m_anyStringString2 == "abc"); | |
212 | CPPUNIT_ASSERT(m_anyStringString2 == L"abc"); | |
213 | CPPUNIT_ASSERT(m_anyBool2 == true); | |
214 | CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2, double), | |
215 | wxANY_AS(m_anyDoubleDouble2, double), | |
216 | FEQ_DELTA); | |
217 | CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2, double), | |
218 | TEST_FLOAT_CONST, | |
219 | FEQ_DELTA); | |
220 | CPPUNIT_ASSERT(wxANY_AS(m_anyWxObjectPtr2, wxObject*) | |
221 | == dummyWxObjectPointer); | |
222 | CPPUNIT_ASSERT(wxANY_AS(m_anyVoidPtr2, void*) == dummyVoidPointer); | |
7db064f6 JS |
223 | |
224 | // Test sub-type system type compatibility | |
225 | CPPUNIT_ASSERT(m_anySignedShort1.GetType()-> | |
226 | IsSameType(m_anySignedLongLong1.GetType())); | |
227 | CPPUNIT_ASSERT(m_anyUnsignedShort1.GetType()-> | |
228 | IsSameType(m_anyUnsignedLongLong1.GetType())); | |
39601a7f VZ |
229 | } |
230 | ||
231 | void wxAnyTestCase::As() | |
232 | { | |
233 | // | |
234 | // Test getting C++ data from wxAny without dynamic conversion | |
235 | signed char a = wxANY_AS(m_anySignedChar1, signed char); | |
236 | CPPUNIT_ASSERT(a == (signed int)15); | |
237 | signed short b = wxANY_AS(m_anySignedShort1, signed short); | |
238 | CPPUNIT_ASSERT(b == (signed int)15); | |
239 | signed int c = wxANY_AS(m_anySignedInt1, signed int); | |
240 | CPPUNIT_ASSERT(c == (signed int)15); | |
241 | signed long d = wxANY_AS(m_anySignedLong1, signed long); | |
242 | CPPUNIT_ASSERT(d == (signed int)15); | |
243 | #ifdef wxLongLong_t | |
244 | wxLongLong_t e = wxANY_AS(m_anySignedLongLong1, wxLongLong_t); | |
245 | CPPUNIT_ASSERT(e == (signed int)15); | |
246 | #endif | |
247 | unsigned char f = wxANY_AS(m_anyUnsignedChar1, unsigned char); | |
248 | CPPUNIT_ASSERT(f == (unsigned int)15); | |
249 | unsigned short g = wxANY_AS(m_anyUnsignedShort1, unsigned short); | |
250 | CPPUNIT_ASSERT(g == (unsigned int)15); | |
251 | unsigned int h = wxANY_AS(m_anyUnsignedInt1, unsigned int); | |
252 | CPPUNIT_ASSERT(h == (unsigned int)15); | |
253 | unsigned long i = wxANY_AS(m_anyUnsignedLong1, unsigned long); | |
254 | CPPUNIT_ASSERT(i == (unsigned int)15); | |
255 | #ifdef wxLongLong_t | |
256 | wxULongLong_t j = wxANY_AS(m_anyUnsignedLongLong1, wxULongLong_t); | |
257 | CPPUNIT_ASSERT(j == (unsigned int)15); | |
258 | #endif | |
259 | wxString k = wxANY_AS(m_anyStringString1, wxString); | |
260 | CPPUNIT_ASSERT(k == "abc"); | |
261 | wxString l = wxANY_AS(m_anyCharString1, wxString); | |
153107b4 | 262 | const char* cptr = wxANY_AS(m_anyCharString1, const char*); |
39601a7f | 263 | CPPUNIT_ASSERT(l == "abc"); |
153107b4 | 264 | CPPUNIT_ASSERT(cptr); |
39601a7f | 265 | wxString m = wxANY_AS(m_anyWcharString1, wxString); |
153107b4 JS |
266 | const wchar_t* wcptr = wxANY_AS(m_anyWcharString1, const wchar_t*); |
267 | CPPUNIT_ASSERT(wcptr); | |
39601a7f VZ |
268 | CPPUNIT_ASSERT(m == "abc"); |
269 | bool n = wxANY_AS(m_anyBool1, bool); | |
270 | CPPUNIT_ASSERT(n); | |
d517b606 JS |
271 | |
272 | // Make sure the stored float that comes back is -identical-. | |
273 | // So do not use delta comparison here. | |
39601a7f | 274 | float o = wxANY_AS(m_anyFloatDouble1, float); |
d517b606 JS |
275 | CPPUNIT_ASSERT_EQUAL(o, TEST_FLOAT_CONST); |
276 | ||
39601a7f | 277 | double p = wxANY_AS(m_anyDoubleDouble1, double); |
d517b606 JS |
278 | CPPUNIT_ASSERT_EQUAL(p, TEST_DOUBLE_CONST); |
279 | ||
0bf14ab8 JS |
280 | wxUniChar chr = wxANY_AS(m_anyUniChar1, wxUniChar); |
281 | CPPUNIT_ASSERT(chr == 'A'); | |
39601a7f VZ |
282 | wxDateTime q = wxANY_AS(m_anyDateTime1, wxDateTime); |
283 | CPPUNIT_ASSERT(q == m_testDateTime); | |
284 | wxObject* r = wxANY_AS(m_anyWxObjectPtr1, wxObject*); | |
285 | CPPUNIT_ASSERT(r == dummyWxObjectPointer); | |
286 | void* s = wxANY_AS(m_anyVoidPtr1, void*); | |
287 | CPPUNIT_ASSERT(s == dummyVoidPointer); | |
288 | } | |
289 | ||
290 | void wxAnyTestCase::Null() | |
291 | { | |
292 | wxAny a; | |
293 | CPPUNIT_ASSERT(a.IsNull()); | |
294 | a = -127; | |
295 | CPPUNIT_ASSERT(a == -127); | |
296 | a.MakeNull(); | |
297 | CPPUNIT_ASSERT(a.IsNull()); | |
298 | } | |
299 | ||
300 | void wxAnyTestCase::GetAs() | |
301 | { | |
302 | // | |
303 | // Test dynamic conversion | |
304 | bool res; | |
305 | long l = 0; | |
7db064f6 | 306 | short int si = 0; |
39601a7f VZ |
307 | unsigned long ul = 0; |
308 | wxString s; | |
309 | // Let's test against float instead of double, since the former | |
310 | // is not the native underlying type the code converts to, but | |
311 | // should still work, all the same. | |
312 | float f = 0.0; | |
313 | bool b = false; | |
314 | ||
315 | // Conversions from signed long type | |
7db064f6 JS |
316 | // The first check should be enough to make sure that the sub-type system |
317 | // has not failed. | |
318 | res = m_anySignedLong1.GetAs(&si); | |
319 | CPPUNIT_ASSERT(res); | |
320 | CPPUNIT_ASSERT_EQUAL(si, 15); | |
39601a7f VZ |
321 | res = m_anySignedLong1.GetAs(&ul); |
322 | CPPUNIT_ASSERT(res); | |
7db064f6 | 323 | CPPUNIT_ASSERT_EQUAL(ul, 15UL); |
39601a7f VZ |
324 | res = m_anySignedLong1.GetAs(&s); |
325 | CPPUNIT_ASSERT(res); | |
326 | CPPUNIT_ASSERT(s == "15"); | |
327 | res = m_anySignedLong1.GetAs(&f); | |
328 | CPPUNIT_ASSERT(res); | |
329 | CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA); | |
330 | res = m_anySignedLong1.GetAs(&b); | |
331 | CPPUNIT_ASSERT(res); | |
332 | CPPUNIT_ASSERT(b == true); | |
333 | ||
334 | // Conversions from unsigned long type | |
335 | res = m_anyUnsignedLong1.GetAs(&l); | |
336 | CPPUNIT_ASSERT(res); | |
337 | CPPUNIT_ASSERT(l == static_cast<signed long>(15)); | |
338 | res = m_anyUnsignedLong1.GetAs(&s); | |
339 | CPPUNIT_ASSERT(res); | |
340 | CPPUNIT_ASSERT(s == "15"); | |
341 | res = m_anyUnsignedLong1.GetAs(&f); | |
342 | CPPUNIT_ASSERT(res); | |
343 | CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA); | |
344 | res = m_anyUnsignedLong1.GetAs(&b); | |
345 | CPPUNIT_ASSERT(res); | |
346 | CPPUNIT_ASSERT(b == true); | |
347 | ||
348 | // Conversions from default "abc" string to other types | |
349 | // should not work. | |
350 | CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&l)); | |
351 | CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&ul)); | |
352 | CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&f)); | |
353 | CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&b)); | |
354 | ||
355 | // Let's test some other conversions from string that should work. | |
356 | wxAny anyString; | |
357 | ||
358 | anyString = "15"; | |
359 | res = anyString.GetAs(&l); | |
360 | CPPUNIT_ASSERT(res); | |
361 | CPPUNIT_ASSERT(l == static_cast<signed long>(15)); | |
362 | res = anyString.GetAs(&ul); | |
363 | CPPUNIT_ASSERT(res); | |
364 | CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(15)); | |
365 | res = anyString.GetAs(&f); | |
366 | CPPUNIT_ASSERT(res); | |
367 | CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA); | |
368 | anyString = "TRUE"; | |
369 | res = anyString.GetAs(&b); | |
370 | CPPUNIT_ASSERT(res); | |
371 | CPPUNIT_ASSERT(b == true); | |
372 | anyString = "0"; | |
373 | res = anyString.GetAs(&b); | |
374 | CPPUNIT_ASSERT(res); | |
375 | CPPUNIT_ASSERT(b == false); | |
376 | ||
377 | // Conversions from bool type | |
378 | res = m_anyBool1.GetAs(&l); | |
379 | CPPUNIT_ASSERT(res); | |
380 | CPPUNIT_ASSERT(l == static_cast<signed long>(1)); | |
381 | res = m_anyBool1.GetAs(&ul); | |
382 | CPPUNIT_ASSERT(res); | |
383 | CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(1)); | |
384 | res = m_anyBool1.GetAs(&s); | |
385 | CPPUNIT_ASSERT(res); | |
386 | CPPUNIT_ASSERT(s == "true"); | |
387 | CPPUNIT_ASSERT(!m_anyBool1.GetAs(&f)); | |
388 | ||
389 | // Conversions from floating point type | |
390 | res = m_anyDoubleDouble1.GetAs(&l); | |
391 | CPPUNIT_ASSERT(res); | |
392 | CPPUNIT_ASSERT(l == static_cast<signed long>(123)); | |
393 | res = m_anyDoubleDouble1.GetAs(&ul); | |
394 | CPPUNIT_ASSERT(res); | |
395 | CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(123)); | |
396 | res = m_anyDoubleDouble1.GetAs(&s); | |
397 | CPPUNIT_ASSERT(res); | |
398 | double d2; | |
399 | res = s.ToDouble(&d2); | |
400 | CPPUNIT_ASSERT(res); | |
401 | CPPUNIT_ASSERT_DOUBLES_EQUAL(d2, TEST_FLOAT_CONST, FEQ_DELTA); | |
402 | } | |
403 | ||
0bf14ab8 | 404 | |
39601a7f | 405 | // |
0bf14ab8 JS |
406 | // Test user data type for wxAnyValueTypeImpl specialization |
407 | // any hand-built wxVariantData | |
39601a7f VZ |
408 | // |
409 | ||
410 | class MyClass | |
411 | { | |
412 | public: | |
413 | MyClass( int someValue = 32768 ) | |
414 | { | |
415 | m_someValue = someValue; | |
416 | } | |
417 | ||
418 | wxString ToString() | |
419 | { | |
420 | return wxString::Format("%i", m_someValue); | |
421 | } | |
422 | ||
423 | private: | |
424 | int m_someValue; | |
425 | }; | |
426 | ||
427 | ||
0bf14ab8 JS |
428 | #if wxUSE_VARIANT |
429 | ||
430 | // For testing purposes, create dummy variant data implementation | |
431 | // that does not have wxAny conversion code | |
432 | class wxMyVariantData : public wxVariantData | |
433 | { | |
434 | public: | |
435 | wxMyVariantData(const MyClass& value) | |
436 | { | |
437 | m_value = value; | |
438 | } | |
439 | ||
440 | virtual bool Eq(wxVariantData& WXUNUSED(data)) const | |
441 | { | |
442 | return false; | |
443 | } | |
444 | ||
445 | // What type is it? Return a string name. | |
446 | virtual wxString GetType() const { return "MyClass"; } | |
447 | ||
448 | virtual wxVariantData* Clone() const | |
449 | { | |
450 | return new wxMyVariantData(m_value); | |
451 | } | |
452 | ||
453 | protected: | |
454 | MyClass m_value; | |
455 | }; | |
456 | ||
457 | #endif // wxUSE_VARIANT | |
458 | ||
459 | ||
460 | void wxAnyTestCase::wxVariantConversions() | |
461 | { | |
462 | #if wxUSE_VARIANT | |
463 | // | |
464 | // Test various conversions to and from wxVariant | |
465 | // | |
466 | bool res; | |
467 | ||
468 | // Prepare wxVariants | |
469 | wxVariant vLong(123L); | |
470 | wxVariant vString("ABC"); | |
471 | wxVariant vDouble(TEST_FLOAT_CONST); | |
472 | wxVariant vBool((bool)true); | |
473 | wxVariant vChar('A'); | |
474 | #ifdef wxLongLong_t | |
fdfd6499 | 475 | wxVariant vLongLong(wxLongLong(wxLL(0xAABBBBCCCC))); |
0bf14ab8 JS |
476 | wxVariant vULongLong(wxULongLong(wxULL(123456))); |
477 | #endif | |
478 | wxArrayString arrstr; | |
479 | arrstr.push_back("test string"); | |
480 | wxVariant vArrayString(arrstr); | |
481 | wxVariant vDateTime(m_testDateTime); | |
482 | wxVariant vVoidPtr(dummyVoidPointer); | |
483 | wxVariant vCustomType(new wxMyVariantData(MyClass(101))); | |
484 | wxVariant vList; | |
485 | ||
486 | vList.NullList(); | |
487 | vList.Append(15); | |
488 | vList.Append("abc"); | |
489 | ||
490 | // Convert to wxAnys, and then back to wxVariants | |
491 | wxVariant variant; | |
492 | ||
493 | wxAny any(vLong); | |
494 | CPPUNIT_ASSERT(any == 123L); | |
495 | res = any.GetAs(&variant); | |
496 | CPPUNIT_ASSERT(res); | |
497 | CPPUNIT_ASSERT(variant == 123L); | |
498 | ||
499 | // Make sure integer variant has correct type information | |
500 | CPPUNIT_ASSERT(variant.GetLong() == 123); | |
501 | CPPUNIT_ASSERT(variant.GetType() == "long"); | |
502 | ||
503 | // Unsigned long wxAny should convert to "ulonglong" wxVariant | |
504 | any = 1000UL; | |
505 | res = any.GetAs(&variant); | |
506 | CPPUNIT_ASSERT(res); | |
507 | CPPUNIT_ASSERT(variant.GetType() == "ulonglong"); | |
508 | CPPUNIT_ASSERT(variant.GetLong() == 1000); | |
509 | ||
49efebe2 | 510 | any = vString; |
0bf14ab8 JS |
511 | CPPUNIT_ASSERT(any == "ABC"); |
512 | res = any.GetAs(&variant); | |
513 | CPPUNIT_ASSERT(res); | |
514 | CPPUNIT_ASSERT(variant.GetString() == "ABC"); | |
515 | ||
153107b4 JS |
516 | // Must be able to build string wxVariant from wxAny built from |
517 | // string literal | |
518 | any = "ABC"; | |
519 | res = any.GetAs(&variant); | |
520 | CPPUNIT_ASSERT(res); | |
521 | CPPUNIT_ASSERT(variant.GetType() == "string"); | |
522 | CPPUNIT_ASSERT(variant.GetString() == "ABC"); | |
523 | any = L"ABC"; | |
524 | res = any.GetAs(&variant); | |
525 | CPPUNIT_ASSERT(res); | |
526 | CPPUNIT_ASSERT(variant.GetType() == "string"); | |
527 | CPPUNIT_ASSERT(variant.GetString() == L"ABC"); | |
528 | ||
49efebe2 | 529 | any = vDouble; |
0bf14ab8 JS |
530 | double d = wxANY_AS(any, double); |
531 | CPPUNIT_ASSERT_DOUBLES_EQUAL(d, TEST_FLOAT_CONST, FEQ_DELTA); | |
532 | res = any.GetAs(&variant); | |
533 | CPPUNIT_ASSERT(res); | |
534 | CPPUNIT_ASSERT_DOUBLES_EQUAL(variant.GetDouble(), | |
535 | TEST_FLOAT_CONST, | |
536 | FEQ_DELTA); | |
537 | ||
49efebe2 | 538 | any = vBool; |
0bf14ab8 JS |
539 | CPPUNIT_ASSERT(wxANY_AS(any, bool) == true); |
540 | res = any.GetAs(&variant); | |
541 | CPPUNIT_ASSERT(res); | |
542 | CPPUNIT_ASSERT(variant.GetBool() == true); | |
543 | ||
0aaed451 | 544 | any = wxAny(vChar); |
0bf14ab8 JS |
545 | //CPPUNIT_ASSERT(wxANY_AS(any, wxUniChar) == 'A'); |
546 | res = any.GetAs(&variant); | |
547 | CPPUNIT_ASSERT(res); | |
548 | CPPUNIT_ASSERT(variant.GetChar() == 'A'); | |
549 | ||
550 | #ifdef wxLongLong_t | |
0aaed451 | 551 | any = wxAny(vLongLong); |
fdfd6499 | 552 | CPPUNIT_ASSERT(any == wxLL(0xAABBBBCCCC)); |
0bf14ab8 JS |
553 | res = any.GetAs(&variant); |
554 | CPPUNIT_ASSERT(res); | |
0bf14ab8 | 555 | CPPUNIT_ASSERT(variant.GetType() == "longlong"); |
fdfd6499 JS |
556 | CPPUNIT_ASSERT(variant.GetLongLong() == wxLongLong(wxLL(0xAABBBBCCCC))); |
557 | ||
558 | #if LONG_MAX == wxINT64_MAX | |
559 | // As a sanity check, test that wxVariant of type 'long' converts | |
560 | // seamlessly to 'longlong' (on some 64-bit systems) | |
561 | any = 0xAABBBBCCCCL; | |
562 | res = any.GetAs(&variant); | |
563 | CPPUNIT_ASSERT(variant.GetLongLong() == wxLongLong(wxLL(0xAABBBBCCCC))); | |
564 | #endif | |
0bf14ab8 | 565 | |
0aaed451 | 566 | any = wxAny(vULongLong); |
0bf14ab8 JS |
567 | CPPUNIT_ASSERT(any == wxLL(123456)); |
568 | res = any.GetAs(&variant); | |
569 | CPPUNIT_ASSERT(res); | |
fdfd6499 | 570 | CPPUNIT_ASSERT(variant.GetType() == "ulonglong"); |
0bf14ab8 JS |
571 | CPPUNIT_ASSERT(variant.GetULongLong() == wxULongLong(wxULL(123456))); |
572 | #endif | |
573 | ||
574 | // Cannot test equality for the rest, just test that they convert | |
575 | // back correctly. | |
0aaed451 | 576 | any = wxAny(vArrayString); |
0bf14ab8 JS |
577 | res = any.GetAs(&variant); |
578 | CPPUNIT_ASSERT(res); | |
579 | wxArrayString arrstr2 = variant.GetArrayString(); | |
580 | CPPUNIT_ASSERT(arrstr2 == arrstr); | |
581 | ||
582 | any = m_testDateTime; | |
583 | CPPUNIT_ASSERT(wxANY_AS(any, wxDateTime) == m_testDateTime); | |
0aaed451 | 584 | any = wxAny(vDateTime); |
0bf14ab8 JS |
585 | CPPUNIT_ASSERT(wxANY_AS(any, wxDateTime) == m_testDateTime); |
586 | res = any.GetAs(&variant); | |
587 | CPPUNIT_ASSERT(res); | |
588 | CPPUNIT_ASSERT(variant == m_testDateTime); | |
589 | ||
0aaed451 | 590 | any = wxAny(vVoidPtr); |
0bf14ab8 JS |
591 | res = any.GetAs(&variant); |
592 | CPPUNIT_ASSERT(res); | |
593 | CPPUNIT_ASSERT(variant.GetVoidPtr() == dummyVoidPointer); | |
594 | ||
0aaed451 | 595 | any = wxAny(vList); |
0bf14ab8 JS |
596 | CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any, wxAnyList)); |
597 | wxAnyList anyList = wxANY_AS(any, wxAnyList); | |
598 | CPPUNIT_ASSERT(anyList.GetCount() == 2); | |
599 | CPPUNIT_ASSERT(wxANY_AS((*anyList[0]), int) == 15); | |
600 | CPPUNIT_ASSERT(wxANY_AS((*anyList[1]), wxString) == "abc"); | |
601 | res = any.GetAs(&variant); | |
602 | CPPUNIT_ASSERT(res); | |
603 | CPPUNIT_ASSERT(variant.GetType() == "list"); | |
604 | CPPUNIT_ASSERT(variant.GetCount() == 2); | |
605 | CPPUNIT_ASSERT(variant[0].GetLong() == 15); | |
606 | CPPUNIT_ASSERT(variant[1].GetString() == "abc"); | |
607 | ||
0aaed451 | 608 | any = wxAny(vCustomType); |
0bf14ab8 JS |
609 | CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any, wxVariantData*)); |
610 | res = any.GetAs(&variant); | |
611 | CPPUNIT_ASSERT(res); | |
612 | CPPUNIT_ASSERT(variant.GetType() == "MyClass"); | |
613 | ||
614 | #endif // wxUSE_VARIANT | |
615 | } | |
616 | ||
39601a7f VZ |
617 | template<> |
618 | class wxAnyValueTypeImpl<MyClass> : | |
619 | public wxAnyValueTypeImplBase<MyClass> | |
620 | { | |
621 | WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>) | |
622 | public: | |
623 | wxAnyValueTypeImpl() : | |
624 | wxAnyValueTypeImplBase<MyClass>() { } | |
625 | virtual ~wxAnyValueTypeImpl() { } | |
626 | ||
627 | virtual bool ConvertValue(const wxAnyValueBuffer& src, | |
628 | wxAnyValueType* dstType, | |
629 | wxAnyValueBuffer& dst) const | |
630 | { | |
631 | MyClass value = GetValue(src); | |
632 | ||
633 | if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType, wxString) ) | |
634 | { | |
635 | wxString s = value.ToString(); | |
636 | wxAnyValueTypeImpl<wxString>::SetValue(s, dst); | |
637 | } | |
638 | else | |
639 | return false; | |
640 | ||
641 | return true; | |
642 | } | |
643 | }; | |
644 | ||
645 | // | |
646 | // Following must be placed somewhere in your source code | |
647 | WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>) | |
648 | ||
649 | void wxAnyTestCase::CustomTemplateSpecialization() | |
650 | { | |
651 | // Do only a minimal CheckType() test, as dynamic type conversion already | |
652 | // uses it a lot. | |
653 | bool res; | |
654 | MyClass myObject; | |
655 | wxAny any = myObject; | |
656 | ||
657 | CPPUNIT_ASSERT( wxANY_CHECK_TYPE(any, MyClass) ); | |
658 | MyClass myObject2 = wxANY_AS(any, MyClass); | |
659 | wxUnusedVar(myObject2); | |
660 | ||
661 | wxString str; | |
662 | res = any.GetAs(&str); | |
663 | CPPUNIT_ASSERT(res); | |
664 | CPPUNIT_ASSERT_EQUAL(str, myObject.ToString()); | |
665 | } | |
666 | ||
667 | #endif // wxUSE_ANY | |
668 |