]> git.saurik.com Git - wxWidgets.git/blob - tests/any/anytest.cpp
4b056916449cdcd787255be28dce1b28598fdf7b
[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
21 #include <math.h>
22
23 // ----------------------------------------------------------------------------
24 // test class
25 // ----------------------------------------------------------------------------
26
27 class wxAnyTestCase : public CppUnit::TestCase
28 {
29 public:
30 wxAnyTestCase();
31
32 private:
33 CPPUNIT_TEST_SUITE( wxAnyTestCase );
34 CPPUNIT_TEST( Equality );
35 CPPUNIT_TEST( As );
36 CPPUNIT_TEST( GetAs );
37 CPPUNIT_TEST( Null );
38 CPPUNIT_TEST( CustomTemplateSpecialization );
39 CPPUNIT_TEST_SUITE_END();
40
41 void Equality();
42 void As();
43 void GetAs();
44 void Null();
45 void CustomTemplateSpecialization();
46
47 wxDateTime m_testDateTime;
48
49 wxAny m_anySignedChar1;
50 wxAny m_anySignedShort1;
51 wxAny m_anySignedInt1;
52 wxAny m_anySignedLong1;
53 wxAny m_anySignedLongLong1;
54 wxAny m_anyUnsignedChar1;
55 wxAny m_anyUnsignedShort1;
56 wxAny m_anyUnsignedInt1;
57 wxAny m_anyUnsignedLong1;
58 wxAny m_anyUnsignedLongLong1;
59 wxAny m_anyStringString1;
60 wxAny m_anyCharString1;
61 wxAny m_anyWcharString1;
62 wxAny m_anyBool1;
63 wxAny m_anyFloatDouble1;
64 wxAny m_anyDoubleDouble1;
65 wxAny m_anyWxObjectPtr1;
66 wxAny m_anyVoidPtr1;
67 wxAny m_anyDateTime1;
68
69 wxAny m_anySignedChar2;
70 wxAny m_anySignedShort2;
71 wxAny m_anySignedInt2;
72 wxAny m_anySignedLong2;
73 wxAny m_anySignedLongLong2;
74 wxAny m_anyUnsignedChar2;
75 wxAny m_anyUnsignedShort2;
76 wxAny m_anyUnsignedInt2;
77 wxAny m_anyUnsignedLong2;
78 wxAny m_anyUnsignedLongLong2;
79 wxAny m_anyStringString2;
80 wxAny m_anyCharString2;
81 wxAny m_anyWcharString2;
82 wxAny m_anyBool2;
83 wxAny m_anyFloatDouble2;
84 wxAny m_anyDoubleDouble2;
85 wxAny m_anyWxObjectPtr2;
86 wxAny m_anyVoidPtr2;
87 wxAny m_anyDateTime2;
88
89 DECLARE_NO_COPY_CLASS(wxAnyTestCase)
90 };
91
92 // register in the unnamed registry so that these tests are run by default
93 CPPUNIT_TEST_SUITE_REGISTRATION( wxAnyTestCase );
94
95 // also include in it's own registry so that these tests can be run alone
96 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( wxAnyTestCase, "wxAnyTestCase" );
97
98 // Let's use a number with first digit after decimal dot less than 5,
99 // so that we don't have to worry about whether conversion from float
100 // to int truncates or rounds.
101 const double TEST_FLOAT_CONST = 123.456;
102
103 const double FEQ_DELTA = 0.001;
104
105 wxObject* dummyWxObjectPointer = reinterpret_cast<wxObject*>(1234);
106 void* dummyVoidPointer = reinterpret_cast<void*>(1234);
107
108
109 //
110 // Test both 'creation' methods
111 wxAnyTestCase::wxAnyTestCase()
112 : m_anySignedChar1((signed char)15),
113 m_anySignedShort1((signed short)15),
114 m_anySignedInt1((signed int)15),
115 m_anySignedLong1((signed long)15),
116 #ifdef wxLongLong_t
117 m_anySignedLongLong1((wxLongLong_t)15),
118 #endif
119 m_anyUnsignedChar1((unsigned char)15),
120 m_anyUnsignedShort1((unsigned short)15),
121 m_anyUnsignedInt1((unsigned int)15),
122 m_anyUnsignedLong1((unsigned long)15),
123 #ifdef wxLongLong_t
124 m_anyUnsignedLongLong1((wxULongLong_t)15),
125 #endif
126 m_anyStringString1(wxString("abc")),
127 m_anyCharString1("abc"),
128 m_anyWcharString1(L"abc"),
129 m_anyBool1(true),
130 m_anyFloatDouble1((float)TEST_FLOAT_CONST),
131 m_anyDoubleDouble1((double)TEST_FLOAT_CONST),
132 m_anyWxObjectPtr1(dummyWxObjectPointer),
133 m_anyVoidPtr1(dummyVoidPointer),
134 m_anyDateTime1(wxDateTime::Now())
135 {
136 m_testDateTime = wxDateTime::Now();
137 m_anySignedChar2 = (signed char)15;
138 m_anySignedShort2 = (signed short)15;
139 m_anySignedInt2 = (signed int)15;
140 m_anySignedLong2 = (signed long)15;
141 #ifdef wxLongLong_t
142 m_anySignedLongLong2 = (wxLongLong_t)15;
143 #endif
144 m_anyUnsignedChar2 = (unsigned char)15;
145 m_anyUnsignedShort2 = (unsigned short)15;
146 m_anyUnsignedInt2 = (unsigned int)15;
147 m_anyUnsignedLong2 = (unsigned long)15;
148 #ifdef wxLongLong_t
149 m_anyUnsignedLongLong2 = (wxULongLong_t)15;
150 #endif
151 m_anyStringString2 = wxString("abc");
152 m_anyCharString2 = "abc";
153 m_anyWcharString2 = L"abc";
154 m_anyBool2 = true;
155 m_anyFloatDouble2 = (float)TEST_FLOAT_CONST;
156 m_anyDoubleDouble2 = (double)TEST_FLOAT_CONST;
157 m_anyDateTime2 = m_testDateTime;
158 m_anyWxObjectPtr2 = dummyWxObjectPointer;
159 m_anyVoidPtr2 = dummyVoidPointer;
160 }
161
162 void wxAnyTestCase::Equality()
163 {
164 //
165 // Currently this should work
166 CPPUNIT_ASSERT(m_anyUnsignedLong1 == 15L);
167 CPPUNIT_ASSERT(m_anyUnsignedLong1 != 30L);
168 CPPUNIT_ASSERT(m_anyUnsignedLong1 == 15UL);
169 CPPUNIT_ASSERT(m_anyUnsignedLong1 != 30UL);
170 CPPUNIT_ASSERT(m_anyStringString1 == wxString("abc"));
171 CPPUNIT_ASSERT(m_anyStringString1 != wxString("ABC"));
172 CPPUNIT_ASSERT(m_anyStringString1 == "abc");
173 CPPUNIT_ASSERT(m_anyStringString1 != "ABC");
174 CPPUNIT_ASSERT(m_anyStringString1 == L"abc");
175 CPPUNIT_ASSERT(m_anyStringString1 != L"ABC");
176 CPPUNIT_ASSERT(m_anyBool1 == true);
177 CPPUNIT_ASSERT(m_anyBool1 != false);
178 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1, double),
179 wxANY_AS(m_anyDoubleDouble1, double),
180 FEQ_DELTA);
181 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble1, double),
182 TEST_FLOAT_CONST,
183 FEQ_DELTA);
184 CPPUNIT_ASSERT(m_anyWxObjectPtr1.As<wxObject*>() == dummyWxObjectPointer);
185 CPPUNIT_ASSERT(m_anyVoidPtr1.As<void*>() == dummyVoidPointer);
186
187 CPPUNIT_ASSERT(m_anySignedLong2 == 15);
188 CPPUNIT_ASSERT(m_anyStringString2 == wxString("abc"));
189 CPPUNIT_ASSERT(m_anyStringString2 == "abc");
190 CPPUNIT_ASSERT(m_anyStringString2 == L"abc");
191 CPPUNIT_ASSERT(m_anyBool2 == true);
192 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2, double),
193 wxANY_AS(m_anyDoubleDouble2, double),
194 FEQ_DELTA);
195 CPPUNIT_ASSERT_DOUBLES_EQUAL(wxANY_AS(m_anyFloatDouble2, double),
196 TEST_FLOAT_CONST,
197 FEQ_DELTA);
198 CPPUNIT_ASSERT(m_anyWxObjectPtr2.As<wxObject*>() == dummyWxObjectPointer);
199 CPPUNIT_ASSERT(m_anyVoidPtr2.As<void*>() == dummyVoidPointer);
200 }
201
202 void wxAnyTestCase::As()
203 {
204 //
205 // Test getting C++ data from wxAny without dynamic conversion
206 signed char a = wxANY_AS(m_anySignedChar1, signed char);
207 CPPUNIT_ASSERT(a == (signed int)15);
208 signed short b = wxANY_AS(m_anySignedShort1, signed short);
209 CPPUNIT_ASSERT(b == (signed int)15);
210 signed int c = wxANY_AS(m_anySignedInt1, signed int);
211 CPPUNIT_ASSERT(c == (signed int)15);
212 signed long d = wxANY_AS(m_anySignedLong1, signed long);
213 CPPUNIT_ASSERT(d == (signed int)15);
214 #ifdef wxLongLong_t
215 wxLongLong_t e = wxANY_AS(m_anySignedLongLong1, wxLongLong_t);
216 CPPUNIT_ASSERT(e == (signed int)15);
217 #endif
218 unsigned char f = wxANY_AS(m_anyUnsignedChar1, unsigned char);
219 CPPUNIT_ASSERT(f == (unsigned int)15);
220 unsigned short g = wxANY_AS(m_anyUnsignedShort1, unsigned short);
221 CPPUNIT_ASSERT(g == (unsigned int)15);
222 unsigned int h = wxANY_AS(m_anyUnsignedInt1, unsigned int);
223 CPPUNIT_ASSERT(h == (unsigned int)15);
224 unsigned long i = wxANY_AS(m_anyUnsignedLong1, unsigned long);
225 CPPUNIT_ASSERT(i == (unsigned int)15);
226 #ifdef wxLongLong_t
227 wxULongLong_t j = wxANY_AS(m_anyUnsignedLongLong1, wxULongLong_t);
228 CPPUNIT_ASSERT(j == (unsigned int)15);
229 #endif
230 wxString k = wxANY_AS(m_anyStringString1, wxString);
231 CPPUNIT_ASSERT(k == "abc");
232 wxString l = wxANY_AS(m_anyCharString1, wxString);
233 CPPUNIT_ASSERT(l == "abc");
234 wxString m = wxANY_AS(m_anyWcharString1, wxString);
235 CPPUNIT_ASSERT(m == "abc");
236 bool n = wxANY_AS(m_anyBool1, bool);
237 CPPUNIT_ASSERT(n);
238 float o = wxANY_AS(m_anyFloatDouble1, float);
239 CPPUNIT_ASSERT_DOUBLES_EQUAL(o, TEST_FLOAT_CONST, FEQ_DELTA);
240 double p = wxANY_AS(m_anyDoubleDouble1, double);
241 CPPUNIT_ASSERT_DOUBLES_EQUAL(p, TEST_FLOAT_CONST, FEQ_DELTA);
242 wxDateTime q = wxANY_AS(m_anyDateTime1, wxDateTime);
243 CPPUNIT_ASSERT(q == m_testDateTime);
244 wxObject* r = wxANY_AS(m_anyWxObjectPtr1, wxObject*);
245 CPPUNIT_ASSERT(r == dummyWxObjectPointer);
246 void* s = wxANY_AS(m_anyVoidPtr1, void*);
247 CPPUNIT_ASSERT(s == dummyVoidPointer);
248 }
249
250 void wxAnyTestCase::Null()
251 {
252 wxAny a;
253 CPPUNIT_ASSERT(a.IsNull());
254 a = -127;
255 CPPUNIT_ASSERT(a == -127);
256 a.MakeNull();
257 CPPUNIT_ASSERT(a.IsNull());
258 }
259
260 void wxAnyTestCase::GetAs()
261 {
262 //
263 // Test dynamic conversion
264 bool res;
265 long l = 0;
266 unsigned long ul = 0;
267 wxString s;
268 // Let's test against float instead of double, since the former
269 // is not the native underlying type the code converts to, but
270 // should still work, all the same.
271 float f = 0.0;
272 bool b = false;
273
274 // Conversions from signed long type
275 res = m_anySignedLong1.GetAs(&ul);
276 CPPUNIT_ASSERT(res);
277 CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(15));
278 res = m_anySignedLong1.GetAs(&s);
279 CPPUNIT_ASSERT(res);
280 CPPUNIT_ASSERT(s == "15");
281 res = m_anySignedLong1.GetAs(&f);
282 CPPUNIT_ASSERT(res);
283 CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA);
284 res = m_anySignedLong1.GetAs(&b);
285 CPPUNIT_ASSERT(res);
286 CPPUNIT_ASSERT(b == true);
287
288 // Conversions from unsigned long type
289 res = m_anyUnsignedLong1.GetAs(&l);
290 CPPUNIT_ASSERT(res);
291 CPPUNIT_ASSERT(l == static_cast<signed long>(15));
292 res = m_anyUnsignedLong1.GetAs(&s);
293 CPPUNIT_ASSERT(res);
294 CPPUNIT_ASSERT(s == "15");
295 res = m_anyUnsignedLong1.GetAs(&f);
296 CPPUNIT_ASSERT(res);
297 CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA);
298 res = m_anyUnsignedLong1.GetAs(&b);
299 CPPUNIT_ASSERT(res);
300 CPPUNIT_ASSERT(b == true);
301
302 // Conversions from default "abc" string to other types
303 // should not work.
304 CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&l));
305 CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&ul));
306 CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&f));
307 CPPUNIT_ASSERT(!m_anyStringString1.GetAs(&b));
308
309 // Let's test some other conversions from string that should work.
310 wxAny anyString;
311
312 anyString = "15";
313 res = anyString.GetAs(&l);
314 CPPUNIT_ASSERT(res);
315 CPPUNIT_ASSERT(l == static_cast<signed long>(15));
316 res = anyString.GetAs(&ul);
317 CPPUNIT_ASSERT(res);
318 CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(15));
319 res = anyString.GetAs(&f);
320 CPPUNIT_ASSERT(res);
321 CPPUNIT_ASSERT_DOUBLES_EQUAL(f, 15.0, FEQ_DELTA);
322 anyString = "TRUE";
323 res = anyString.GetAs(&b);
324 CPPUNIT_ASSERT(res);
325 CPPUNIT_ASSERT(b == true);
326 anyString = "0";
327 res = anyString.GetAs(&b);
328 CPPUNIT_ASSERT(res);
329 CPPUNIT_ASSERT(b == false);
330
331 // Conversions from bool type
332 res = m_anyBool1.GetAs(&l);
333 CPPUNIT_ASSERT(res);
334 CPPUNIT_ASSERT(l == static_cast<signed long>(1));
335 res = m_anyBool1.GetAs(&ul);
336 CPPUNIT_ASSERT(res);
337 CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(1));
338 res = m_anyBool1.GetAs(&s);
339 CPPUNIT_ASSERT(res);
340 CPPUNIT_ASSERT(s == "true");
341 CPPUNIT_ASSERT(!m_anyBool1.GetAs(&f));
342
343 // Conversions from floating point type
344 res = m_anyDoubleDouble1.GetAs(&l);
345 CPPUNIT_ASSERT(res);
346 CPPUNIT_ASSERT(l == static_cast<signed long>(123));
347 res = m_anyDoubleDouble1.GetAs(&ul);
348 CPPUNIT_ASSERT(res);
349 CPPUNIT_ASSERT_EQUAL(ul, static_cast<unsigned long>(123));
350 res = m_anyDoubleDouble1.GetAs(&s);
351 CPPUNIT_ASSERT(res);
352 double d2;
353 res = s.ToDouble(&d2);
354 CPPUNIT_ASSERT(res);
355 CPPUNIT_ASSERT_DOUBLES_EQUAL(d2, TEST_FLOAT_CONST, FEQ_DELTA);
356 }
357
358 //
359 // Test user data type specialization of wxAnyValueTypeImpl
360 //
361
362 class MyClass
363 {
364 public:
365 MyClass( int someValue = 32768 )
366 {
367 m_someValue = someValue;
368 }
369
370 wxString ToString()
371 {
372 return wxString::Format("%i", m_someValue);
373 }
374
375 private:
376 int m_someValue;
377 };
378
379
380 template<>
381 class wxAnyValueTypeImpl<MyClass> :
382 public wxAnyValueTypeImplBase<MyClass>
383 {
384 WX_DECLARE_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
385 public:
386 wxAnyValueTypeImpl() :
387 wxAnyValueTypeImplBase<MyClass>() { }
388 virtual ~wxAnyValueTypeImpl() { }
389
390 virtual bool ConvertValue(const wxAnyValueBuffer& src,
391 wxAnyValueType* dstType,
392 wxAnyValueBuffer& dst) const
393 {
394 MyClass value = GetValue(src);
395
396 if ( wxANY_VALUE_TYPE_CHECK_TYPE(dstType, wxString) )
397 {
398 wxString s = value.ToString();
399 wxAnyValueTypeImpl<wxString>::SetValue(s, dst);
400 }
401 else
402 return false;
403
404 return true;
405 }
406 };
407
408 //
409 // Following must be placed somewhere in your source code
410 WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<MyClass>)
411
412 void wxAnyTestCase::CustomTemplateSpecialization()
413 {
414 // Do only a minimal CheckType() test, as dynamic type conversion already
415 // uses it a lot.
416 bool res;
417 MyClass myObject;
418 wxAny any = myObject;
419
420 CPPUNIT_ASSERT( wxANY_CHECK_TYPE(any, MyClass) );
421 MyClass myObject2 = wxANY_AS(any, MyClass);
422 wxUnusedVar(myObject2);
423
424 wxString str;
425 res = any.GetAs(&str);
426 CPPUNIT_ASSERT(res);
427 CPPUNIT_ASSERT_EQUAL(str, myObject.ToString());
428 }
429
430 #endif // wxUSE_ANY
431