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