1 // Copyright (c) 2014 BitPay Inc.
2 // Copyright (c) 2014-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
14 #define BOOST_FIXTURE_TEST_SUITE(a, b)
15 #define BOOST_AUTO_TEST_CASE(funcName) void funcName()
16 #define BOOST_AUTO_TEST_SUITE_END()
17 #define BOOST_CHECK(expr) assert(expr)
18 #define BOOST_CHECK_EQUAL(v1, v2) assert((v1) == (v2))
19 #define BOOST_CHECK_THROW(stmt, excMatch) { \
22 assert(0 && "No exception caught"); \
23 } catch (excMatch & e) { \
25 assert(0 && "Wrong exception caught"); \
28 #define BOOST_CHECK_NO_THROW(stmt) { \
36 BOOST_FIXTURE_TEST_SUITE(univalue_tests
, BasicTestingSetup
)
38 BOOST_AUTO_TEST_CASE(univalue_constructor
)
41 BOOST_CHECK(v1
.isNull());
43 UniValue
v2(UniValue::VSTR
);
44 BOOST_CHECK(v2
.isStr());
46 UniValue
v3(UniValue::VSTR
, "foo");
47 BOOST_CHECK(v3
.isStr());
48 BOOST_CHECK_EQUAL(v3
.getValStr(), "foo");
51 BOOST_CHECK(numTest
.setNumStr("82"));
52 BOOST_CHECK(numTest
.isNum());
53 BOOST_CHECK_EQUAL(numTest
.getValStr(), "82");
57 BOOST_CHECK(v4
.isNum());
58 BOOST_CHECK_EQUAL(v4
.getValStr(), "82");
62 BOOST_CHECK(v5
.isNum());
63 BOOST_CHECK_EQUAL(v5
.getValStr(), "-82");
67 BOOST_CHECK(v6
.isNum());
68 BOOST_CHECK_EQUAL(v6
.getValStr(), "-688");
72 BOOST_CHECK(v7
.isNum());
73 BOOST_CHECK_EQUAL(v7
.getValStr(), "-7.21");
75 std::string
vs("yawn");
77 BOOST_CHECK(v8
.isStr());
78 BOOST_CHECK_EQUAL(v8
.getValStr(), "yawn");
80 const char *vcs
= "zappa";
82 BOOST_CHECK(v9
.isStr());
83 BOOST_CHECK_EQUAL(v9
.getValStr(), "zappa");
86 BOOST_AUTO_TEST_CASE(univalue_typecheck
)
89 BOOST_CHECK(v1
.setNumStr("1"));
90 BOOST_CHECK(v1
.isNum());
91 BOOST_CHECK_THROW(v1
.get_bool(), std::runtime_error
);
94 BOOST_CHECK(v2
.setBool(true));
95 BOOST_CHECK_EQUAL(v2
.get_bool(), true);
96 BOOST_CHECK_THROW(v2
.get_int(), std::runtime_error
);
99 BOOST_CHECK(v3
.setNumStr("32482348723847471234"));
100 BOOST_CHECK_THROW(v3
.get_int64(), std::runtime_error
);
101 BOOST_CHECK(v3
.setNumStr("1000"));
102 BOOST_CHECK_EQUAL(v3
.get_int64(), 1000);
105 BOOST_CHECK(v4
.setNumStr("2147483648"));
106 BOOST_CHECK_EQUAL(v4
.get_int64(), 2147483648);
107 BOOST_CHECK_THROW(v4
.get_int(), std::runtime_error
);
108 BOOST_CHECK(v4
.setNumStr("1000"));
109 BOOST_CHECK_EQUAL(v4
.get_int(), 1000);
110 BOOST_CHECK_THROW(v4
.get_str(), std::runtime_error
);
111 BOOST_CHECK_EQUAL(v4
.get_real(), 1000);
112 BOOST_CHECK_THROW(v4
.get_array(), std::runtime_error
);
113 BOOST_CHECK_THROW(v4
.getKeys(), std::runtime_error
);
114 BOOST_CHECK_THROW(v4
.getValues(), std::runtime_error
);
115 BOOST_CHECK_THROW(v4
.get_obj(), std::runtime_error
);
118 BOOST_CHECK(v5
.read("[true, 10]"));
119 BOOST_CHECK_NO_THROW(v5
.get_array());
120 std::vector
<UniValue
> vals
= v5
.getValues();
121 BOOST_CHECK_THROW(vals
[0].get_int(), std::runtime_error
);
122 BOOST_CHECK_EQUAL(vals
[0].get_bool(), true);
124 BOOST_CHECK_EQUAL(vals
[1].get_int(), 10);
125 BOOST_CHECK_THROW(vals
[1].get_bool(), std::runtime_error
);
128 BOOST_AUTO_TEST_CASE(univalue_set
)
130 UniValue
v(UniValue::VSTR
, "foo");
132 BOOST_CHECK(v
.isNull());
133 BOOST_CHECK_EQUAL(v
.getValStr(), "");
135 BOOST_CHECK(v
.setObject());
136 BOOST_CHECK(v
.isObject());
137 BOOST_CHECK_EQUAL(v
.size(), 0);
138 BOOST_CHECK_EQUAL(v
.getType(), UniValue::VOBJ
);
139 BOOST_CHECK(v
.empty());
141 BOOST_CHECK(v
.setArray());
142 BOOST_CHECK(v
.isArray());
143 BOOST_CHECK_EQUAL(v
.size(), 0);
145 BOOST_CHECK(v
.setStr("zum"));
146 BOOST_CHECK(v
.isStr());
147 BOOST_CHECK_EQUAL(v
.getValStr(), "zum");
149 BOOST_CHECK(v
.setFloat(-1.01));
150 BOOST_CHECK(v
.isNum());
151 BOOST_CHECK_EQUAL(v
.getValStr(), "-1.01");
153 BOOST_CHECK(v
.setInt((int)1023));
154 BOOST_CHECK(v
.isNum());
155 BOOST_CHECK_EQUAL(v
.getValStr(), "1023");
157 BOOST_CHECK(v
.setInt((int64_t)-1023LL));
158 BOOST_CHECK(v
.isNum());
159 BOOST_CHECK_EQUAL(v
.getValStr(), "-1023");
161 BOOST_CHECK(v
.setInt((uint64_t)1023ULL));
162 BOOST_CHECK(v
.isNum());
163 BOOST_CHECK_EQUAL(v
.getValStr(), "1023");
165 BOOST_CHECK(v
.setNumStr("-688"));
166 BOOST_CHECK(v
.isNum());
167 BOOST_CHECK_EQUAL(v
.getValStr(), "-688");
169 BOOST_CHECK(v
.setBool(false));
170 BOOST_CHECK_EQUAL(v
.isBool(), true);
171 BOOST_CHECK_EQUAL(v
.isTrue(), false);
172 BOOST_CHECK_EQUAL(v
.isFalse(), true);
173 BOOST_CHECK_EQUAL(v
.getBool(), false);
175 BOOST_CHECK(v
.setBool(true));
176 BOOST_CHECK_EQUAL(v
.isBool(), true);
177 BOOST_CHECK_EQUAL(v
.isTrue(), true);
178 BOOST_CHECK_EQUAL(v
.isFalse(), false);
179 BOOST_CHECK_EQUAL(v
.getBool(), true);
181 BOOST_CHECK(!v
.setNumStr("zombocom"));
183 BOOST_CHECK(v
.setNull());
184 BOOST_CHECK(v
.isNull());
187 BOOST_AUTO_TEST_CASE(univalue_array
)
189 UniValue
arr(UniValue::VARR
);
191 UniValue
v((int64_t)1023LL);
192 BOOST_CHECK(arr
.push_back(v
));
194 std::string
vStr("zippy");
195 BOOST_CHECK(arr
.push_back(vStr
));
197 const char *s
= "pippy";
198 BOOST_CHECK(arr
.push_back(s
));
200 std::vector
<UniValue
> vec
;
207 BOOST_CHECK(arr
.push_backV(vec
));
209 BOOST_CHECK(arr
.push_back((uint64_t) 400ULL));
210 BOOST_CHECK(arr
.push_back((int64_t) -400LL));
211 BOOST_CHECK(arr
.push_back((int) -401));
212 BOOST_CHECK(arr
.push_back(-40.1));
214 BOOST_CHECK_EQUAL(arr
.empty(), false);
215 BOOST_CHECK_EQUAL(arr
.size(), 9);
217 BOOST_CHECK_EQUAL(arr
[0].getValStr(), "1023");
218 BOOST_CHECK_EQUAL(arr
[1].getValStr(), "zippy");
219 BOOST_CHECK_EQUAL(arr
[2].getValStr(), "pippy");
220 BOOST_CHECK_EQUAL(arr
[3].getValStr(), "boing");
221 BOOST_CHECK_EQUAL(arr
[4].getValStr(), "going");
222 BOOST_CHECK_EQUAL(arr
[5].getValStr(), "400");
223 BOOST_CHECK_EQUAL(arr
[6].getValStr(), "-400");
224 BOOST_CHECK_EQUAL(arr
[7].getValStr(), "-401");
225 BOOST_CHECK_EQUAL(arr
[8].getValStr(), "-40.1");
227 BOOST_CHECK_EQUAL(arr
[999].getValStr(), "");
230 BOOST_CHECK(arr
.empty());
231 BOOST_CHECK_EQUAL(arr
.size(), 0);
234 BOOST_AUTO_TEST_CASE(univalue_object
)
236 UniValue
obj(UniValue::VOBJ
);
237 std::string strKey
, strVal
;
242 BOOST_CHECK(obj
.pushKV(strKey
, v
));
246 BOOST_CHECK(obj
.pushKV(strKey
, strVal
));
249 const char *cVal
= "Smith";
250 BOOST_CHECK(obj
.pushKV(strKey
, cVal
));
253 BOOST_CHECK(obj
.pushKV(strKey
, (int64_t) 25));
256 BOOST_CHECK(obj
.pushKV(strKey
, (uint64_t) 3600));
259 BOOST_CHECK(obj
.pushKV(strKey
, (int) 12));
261 strKey
= "temperature";
262 BOOST_CHECK(obj
.pushKV(strKey
, (double) 90.012));
264 UniValue
obj2(UniValue::VOBJ
);
265 BOOST_CHECK(obj2
.pushKV("cat1", 9000));
266 BOOST_CHECK(obj2
.pushKV("cat2", 12345));
268 BOOST_CHECK(obj
.pushKVs(obj2
));
270 BOOST_CHECK_EQUAL(obj
.empty(), false);
271 BOOST_CHECK_EQUAL(obj
.size(), 9);
273 BOOST_CHECK_EQUAL(obj
["age"].getValStr(), "100");
274 BOOST_CHECK_EQUAL(obj
["first"].getValStr(), "John");
275 BOOST_CHECK_EQUAL(obj
["last"].getValStr(), "Smith");
276 BOOST_CHECK_EQUAL(obj
["distance"].getValStr(), "25");
277 BOOST_CHECK_EQUAL(obj
["time"].getValStr(), "3600");
278 BOOST_CHECK_EQUAL(obj
["calories"].getValStr(), "12");
279 BOOST_CHECK_EQUAL(obj
["temperature"].getValStr(), "90.012");
280 BOOST_CHECK_EQUAL(obj
["cat1"].getValStr(), "9000");
281 BOOST_CHECK_EQUAL(obj
["cat2"].getValStr(), "12345");
283 BOOST_CHECK_EQUAL(obj
["nyuknyuknyuk"].getValStr(), "");
285 BOOST_CHECK(obj
.exists("age"));
286 BOOST_CHECK(obj
.exists("first"));
287 BOOST_CHECK(obj
.exists("last"));
288 BOOST_CHECK(obj
.exists("distance"));
289 BOOST_CHECK(obj
.exists("time"));
290 BOOST_CHECK(obj
.exists("calories"));
291 BOOST_CHECK(obj
.exists("temperature"));
292 BOOST_CHECK(obj
.exists("cat1"));
293 BOOST_CHECK(obj
.exists("cat2"));
295 BOOST_CHECK(!obj
.exists("nyuknyuknyuk"));
297 std::map
<std::string
, UniValue::VType
> objTypes
;
298 objTypes
["age"] = UniValue::VNUM
;
299 objTypes
["first"] = UniValue::VSTR
;
300 objTypes
["last"] = UniValue::VSTR
;
301 objTypes
["distance"] = UniValue::VNUM
;
302 objTypes
["time"] = UniValue::VNUM
;
303 objTypes
["calories"] = UniValue::VNUM
;
304 objTypes
["temperature"] = UniValue::VNUM
;
305 objTypes
["cat1"] = UniValue::VNUM
;
306 objTypes
["cat2"] = UniValue::VNUM
;
307 BOOST_CHECK(obj
.checkObject(objTypes
));
309 objTypes
["cat2"] = UniValue::VSTR
;
310 BOOST_CHECK(!obj
.checkObject(objTypes
));
313 BOOST_CHECK(obj
.empty());
314 BOOST_CHECK_EQUAL(obj
.size(), 0);
315 BOOST_CHECK_EQUAL(obj
.getType(), UniValue::VNULL
);
317 BOOST_CHECK_EQUAL(obj
.setObject(), true);
320 obj
.__pushKV("age", uv
);
321 BOOST_CHECK_EQUAL(obj
.size(), 1);
322 BOOST_CHECK_EQUAL(obj
["age"].getValStr(), "42");
325 obj
.pushKV("age", uv
);
326 BOOST_CHECK_EQUAL(obj
.size(), 1);
327 BOOST_CHECK_EQUAL(obj
["age"].getValStr(), "43");
329 obj
.pushKV("name", "foo bar");
331 std::map
<std::string
,UniValue
> kv
;
333 BOOST_CHECK_EQUAL(kv
["age"].getValStr(), "43");
334 BOOST_CHECK_EQUAL(kv
["name"].getValStr(), "foo bar");
338 static const char *json1
=
339 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
341 BOOST_AUTO_TEST_CASE(univalue_readwrite
)
344 BOOST_CHECK(v
.read(json1
));
346 std::string
strJson1(json1
);
347 BOOST_CHECK(v
.read(strJson1
));
349 BOOST_CHECK(v
.isArray());
350 BOOST_CHECK_EQUAL(v
.size(), 2);
352 BOOST_CHECK_EQUAL(v
[0].getValStr(), "1.10000000");
355 BOOST_CHECK(obj
.isObject());
356 BOOST_CHECK_EQUAL(obj
.size(), 3);
358 BOOST_CHECK(obj
["key1"].isStr());
359 std::string
correctValue("str");
360 correctValue
.push_back('\0');
361 BOOST_CHECK_EQUAL(obj
["key1"].getValStr(), correctValue
);
362 BOOST_CHECK(obj
["key2"].isNum());
363 BOOST_CHECK_EQUAL(obj
["key2"].getValStr(), "800");
364 BOOST_CHECK(obj
["key3"].isObject());
366 BOOST_CHECK_EQUAL(strJson1
, v
.write());
368 /* Check for (correctly reporting) a parsing error if the initial
369 JSON construct is followed by more stuff. Note that whitespace
370 is, of course, exempt. */
372 BOOST_CHECK(v
.read(" {}\n "));
373 BOOST_CHECK(v
.isObject());
374 BOOST_CHECK(v
.read(" []\n "));
375 BOOST_CHECK(v
.isArray());
377 BOOST_CHECK(!v
.read("@{}"));
378 BOOST_CHECK(!v
.read("{} garbage"));
379 BOOST_CHECK(!v
.read("[]{}"));
380 BOOST_CHECK(!v
.read("{}[]"));
381 BOOST_CHECK(!v
.read("{} 42"));
384 BOOST_AUTO_TEST_SUITE_END()
386 int main (int argc
, char *argv
[])
388 univalue_constructor();
389 univalue_typecheck();
393 univalue_readwrite();