Merge #11722: Switched sync.{cpp,h} to std threading primitives.
[bitcoinplatinum.git] / src / univalue / test / object.cpp
blob02446292a1aa4a3a76a2f7a1ed7f1a22cc138a56
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.
6 #include <stdint.h>
7 #include <vector>
8 #include <string>
9 #include <map>
10 #include <cassert>
11 #include <stdexcept>
12 #include <univalue.h>
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) { \
20 try { \
21 (stmt); \
22 } catch (excMatch & e) { \
23 } catch (...) { \
24 assert(0); \
25 } \
27 #define BOOST_CHECK_NO_THROW(stmt) { \
28 try { \
29 (stmt); \
30 } catch (...) { \
31 assert(0); \
32 } \
35 BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup)
37 BOOST_AUTO_TEST_CASE(univalue_constructor)
39 UniValue v1;
40 BOOST_CHECK(v1.isNull());
42 UniValue v2(UniValue::VSTR);
43 BOOST_CHECK(v2.isStr());
45 UniValue v3(UniValue::VSTR, "foo");
46 BOOST_CHECK(v3.isStr());
47 BOOST_CHECK_EQUAL(v3.getValStr(), "foo");
49 UniValue numTest;
50 BOOST_CHECK(numTest.setNumStr("82"));
51 BOOST_CHECK(numTest.isNum());
52 BOOST_CHECK_EQUAL(numTest.getValStr(), "82");
54 uint64_t vu64 = 82;
55 UniValue v4(vu64);
56 BOOST_CHECK(v4.isNum());
57 BOOST_CHECK_EQUAL(v4.getValStr(), "82");
59 int64_t vi64 = -82;
60 UniValue v5(vi64);
61 BOOST_CHECK(v5.isNum());
62 BOOST_CHECK_EQUAL(v5.getValStr(), "-82");
64 int vi = -688;
65 UniValue v6(vi);
66 BOOST_CHECK(v6.isNum());
67 BOOST_CHECK_EQUAL(v6.getValStr(), "-688");
69 double vd = -7.21;
70 UniValue v7(vd);
71 BOOST_CHECK(v7.isNum());
72 BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
74 std::string vs("yawn");
75 UniValue v8(vs);
76 BOOST_CHECK(v8.isStr());
77 BOOST_CHECK_EQUAL(v8.getValStr(), "yawn");
79 const char *vcs = "zappa";
80 UniValue v9(vcs);
81 BOOST_CHECK(v9.isStr());
82 BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
85 BOOST_AUTO_TEST_CASE(univalue_typecheck)
87 UniValue v1;
88 BOOST_CHECK(v1.setNumStr("1"));
89 BOOST_CHECK(v1.isNum());
90 BOOST_CHECK_THROW(v1.get_bool(), std::runtime_error);
92 UniValue v2;
93 BOOST_CHECK(v2.setBool(true));
94 BOOST_CHECK_EQUAL(v2.get_bool(), true);
95 BOOST_CHECK_THROW(v2.get_int(), std::runtime_error);
97 UniValue v3;
98 BOOST_CHECK(v3.setNumStr("32482348723847471234"));
99 BOOST_CHECK_THROW(v3.get_int64(), std::runtime_error);
100 BOOST_CHECK(v3.setNumStr("1000"));
101 BOOST_CHECK_EQUAL(v3.get_int64(), 1000);
103 UniValue v4;
104 BOOST_CHECK(v4.setNumStr("2147483648"));
105 BOOST_CHECK_EQUAL(v4.get_int64(), 2147483648);
106 BOOST_CHECK_THROW(v4.get_int(), std::runtime_error);
107 BOOST_CHECK(v4.setNumStr("1000"));
108 BOOST_CHECK_EQUAL(v4.get_int(), 1000);
109 BOOST_CHECK_THROW(v4.get_str(), std::runtime_error);
110 BOOST_CHECK_EQUAL(v4.get_real(), 1000);
111 BOOST_CHECK_THROW(v4.get_array(), std::runtime_error);
112 BOOST_CHECK_THROW(v4.getKeys(), std::runtime_error);
113 BOOST_CHECK_THROW(v4.getValues(), std::runtime_error);
114 BOOST_CHECK_THROW(v4.get_obj(), std::runtime_error);
116 UniValue v5;
117 BOOST_CHECK(v5.read("[true, 10]"));
118 BOOST_CHECK_NO_THROW(v5.get_array());
119 std::vector<UniValue> vals = v5.getValues();
120 BOOST_CHECK_THROW(vals[0].get_int(), std::runtime_error);
121 BOOST_CHECK_EQUAL(vals[0].get_bool(), true);
123 BOOST_CHECK_EQUAL(vals[1].get_int(), 10);
124 BOOST_CHECK_THROW(vals[1].get_bool(), std::runtime_error);
127 BOOST_AUTO_TEST_CASE(univalue_set)
129 UniValue v(UniValue::VSTR, "foo");
130 v.clear();
131 BOOST_CHECK(v.isNull());
132 BOOST_CHECK_EQUAL(v.getValStr(), "");
134 BOOST_CHECK(v.setObject());
135 BOOST_CHECK(v.isObject());
136 BOOST_CHECK_EQUAL(v.size(), 0);
137 BOOST_CHECK_EQUAL(v.getType(), UniValue::VOBJ);
138 BOOST_CHECK(v.empty());
140 BOOST_CHECK(v.setArray());
141 BOOST_CHECK(v.isArray());
142 BOOST_CHECK_EQUAL(v.size(), 0);
144 BOOST_CHECK(v.setStr("zum"));
145 BOOST_CHECK(v.isStr());
146 BOOST_CHECK_EQUAL(v.getValStr(), "zum");
148 BOOST_CHECK(v.setFloat(-1.01));
149 BOOST_CHECK(v.isNum());
150 BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
152 BOOST_CHECK(v.setInt((int)1023));
153 BOOST_CHECK(v.isNum());
154 BOOST_CHECK_EQUAL(v.getValStr(), "1023");
156 BOOST_CHECK(v.setInt((int64_t)-1023LL));
157 BOOST_CHECK(v.isNum());
158 BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
160 BOOST_CHECK(v.setInt((uint64_t)1023ULL));
161 BOOST_CHECK(v.isNum());
162 BOOST_CHECK_EQUAL(v.getValStr(), "1023");
164 BOOST_CHECK(v.setNumStr("-688"));
165 BOOST_CHECK(v.isNum());
166 BOOST_CHECK_EQUAL(v.getValStr(), "-688");
168 BOOST_CHECK(v.setBool(false));
169 BOOST_CHECK_EQUAL(v.isBool(), true);
170 BOOST_CHECK_EQUAL(v.isTrue(), false);
171 BOOST_CHECK_EQUAL(v.isFalse(), true);
172 BOOST_CHECK_EQUAL(v.getBool(), false);
174 BOOST_CHECK(v.setBool(true));
175 BOOST_CHECK_EQUAL(v.isBool(), true);
176 BOOST_CHECK_EQUAL(v.isTrue(), true);
177 BOOST_CHECK_EQUAL(v.isFalse(), false);
178 BOOST_CHECK_EQUAL(v.getBool(), true);
180 BOOST_CHECK(!v.setNumStr("zombocom"));
182 BOOST_CHECK(v.setNull());
183 BOOST_CHECK(v.isNull());
186 BOOST_AUTO_TEST_CASE(univalue_array)
188 UniValue arr(UniValue::VARR);
190 UniValue v((int64_t)1023LL);
191 BOOST_CHECK(arr.push_back(v));
193 std::string vStr("zippy");
194 BOOST_CHECK(arr.push_back(vStr));
196 const char *s = "pippy";
197 BOOST_CHECK(arr.push_back(s));
199 std::vector<UniValue> vec;
200 v.setStr("boing");
201 vec.push_back(v);
203 v.setStr("going");
204 vec.push_back(v);
206 BOOST_CHECK(arr.push_backV(vec));
208 BOOST_CHECK(arr.push_back((uint64_t) 400ULL));
209 BOOST_CHECK(arr.push_back((int64_t) -400LL));
210 BOOST_CHECK(arr.push_back((int) -401));
211 BOOST_CHECK(arr.push_back(-40.1));
213 BOOST_CHECK_EQUAL(arr.empty(), false);
214 BOOST_CHECK_EQUAL(arr.size(), 9);
216 BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023");
217 BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy");
218 BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy");
219 BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing");
220 BOOST_CHECK_EQUAL(arr[4].getValStr(), "going");
221 BOOST_CHECK_EQUAL(arr[5].getValStr(), "400");
222 BOOST_CHECK_EQUAL(arr[6].getValStr(), "-400");
223 BOOST_CHECK_EQUAL(arr[7].getValStr(), "-401");
224 BOOST_CHECK_EQUAL(arr[8].getValStr(), "-40.1");
226 BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
228 arr.clear();
229 BOOST_CHECK(arr.empty());
230 BOOST_CHECK_EQUAL(arr.size(), 0);
233 BOOST_AUTO_TEST_CASE(univalue_object)
235 UniValue obj(UniValue::VOBJ);
236 std::string strKey, strVal;
237 UniValue v;
239 strKey = "age";
240 v.setInt(100);
241 BOOST_CHECK(obj.pushKV(strKey, v));
243 strKey = "first";
244 strVal = "John";
245 BOOST_CHECK(obj.pushKV(strKey, strVal));
247 strKey = "last";
248 const char *cVal = "Smith";
249 BOOST_CHECK(obj.pushKV(strKey, cVal));
251 strKey = "distance";
252 BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
254 strKey = "time";
255 BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
257 strKey = "calories";
258 BOOST_CHECK(obj.pushKV(strKey, (int) 12));
260 strKey = "temperature";
261 BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
263 UniValue obj2(UniValue::VOBJ);
264 BOOST_CHECK(obj2.pushKV("cat1", 9000));
265 BOOST_CHECK(obj2.pushKV("cat2", 12345));
267 BOOST_CHECK(obj.pushKVs(obj2));
269 BOOST_CHECK_EQUAL(obj.empty(), false);
270 BOOST_CHECK_EQUAL(obj.size(), 9);
272 BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
273 BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
274 BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith");
275 BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25");
276 BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
277 BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
278 BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
279 BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
280 BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");
282 BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
284 BOOST_CHECK(obj.exists("age"));
285 BOOST_CHECK(obj.exists("first"));
286 BOOST_CHECK(obj.exists("last"));
287 BOOST_CHECK(obj.exists("distance"));
288 BOOST_CHECK(obj.exists("time"));
289 BOOST_CHECK(obj.exists("calories"));
290 BOOST_CHECK(obj.exists("temperature"));
291 BOOST_CHECK(obj.exists("cat1"));
292 BOOST_CHECK(obj.exists("cat2"));
294 BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
296 std::map<std::string, UniValue::VType> objTypes;
297 objTypes["age"] = UniValue::VNUM;
298 objTypes["first"] = UniValue::VSTR;
299 objTypes["last"] = UniValue::VSTR;
300 objTypes["distance"] = UniValue::VNUM;
301 objTypes["time"] = UniValue::VNUM;
302 objTypes["calories"] = UniValue::VNUM;
303 objTypes["temperature"] = UniValue::VNUM;
304 objTypes["cat1"] = UniValue::VNUM;
305 objTypes["cat2"] = UniValue::VNUM;
306 BOOST_CHECK(obj.checkObject(objTypes));
308 objTypes["cat2"] = UniValue::VSTR;
309 BOOST_CHECK(!obj.checkObject(objTypes));
311 obj.clear();
312 BOOST_CHECK(obj.empty());
313 BOOST_CHECK_EQUAL(obj.size(), 0);
314 BOOST_CHECK_EQUAL(obj.getType(), UniValue::VNULL);
316 BOOST_CHECK_EQUAL(obj.setObject(), true);
317 UniValue uv;
318 uv.setInt(42);
319 obj.__pushKV("age", uv);
320 BOOST_CHECK_EQUAL(obj.size(), 1);
321 BOOST_CHECK_EQUAL(obj["age"].getValStr(), "42");
323 uv.setInt(43);
324 obj.pushKV("age", uv);
325 BOOST_CHECK_EQUAL(obj.size(), 1);
326 BOOST_CHECK_EQUAL(obj["age"].getValStr(), "43");
328 obj.pushKV("name", "foo bar");
330 std::map<std::string,UniValue> kv;
331 obj.getObjMap(kv);
332 BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43");
333 BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar");
337 static const char *json1 =
338 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
340 BOOST_AUTO_TEST_CASE(univalue_readwrite)
342 UniValue v;
343 BOOST_CHECK(v.read(json1));
345 std::string strJson1(json1);
346 BOOST_CHECK(v.read(strJson1));
348 BOOST_CHECK(v.isArray());
349 BOOST_CHECK_EQUAL(v.size(), 2);
351 BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
353 UniValue obj = v[1];
354 BOOST_CHECK(obj.isObject());
355 BOOST_CHECK_EQUAL(obj.size(), 3);
357 BOOST_CHECK(obj["key1"].isStr());
358 std::string correctValue("str");
359 correctValue.push_back('\0');
360 BOOST_CHECK_EQUAL(obj["key1"].getValStr(), correctValue);
361 BOOST_CHECK(obj["key2"].isNum());
362 BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800");
363 BOOST_CHECK(obj["key3"].isObject());
365 BOOST_CHECK_EQUAL(strJson1, v.write());
367 /* Check for (correctly reporting) a parsing error if the initial
368 JSON construct is followed by more stuff. Note that whitespace
369 is, of course, exempt. */
371 BOOST_CHECK(v.read(" {}\n "));
372 BOOST_CHECK(v.isObject());
373 BOOST_CHECK(v.read(" []\n "));
374 BOOST_CHECK(v.isArray());
376 BOOST_CHECK(!v.read("@{}"));
377 BOOST_CHECK(!v.read("{} garbage"));
378 BOOST_CHECK(!v.read("[]{}"));
379 BOOST_CHECK(!v.read("{}[]"));
380 BOOST_CHECK(!v.read("{} 42"));
383 BOOST_AUTO_TEST_SUITE_END()
385 int main (int argc, char *argv[])
387 univalue_constructor();
388 univalue_typecheck();
389 univalue_set();
390 univalue_array();
391 univalue_object();
392 univalue_readwrite();
393 return 0;