1//
2// VarTest.h
3//
4// Tests for Any types
5//
6// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
7// and Contributors.
8//
9// SPDX-License-Identifier: BSL-1.0
10//
11
12#ifndef VarTest_INCLUDED
13#define VarTest_INCLUDED
14
15
16#include "Poco/Foundation.h"
17#include "Poco/Dynamic/Var.h"
18#include "Poco/Dynamic/VarIterator.h"
19#include "Poco/Exception.h"
20#include "Poco/CppUnit/TestCase.h"
21
22
23class VarTest: public CppUnit::TestCase
24{
25public:
26 VarTest(const std::string& name);
27 ~VarTest();
28
29 void testInt8();
30 void testInt16();
31 void testInt32();
32 void testInt64();
33 void testUInt8();
34 void testUInt16();
35 void testUInt32();
36 void testUInt64();
37 void testBool();
38 void testChar();
39 void testFloat();
40 void testDouble();
41 void testLong();
42 void testULong();
43 void testString();
44 void testUDT();
45 void testConversionOperator();
46 void testComparisonOperators();
47 void testArithmeticOperators();
48 void testLimitsInt();
49 void testLimitsFloat();
50 void testCtor();
51 void testIsStruct();
52 void testIsArray();
53 void testArrayIdxOperator();
54 void testDynamicPair();
55 void testDynamicStructBasics();
56 void testOrderedDynamicStructBasics();
57 void testDynamicStructString();
58 void testOrderedDynamicStructString();
59 void testDynamicStructInt();
60 void testOrderedDynamicStructInt();
61 void testArrayToString();
62 void testArrayToStringEscape();
63 void testStructToString();
64 void testOrderedStructToString();
65 void testStructToStringEscape();
66 void testArrayOfStructsToString();
67 void testStructWithArraysToString();
68 void testJSONDeserializeString();
69 void testJSONDeserializePrimitives();
70 void testJSONDeserializeArray();
71 void testJSONDeserializeStruct();
72 void testJSONRoundtripStruct();
73 void testJSONDeserializeComplex();
74 void testDate();
75 void testEmpty();
76 void testIterator();
77
78
79 void setUp();
80 void tearDown();
81 static CppUnit::Test* suite();
82
83private:
84 void testGetIdxMustThrow(Poco::Dynamic::Var& a1, std::vector<Poco::Dynamic::Var>::size_type n);
85 void testGetIdxNoThrow(Poco::Dynamic::Var& a1, std::vector<Poco::Dynamic::Var>::size_type n);
86 template<typename T>
87 void testGetIdx(const Poco::Dynamic::Var& a1, std::vector<Poco::Dynamic::Var>::size_type n, const T& expectedResult)
88 {
89 Poco::Dynamic::Var val1 = a1[n];
90 assertTrue (val1 == expectedResult);
91
92 const Poco::Dynamic::Var c1 = a1;
93 assertTrue (a1 == c1); // silence the compiler
94 const Poco::Dynamic::Var cval1 = a1[n];
95 assertTrue (cval1 == expectedResult);
96 }
97
98
99 template<typename TL, typename TS>
100 void testLimitsSigned()
101 {
102 TL iMin = std::numeric_limits<TS>::min();
103 Poco::Dynamic::Var da = iMin - 1;
104 try { TS i; i = da.convert<TS>(); fail("must fail"); }
105 catch (Poco::RangeException&) {}
106
107 TL iMax = std::numeric_limits<TS>::max();
108 da = iMax + 1;
109 try { TS i; i = da.convert<TS>(); fail("must fail"); }
110 catch (Poco::RangeException&) {}
111 }
112
113 template<typename TL, typename TS>
114 void testLimitsFloatToInt()
115 {
116 Poco::Dynamic::Var da;
117
118 if (std::numeric_limits<TS>::is_signed)
119 {
120 TL iMin = static_cast<TL>(std::numeric_limits<TS>::min());
121 da = iMin * 10;
122 try { TS i; i = da.convert<TS>(); fail("must fail"); }
123 catch (Poco::RangeException&) {}
124 }
125
126 TL iMax = static_cast<TL>(std::numeric_limits<TS>::max());
127 da = iMax * 10;
128 try { TS i; i = da.convert<TS>(); fail("must fail"); }
129 catch (Poco::RangeException&) {}
130 }
131
132 template<typename TS, typename TU>
133 void testLimitsSignedUnsigned()
134 {
135 assertTrue (std::numeric_limits<TS>::is_signed);
136 assertTrue (!std::numeric_limits<TU>::is_signed);
137
138 TS iMin = std::numeric_limits<TS>::min();
139 Poco::Dynamic::Var dMin = iMin;
140 try { TU i; i = dMin.convert<TU>(); fail("must fail"); }
141 catch (Poco::RangeException&) {}
142
143 if(sizeof(TS) == sizeof(TU))
144 {
145 TU iMax = std::numeric_limits<TU>::max();
146 Poco::Dynamic::Var dMax = iMax;
147 try { TS i; i = dMax.convert<TS>(); fail("must fail"); }
148 catch (Poco::RangeException&) {}
149 }
150 }
151
152 template<typename TL, typename TS>
153 void testLimitsUnsigned()
154 {
155 TL iMax = std::numeric_limits<TS>::max();
156 Poco::Dynamic::Var da = iMax + 1;
157 try { TS i; i = da.convert<TS>(); fail("must fail"); }
158 catch (Poco::RangeException&) {}
159 }
160
161 template <typename T>
162 void testEmptyComparisons()
163 {
164 Poco::Dynamic::Var da;
165 T val = 0;
166
167 assertTrue (da != val);
168 assertTrue (val != da);
169 assertTrue (!(val == da));
170 assertTrue (!(da == val));
171 assertTrue (!(da < val));
172 assertTrue (!(val < da));
173 assertTrue (!(da > val));
174 assertTrue (!(val > da));
175 assertTrue (!(da <= val));
176 assertTrue (!(val <= da));
177 assertTrue (!(da >= val));
178 assertTrue (!(val >= da));
179 }
180
181 template <typename C>
182 void testContainerIterator()
183 {
184 C cont;
185 cont.push_back(1);
186 cont.push_back("2");
187 cont.push_back(3.5);
188 Poco::Dynamic::Var arr(cont);
189 assertTrue (arr.size() == 3);
190 Poco::Dynamic::Var::Iterator it = arr.begin();
191 Poco::Dynamic::Var::Iterator end = arr.end();
192 int counter = 0;
193 for (; it != end; ++it)
194 {
195 switch (++counter)
196 {
197 case 1:
198 assertTrue (*it == 1);
199 break;
200 case 2:
201 assertTrue (*it == 2);
202 break;
203 case 3:
204 assertTrue (*it == 3.5);
205 break;
206 default:
207 fail ("must fail - wrong size");
208 }
209 }
210 }
211};
212
213
214#endif // VarTest_INCLUDED
215