Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

object.cpp
Go to the documentation of this file.
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.
5 
6 #include <stdint.h>
7 #include <vector>
8 #include <string>
9 #include <map>
10 #include <cassert>
11 #include <stdexcept>
12 #include <univalue.h>
13 
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  assert(0 && "No exception caught"); \
23  } catch (excMatch & e) { \
24  } catch (...) { \
25  assert(0 && "Wrong exception caught"); \
26  } \
27  }
28 #define BOOST_CHECK_NO_THROW(stmt) { \
29  try { \
30  (stmt); \
31  } catch (...) { \
32  assert(0); \
33  } \
34  }
35 
36 BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup)
37 
38 BOOST_AUTO_TEST_CASE(univalue_constructor)
39 {
40  UniValue v1;
41  BOOST_CHECK(v1.isNull());
42 
44  BOOST_CHECK(v2.isStr());
45 
46  UniValue v3(UniValue::VSTR, "foo");
47  BOOST_CHECK(v3.isStr());
48  BOOST_CHECK_EQUAL(v3.getValStr(), "foo");
49 
50  UniValue numTest;
51  BOOST_CHECK(numTest.setNumStr("82"));
52  BOOST_CHECK(numTest.isNum());
53  BOOST_CHECK_EQUAL(numTest.getValStr(), "82");
54 
55  uint64_t vu64 = 82;
56  UniValue v4(vu64);
57  BOOST_CHECK(v4.isNum());
58  BOOST_CHECK_EQUAL(v4.getValStr(), "82");
59 
60  int64_t vi64 = -82;
61  UniValue v5(vi64);
62  BOOST_CHECK(v5.isNum());
63  BOOST_CHECK_EQUAL(v5.getValStr(), "-82");
64 
65  int vi = -688;
66  UniValue v6(vi);
67  BOOST_CHECK(v6.isNum());
68  BOOST_CHECK_EQUAL(v6.getValStr(), "-688");
69 
70  double vd = -7.21;
71  UniValue v7(vd);
72  BOOST_CHECK(v7.isNum());
73  BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
74 
75  std::string vs("yawn");
76  UniValue v8(vs);
77  BOOST_CHECK(v8.isStr());
78  BOOST_CHECK_EQUAL(v8.getValStr(), "yawn");
79 
80  const char *vcs = "zappa";
81  UniValue v9(vcs);
82  BOOST_CHECK(v9.isStr());
83  BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
84 }
85 
86 BOOST_AUTO_TEST_CASE(univalue_typecheck)
87 {
88  UniValue v1;
89  BOOST_CHECK(v1.setNumStr("1"));
90  BOOST_CHECK(v1.isNum());
91  BOOST_CHECK_THROW(v1.get_bool(), std::runtime_error);
92 
93  UniValue v2;
94  BOOST_CHECK(v2.setBool(true));
95  BOOST_CHECK_EQUAL(v2.get_bool(), true);
96  BOOST_CHECK_THROW(v2.get_int(), std::runtime_error);
97 
98  UniValue v3;
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);
103 
104  UniValue v4;
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);
116 
117  UniValue v5;
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);
123 
124  BOOST_CHECK_EQUAL(vals[1].get_int(), 10);
125  BOOST_CHECK_THROW(vals[1].get_bool(), std::runtime_error);
126 }
127 
128 BOOST_AUTO_TEST_CASE(univalue_set)
129 {
130  UniValue v(UniValue::VSTR, "foo");
131  v.clear();
132  BOOST_CHECK(v.isNull());
133  BOOST_CHECK_EQUAL(v.getValStr(), "");
134 
135  BOOST_CHECK(v.setObject());
136  BOOST_CHECK(v.isObject());
137  BOOST_CHECK_EQUAL(v.size(), 0);
139  BOOST_CHECK(v.empty());
140 
141  BOOST_CHECK(v.setArray());
142  BOOST_CHECK(v.isArray());
143  BOOST_CHECK_EQUAL(v.size(), 0);
144 
145  BOOST_CHECK(v.setStr("zum"));
146  BOOST_CHECK(v.isStr());
147  BOOST_CHECK_EQUAL(v.getValStr(), "zum");
148 
149  BOOST_CHECK(v.setFloat(-1.01));
150  BOOST_CHECK(v.isNum());
151  BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
152 
153  BOOST_CHECK(v.setInt((int)1023));
154  BOOST_CHECK(v.isNum());
155  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
156 
157  BOOST_CHECK(v.setInt((int64_t)-1023LL));
158  BOOST_CHECK(v.isNum());
159  BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
160 
161  BOOST_CHECK(v.setInt((uint64_t)1023ULL));
162  BOOST_CHECK(v.isNum());
163  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
164 
165  BOOST_CHECK(v.setNumStr("-688"));
166  BOOST_CHECK(v.isNum());
167  BOOST_CHECK_EQUAL(v.getValStr(), "-688");
168 
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);
174 
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);
180 
181  BOOST_CHECK(!v.setNumStr("zombocom"));
182 
183  BOOST_CHECK(v.setNull());
184  BOOST_CHECK(v.isNull());
185 }
186 
187 BOOST_AUTO_TEST_CASE(univalue_array)
188 {
190 
191  UniValue v((int64_t)1023LL);
192  BOOST_CHECK(arr.push_back(v));
193 
194  std::string vStr("zippy");
195  BOOST_CHECK(arr.push_back(vStr));
196 
197  const char *s = "pippy";
198  BOOST_CHECK(arr.push_back(s));
199 
200  std::vector<UniValue> vec;
201  v.setStr("boing");
202  vec.push_back(v);
203 
204  v.setStr("going");
205  vec.push_back(v);
206 
207  BOOST_CHECK(arr.push_backV(vec));
208 
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));
213 
214  BOOST_CHECK_EQUAL(arr.empty(), false);
215  BOOST_CHECK_EQUAL(arr.size(), 9);
216 
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");
226 
227  BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
228 
229  arr.clear();
230  BOOST_CHECK(arr.empty());
231  BOOST_CHECK_EQUAL(arr.size(), 0);
232 }
233 
234 BOOST_AUTO_TEST_CASE(univalue_object)
235 {
237  std::string strKey, strVal;
238  UniValue v;
239 
240  strKey = "age";
241  v.setInt(100);
242  BOOST_CHECK(obj.pushKV(strKey, v));
243 
244  strKey = "first";
245  strVal = "John";
246  BOOST_CHECK(obj.pushKV(strKey, strVal));
247 
248  strKey = "last";
249  const char *cVal = "Smith";
250  BOOST_CHECK(obj.pushKV(strKey, cVal));
251 
252  strKey = "distance";
253  BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
254 
255  strKey = "time";
256  BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
257 
258  strKey = "calories";
259  BOOST_CHECK(obj.pushKV(strKey, (int) 12));
260 
261  strKey = "temperature";
262  BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
263 
264  UniValue obj2(UniValue::VOBJ);
265  BOOST_CHECK(obj2.pushKV("cat1", 9000));
266  BOOST_CHECK(obj2.pushKV("cat2", 12345));
267 
268  BOOST_CHECK(obj.pushKVs(obj2));
269 
270  BOOST_CHECK_EQUAL(obj.empty(), false);
271  BOOST_CHECK_EQUAL(obj.size(), 9);
272 
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");
282 
283  BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
284 
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"));
294 
295  BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
296 
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));
308 
309  objTypes["cat2"] = UniValue::VSTR;
310  BOOST_CHECK(!obj.checkObject(objTypes));
311 
312  obj.clear();
313  BOOST_CHECK(obj.empty());
314  BOOST_CHECK_EQUAL(obj.size(), 0);
316 
317  BOOST_CHECK_EQUAL(obj.setObject(), true);
318  UniValue uv;
319  uv.setInt(42);
320  obj.__pushKV("age", uv);
321  BOOST_CHECK_EQUAL(obj.size(), 1);
322  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "42");
323 
324  uv.setInt(43);
325  obj.pushKV("age", uv);
326  BOOST_CHECK_EQUAL(obj.size(), 1);
327  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "43");
328 
329  obj.pushKV("name", "foo bar");
330 
331  std::map<std::string,UniValue> kv;
332  obj.getObjMap(kv);
333  BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43");
334  BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar");
335 
336 }
337 
338 static const char *json1 =
339 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
340 
341 BOOST_AUTO_TEST_CASE(univalue_readwrite)
342 {
343  UniValue v;
344  BOOST_CHECK(v.read(json1));
345 
346  std::string strJson1(json1);
347  BOOST_CHECK(v.read(strJson1));
348 
349  BOOST_CHECK(v.isArray());
350  BOOST_CHECK_EQUAL(v.size(), 2);
351 
352  BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
353 
354  UniValue obj = v[1];
355  BOOST_CHECK(obj.isObject());
356  BOOST_CHECK_EQUAL(obj.size(), 3);
357 
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());
365 
366  BOOST_CHECK_EQUAL(strJson1, v.write());
367 
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. */
371 
372  BOOST_CHECK(v.read(" {}\n "));
373  BOOST_CHECK(v.isObject());
374  BOOST_CHECK(v.read(" []\n "));
375  BOOST_CHECK(v.isArray());
376 
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"));
382 }
383 
385 
386 int main (int argc, char *argv[])
387 {
388  univalue_constructor();
389  univalue_typecheck();
390  univalue_set();
391  univalue_array();
392  univalue_object();
393  univalue_readwrite();
394  return 0;
395 }
396 
bool isObject() const
Definition: univalue.h:85
bool setNumStr(const std::string &val)
Definition: univalue.cpp:48
bool isBool() const
Definition: univalue.h:81
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
bool get_bool() const
int main(int argc, char *argv[])
Definition: object.cpp:386
#define BOOST_AUTO_TEST_CASE(funcName)
Definition: object.cpp:15
bool read(const char *raw, size_t len)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
enum VType getType() const
Definition: univalue.h:65
bool isNum() const
Definition: univalue.h:83
bool isStr() const
Definition: univalue.h:82
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:148
const std::string & getValStr() const
Definition: univalue.h:66
static const char * json1
Definition: object.cpp:338
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
bool isFalse() const
Definition: univalue.h:80
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:159
bool exists(const std::string &key) const
Definition: univalue.h:76
bool empty() const
Definition: univalue.h:67
bool setFloat(double val)
Definition: univalue.cpp:77
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool setNull()
Definition: univalue.cpp:25
bool isNull() const
Definition: univalue.h:78
bool isTrue() const
Definition: univalue.h:79
bool getBool() const
Definition: univalue.h:71
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:181
bool setStr(const std::string &val)
Definition: univalue.cpp:88
bool setInt(uint64_t val)
Definition: univalue.cpp:59
bool setArray()
Definition: univalue.cpp:96
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:119
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
bool setObject()
Definition: univalue.cpp:103
bool setBool(bool val)
Definition: univalue.cpp:31
#define BOOST_CHECK_NO_THROW(stmt)
Definition: object.cpp:28
void clear()
Definition: univalue.cpp:17
size_t size() const
Definition: univalue.h:69
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:129
bool isArray() const
Definition: univalue.h:84
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Released under the MIT license