Merge #11603: Move RPC registration out of AppInitParameterInteraction
[bitcoinplatinum.git] / src / test / univalue_tests.cpp
blob738620443765ff7254e86e786146920ebc6d0298
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 <univalue.h>
11 #include <test/test_bitcoin.h>
13 #include <boost/test/unit_test.hpp>
15 BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup)
17 BOOST_AUTO_TEST_CASE(univalue_constructor)
19 UniValue v1;
20 BOOST_CHECK(v1.isNull());
22 UniValue v2(UniValue::VSTR);
23 BOOST_CHECK(v2.isStr());
25 UniValue v3(UniValue::VSTR, "foo");
26 BOOST_CHECK(v3.isStr());
27 BOOST_CHECK_EQUAL(v3.getValStr(), "foo");
29 UniValue numTest;
30 BOOST_CHECK(numTest.setNumStr("82"));
31 BOOST_CHECK(numTest.isNum());
32 BOOST_CHECK_EQUAL(numTest.getValStr(), "82");
34 uint64_t vu64 = 82;
35 UniValue v4(vu64);
36 BOOST_CHECK(v4.isNum());
37 BOOST_CHECK_EQUAL(v4.getValStr(), "82");
39 int64_t vi64 = -82;
40 UniValue v5(vi64);
41 BOOST_CHECK(v5.isNum());
42 BOOST_CHECK_EQUAL(v5.getValStr(), "-82");
44 int vi = -688;
45 UniValue v6(vi);
46 BOOST_CHECK(v6.isNum());
47 BOOST_CHECK_EQUAL(v6.getValStr(), "-688");
49 double vd = -7.21;
50 UniValue v7(vd);
51 BOOST_CHECK(v7.isNum());
52 BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
54 std::string vs("yawn");
55 UniValue v8(vs);
56 BOOST_CHECK(v8.isStr());
57 BOOST_CHECK_EQUAL(v8.getValStr(), "yawn");
59 const char *vcs = "zappa";
60 UniValue v9(vcs);
61 BOOST_CHECK(v9.isStr());
62 BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
65 BOOST_AUTO_TEST_CASE(univalue_typecheck)
67 UniValue v1;
68 BOOST_CHECK(v1.setNumStr("1"));
69 BOOST_CHECK(v1.isNum());
70 BOOST_CHECK_THROW(v1.get_bool(), std::runtime_error);
72 UniValue v2;
73 BOOST_CHECK(v2.setBool(true));
74 BOOST_CHECK_EQUAL(v2.get_bool(), true);
75 BOOST_CHECK_THROW(v2.get_int(), std::runtime_error);
77 UniValue v3;
78 BOOST_CHECK(v3.setNumStr("32482348723847471234"));
79 BOOST_CHECK_THROW(v3.get_int64(), std::runtime_error);
80 BOOST_CHECK(v3.setNumStr("1000"));
81 BOOST_CHECK_EQUAL(v3.get_int64(), 1000);
83 UniValue v4;
84 BOOST_CHECK(v4.setNumStr("2147483648"));
85 BOOST_CHECK_EQUAL(v4.get_int64(), 2147483648);
86 BOOST_CHECK_THROW(v4.get_int(), std::runtime_error);
87 BOOST_CHECK(v4.setNumStr("1000"));
88 BOOST_CHECK_EQUAL(v4.get_int(), 1000);
89 BOOST_CHECK_THROW(v4.get_str(), std::runtime_error);
90 BOOST_CHECK_EQUAL(v4.get_real(), 1000);
91 BOOST_CHECK_THROW(v4.get_array(), std::runtime_error);
92 BOOST_CHECK_THROW(v4.getKeys(), std::runtime_error);
93 BOOST_CHECK_THROW(v4.getValues(), std::runtime_error);
94 BOOST_CHECK_THROW(v4.get_obj(), std::runtime_error);
96 UniValue v5;
97 BOOST_CHECK(v5.read("[true, 10]"));
98 BOOST_CHECK_NO_THROW(v5.get_array());
99 std::vector<UniValue> vals = v5.getValues();
100 BOOST_CHECK_THROW(vals[0].get_int(), std::runtime_error);
101 BOOST_CHECK_EQUAL(vals[0].get_bool(), true);
103 BOOST_CHECK_EQUAL(vals[1].get_int(), 10);
104 BOOST_CHECK_THROW(vals[1].get_bool(), std::runtime_error);
107 BOOST_AUTO_TEST_CASE(univalue_set)
109 UniValue v(UniValue::VSTR, "foo");
110 v.clear();
111 BOOST_CHECK(v.isNull());
112 BOOST_CHECK_EQUAL(v.getValStr(), "");
114 BOOST_CHECK(v.setObject());
115 BOOST_CHECK(v.isObject());
116 BOOST_CHECK_EQUAL(v.size(), 0);
117 BOOST_CHECK_EQUAL(v.getType(), UniValue::VOBJ);
118 BOOST_CHECK(v.empty());
120 BOOST_CHECK(v.setArray());
121 BOOST_CHECK(v.isArray());
122 BOOST_CHECK_EQUAL(v.size(), 0);
124 BOOST_CHECK(v.setStr("zum"));
125 BOOST_CHECK(v.isStr());
126 BOOST_CHECK_EQUAL(v.getValStr(), "zum");
128 BOOST_CHECK(v.setFloat(-1.01));
129 BOOST_CHECK(v.isNum());
130 BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
132 BOOST_CHECK(v.setInt((int)1023));
133 BOOST_CHECK(v.isNum());
134 BOOST_CHECK_EQUAL(v.getValStr(), "1023");
136 BOOST_CHECK(v.setInt((int64_t)-1023LL));
137 BOOST_CHECK(v.isNum());
138 BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
140 BOOST_CHECK(v.setInt((uint64_t)1023ULL));
141 BOOST_CHECK(v.isNum());
142 BOOST_CHECK_EQUAL(v.getValStr(), "1023");
144 BOOST_CHECK(v.setNumStr("-688"));
145 BOOST_CHECK(v.isNum());
146 BOOST_CHECK_EQUAL(v.getValStr(), "-688");
148 BOOST_CHECK(v.setBool(false));
149 BOOST_CHECK_EQUAL(v.isBool(), true);
150 BOOST_CHECK_EQUAL(v.isTrue(), false);
151 BOOST_CHECK_EQUAL(v.isFalse(), true);
152 BOOST_CHECK_EQUAL(v.getBool(), false);
154 BOOST_CHECK(v.setBool(true));
155 BOOST_CHECK_EQUAL(v.isBool(), true);
156 BOOST_CHECK_EQUAL(v.isTrue(), true);
157 BOOST_CHECK_EQUAL(v.isFalse(), false);
158 BOOST_CHECK_EQUAL(v.getBool(), true);
160 BOOST_CHECK(!v.setNumStr("zombocom"));
162 BOOST_CHECK(v.setNull());
163 BOOST_CHECK(v.isNull());
166 BOOST_AUTO_TEST_CASE(univalue_array)
168 UniValue arr(UniValue::VARR);
170 UniValue v((int64_t)1023LL);
171 BOOST_CHECK(arr.push_back(v));
173 std::string vStr("zippy");
174 BOOST_CHECK(arr.push_back(vStr));
176 const char *s = "pippy";
177 BOOST_CHECK(arr.push_back(s));
179 std::vector<UniValue> vec;
180 v.setStr("boing");
181 vec.push_back(v);
183 v.setStr("going");
184 vec.push_back(v);
186 BOOST_CHECK(arr.push_backV(vec));
188 BOOST_CHECK_EQUAL(arr.empty(), false);
189 BOOST_CHECK_EQUAL(arr.size(), 5);
191 BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023");
192 BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy");
193 BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy");
194 BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing");
195 BOOST_CHECK_EQUAL(arr[4].getValStr(), "going");
197 BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
199 arr.clear();
200 BOOST_CHECK(arr.empty());
201 BOOST_CHECK_EQUAL(arr.size(), 0);
204 BOOST_AUTO_TEST_CASE(univalue_object)
206 UniValue obj(UniValue::VOBJ);
207 std::string strKey, strVal;
208 UniValue v;
210 strKey = "age";
211 v.setInt(100);
212 BOOST_CHECK(obj.pushKV(strKey, v));
214 strKey = "first";
215 strVal = "John";
216 BOOST_CHECK(obj.pushKV(strKey, strVal));
218 strKey = "last";
219 const char *cVal = "Smith";
220 BOOST_CHECK(obj.pushKV(strKey, cVal));
222 strKey = "distance";
223 BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
225 strKey = "time";
226 BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
228 strKey = "calories";
229 BOOST_CHECK(obj.pushKV(strKey, (int) 12));
231 strKey = "temperature";
232 BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
234 UniValue obj2(UniValue::VOBJ);
235 BOOST_CHECK(obj2.pushKV("cat1", 9000));
236 BOOST_CHECK(obj2.pushKV("cat2", 12345));
238 BOOST_CHECK(obj.pushKVs(obj2));
240 BOOST_CHECK_EQUAL(obj.empty(), false);
241 BOOST_CHECK_EQUAL(obj.size(), 9);
243 BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
244 BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
245 BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith");
246 BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25");
247 BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
248 BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
249 BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
250 BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
251 BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");
253 BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
255 BOOST_CHECK(obj.exists("age"));
256 BOOST_CHECK(obj.exists("first"));
257 BOOST_CHECK(obj.exists("last"));
258 BOOST_CHECK(obj.exists("distance"));
259 BOOST_CHECK(obj.exists("time"));
260 BOOST_CHECK(obj.exists("calories"));
261 BOOST_CHECK(obj.exists("temperature"));
262 BOOST_CHECK(obj.exists("cat1"));
263 BOOST_CHECK(obj.exists("cat2"));
265 BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
267 std::map<std::string, UniValue::VType> objTypes;
268 objTypes["age"] = UniValue::VNUM;
269 objTypes["first"] = UniValue::VSTR;
270 objTypes["last"] = UniValue::VSTR;
271 objTypes["distance"] = UniValue::VNUM;
272 objTypes["time"] = UniValue::VNUM;
273 objTypes["calories"] = UniValue::VNUM;
274 objTypes["temperature"] = UniValue::VNUM;
275 objTypes["cat1"] = UniValue::VNUM;
276 objTypes["cat2"] = UniValue::VNUM;
277 BOOST_CHECK(obj.checkObject(objTypes));
279 objTypes["cat2"] = UniValue::VSTR;
280 BOOST_CHECK(!obj.checkObject(objTypes));
282 obj.clear();
283 BOOST_CHECK(obj.empty());
284 BOOST_CHECK_EQUAL(obj.size(), 0);
287 static const char *json1 =
288 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
290 BOOST_AUTO_TEST_CASE(univalue_readwrite)
292 UniValue v;
293 BOOST_CHECK(v.read(json1));
295 std::string strJson1(json1);
296 BOOST_CHECK(v.read(strJson1));
298 BOOST_CHECK(v.isArray());
299 BOOST_CHECK_EQUAL(v.size(), 2);
301 BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
303 UniValue obj = v[1];
304 BOOST_CHECK(obj.isObject());
305 BOOST_CHECK_EQUAL(obj.size(), 3);
307 BOOST_CHECK(obj["key1"].isStr());
308 std::string correctValue("str");
309 correctValue.push_back('\0');
310 BOOST_CHECK_EQUAL(obj["key1"].getValStr(), correctValue);
311 BOOST_CHECK(obj["key2"].isNum());
312 BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800");
313 BOOST_CHECK(obj["key3"].isObject());
315 BOOST_CHECK_EQUAL(strJson1, v.write());
317 /* Check for (correctly reporting) a parsing error if the initial
318 JSON construct is followed by more stuff. Note that whitespace
319 is, of course, exempt. */
321 BOOST_CHECK(v.read(" {}\n "));
322 BOOST_CHECK(v.isObject());
323 BOOST_CHECK(v.read(" []\n "));
324 BOOST_CHECK(v.isArray());
326 BOOST_CHECK(!v.read("@{}"));
327 BOOST_CHECK(!v.read("{} garbage"));
328 BOOST_CHECK(!v.read("[]{}"));
329 BOOST_CHECK(!v.read("{}[]"));
330 BOOST_CHECK(!v.read("{} 42"));
333 BOOST_AUTO_TEST_SUITE_END()