1//
2// AnyTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "AnyTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Exception.h"
15#include "Poco/Any.h"
16#include "Poco/Bugcheck.h"
17#include <vector>
18
19
20#if defined(_MSC_VER) && _MSC_VER < 1400
21 #pragma warning(disable:4800)//forcing value to bool 'true' or 'false'
22#endif
23
24
25using namespace Poco;
26
27
28class SomeClass
29{
30public:
31 int i;
32 std::string str;
33 SomeClass(int h, std::string s): i (h), str(s)
34 {
35 };
36 bool operator==(const SomeClass& other) const
37 {
38 return i == other.i && str == other.str;
39 }
40};
41
42
43AnyTest::AnyTest(const std::string& rName): CppUnit::TestCase(rName)
44{
45}
46
47
48AnyTest::~AnyTest()
49{
50}
51
52
53void AnyTest::testDefaultCtor()
54{
55 const Any value;
56
57 assertTrue (value.empty());
58 assertTrue (0 == AnyCast<int>(&value));
59 assertTrue (value.type() == typeid(void));
60}
61
62
63void AnyTest::testConvertingCtor()
64{
65 std::string text = "test message";
66 Any value = text;
67
68 assertTrue (!value.empty());
69 assertTrue (value.type() == typeid(std::string));
70 assertTrue (0 == AnyCast<int>(&value));
71 assertTrue (0 != AnyCast<std::string>(&value));
72 assertTrue (AnyCast<std::string>(value) == text);
73 assertTrue (AnyCast<std::string>(&value) != &text);
74}
75
76
77void AnyTest::testCopyCtor()
78{
79 std::string text = "test message";
80 Any original = text, copy = original;
81
82 assertTrue (!copy.empty());
83 assertTrue (original.type() == copy.type());
84 assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
85 assertTrue (text == AnyCast<std::string>(copy));
86 assertTrue (AnyCast<std::string>(&original) != AnyCast<std::string>(&copy));
87}
88
89
90void AnyTest::testCopyAssign()
91{
92 std::string text = "test message";
93 Any original = text, copy;
94 Any* assignResult = &(copy = original);
95
96 assertTrue (!copy.empty());
97 assertTrue (original.type() == copy.type());
98 assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
99 assertTrue (text == AnyCast<std::string>(copy));
100 assertTrue (AnyCast<std::string>(&original) != AnyCast<std::string>(&copy));
101 assertTrue (assignResult == &copy);
102
103 // test self assignment
104 Any& ref = original;
105 original = ref;
106 assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
107 original = original;
108 assertTrue (AnyCast<std::string>(original) == AnyCast<std::string>(copy));
109}
110
111
112void AnyTest::testConvertingAssign()
113{
114 std::string text = "test message";
115 Any value;
116 Any* assignResult = &(value = text);
117
118 assertTrue (!value.empty());
119 assertTrue (value.type() == typeid(std::string));
120 assertTrue (0 == AnyCast<int>(&value));
121 assertTrue (0 != AnyCast<std::string>(&value));
122 assertTrue (AnyCast<std::string>(value) == text);
123 assertTrue (AnyCast<std::string>(&value) != &text);
124 assertTrue (assignResult == &value);
125}
126
127
128void AnyTest::testCastToReference()
129{
130 Any a(137);
131 const Any b(a);
132
133 int& ra = AnyCast<int &>(a);
134 int const& ra_c = AnyCast<int const &>(a);
135 int volatile& ra_v = AnyCast<int volatile &>(a);
136 int const volatile& ra_cv = AnyCast<int const volatile&>(a);
137
138 // cv references to same obj
139 assertTrue (&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv);
140
141 int const & rb_c = AnyCast<int const &>(b);
142 int const volatile & rb_cv = AnyCast<int const volatile &>(b);
143
144 assertTrue (&rb_c == &rb_cv); // cv references to copied const obj
145 assertTrue (&ra != &rb_c); // copies hold different objects
146
147 ++ra;
148 int incremented = AnyCast<int>(a);
149 assertTrue (incremented == 138); // increment by reference changes value
150
151 try
152 {
153 AnyCast<char &>(a);
154 failmsg ("AnyCast to incorrect reference type");
155 }
156 catch (BadCastException&) { }
157
158 try
159 {
160 AnyCast<const char &>(b),
161 failmsg ("AnyCast to incorrect const reference type");
162 }
163 catch (BadCastException&) { }
164}
165
166
167void AnyTest::testBadCast()
168{
169 std::string text = "test message";
170 Any value = text;
171
172 try
173 {
174 AnyCast<const char *>(value);
175 fail ("must throw");
176 }
177 catch (BadCastException&) { }
178}
179
180
181void AnyTest::testSwap()
182{
183 std::string text = "test message";
184 Any original = text, swapped;
185 std::string* originalPtr = AnyCast<std::string>(&original);
186 Any* swapResult = &original.swap(swapped);
187
188 assertTrue (original.empty());
189 assertTrue (!swapped.empty());
190 assertTrue (swapped.type() == typeid(std::string));
191 assertTrue (text == AnyCast<std::string>(swapped));
192 assertTrue (0 != originalPtr);
193#ifdef POCO_NO_SOO // pointers only match when heap-allocated
194 assertTrue (originalPtr == AnyCast<std::string>(&swapped));
195#endif
196 assertTrue (swapResult == &original);
197}
198
199
200void AnyTest::testEmptyCopy()
201{
202 const Any null;
203 Any copied = null, assigned;
204 assigned = null;
205
206 assertTrue (null.empty());
207 assertTrue (copied.empty());
208 assertTrue (assigned.empty());
209}
210
211
212void AnyTest::testInt()
213{
214 Any e;
215 assertTrue (e.empty());
216
217 Any a = 13;
218 assertTrue (a.type() == typeid(int));
219 int* i = AnyCast<int>(&a);
220 assertTrue (*i == 13);
221 Any b = a;
222 assertTrue (b.type() == typeid(int));
223 int *cpyI = AnyCast<int>(&b);
224 assertTrue (*cpyI == *i);
225 *cpyI = 20;
226 assertTrue (*cpyI != *i);
227 std::string* s = AnyCast<std::string>(&a);
228 assertTrue (s == NULL);
229
230 int tmp = AnyCast<int>(a);
231 const Any c = a;
232 tmp = AnyCast<int>(a);
233}
234
235
236void AnyTest::testComplexType()
237{
238 SomeClass str(13,std::string("hello"));
239 Any a = str;
240 Any b = a;
241 assertTrue (a.type() == typeid(SomeClass));
242 assertTrue (b.type() == typeid(SomeClass));
243 SomeClass str2 = AnyCast<SomeClass>(a);
244 assertTrue (str == str2);
245 const SomeClass& strCRef = RefAnyCast<SomeClass>(a);
246 assertTrue (str == strCRef);
247 SomeClass& strRef = RefAnyCast<SomeClass>(a);
248 assertTrue (str == strRef);
249}
250
251
252void AnyTest::testVector()
253{
254 std::vector<int> tmp;
255 tmp.push_back(1);
256 tmp.push_back(2);
257 tmp.push_back(3);
258 Any a = tmp;
259 assertTrue (a.type() == typeid(std::vector<int>));
260 std::vector<int> tmp2 = AnyCast<std::vector<int> >(a);
261 assertTrue (tmp2.size() == 3);
262 const std::vector<int>& vecCRef = RefAnyCast<std::vector<int> >(a);
263 std::vector<int>& vecRef = RefAnyCast<std::vector<int> >(a);
264
265 assertTrue (vecRef[0] == 1);
266 assertTrue (vecRef[1] == 2);
267 assertTrue (vecRef[2] == 3);
268 vecRef[0] = 0;
269 assertTrue (vecRef[0] == vecCRef[0]);
270}
271
272
273void AnyTest::setUp()
274{
275}
276
277
278void AnyTest::tearDown()
279{
280}
281
282
283CppUnit::Test* AnyTest::suite()
284{
285 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AnyTest");
286
287 CppUnit_addTest(pSuite, AnyTest, testConvertingCtor);
288 CppUnit_addTest(pSuite, AnyTest, testDefaultCtor);
289 CppUnit_addTest(pSuite, AnyTest, testCopyCtor);
290 CppUnit_addTest(pSuite, AnyTest, testCopyAssign);
291 CppUnit_addTest(pSuite, AnyTest, testConvertingAssign);
292 CppUnit_addTest(pSuite, AnyTest, testBadCast);
293 CppUnit_addTest(pSuite, AnyTest, testSwap);
294 CppUnit_addTest(pSuite, AnyTest, testEmptyCopy);
295 CppUnit_addTest(pSuite, AnyTest, testCastToReference);
296 CppUnit_addTest(pSuite, AnyTest, testInt);
297 CppUnit_addTest(pSuite, AnyTest, testComplexType);
298 CppUnit_addTest(pSuite, AnyTest, testVector);
299
300 return pSuite;
301}
302