Bitcoin ABC  0.22.12
P2P Digital Currency
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 https://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(""));
146  v.reserve(3);
147  BOOST_CHECK(v.setStr("zum"));
148  BOOST_CHECK(v.isStr());
149  BOOST_CHECK_EQUAL(v.getValStr(), "zum");
150 
151  BOOST_CHECK(v.setFloat(-1.01));
152  BOOST_CHECK(v.isNum());
153  BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
154 
155  BOOST_CHECK(v.setInt((int)1023));
156  BOOST_CHECK(v.isNum());
157  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
158 
159  BOOST_CHECK(v.setInt((int64_t)-1023LL));
160  BOOST_CHECK(v.isNum());
161  BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
162 
163  BOOST_CHECK(v.setInt((uint64_t)1023ULL));
164  BOOST_CHECK(v.isNum());
165  BOOST_CHECK_EQUAL(v.getValStr(), "1023");
166 
167  BOOST_CHECK(v.setNumStr("-688"));
168  BOOST_CHECK(v.isNum());
169  BOOST_CHECK_EQUAL(v.getValStr(), "-688");
170 
171  BOOST_CHECK(v.setBool(false));
172  BOOST_CHECK_EQUAL(v.isBool(), true);
173  BOOST_CHECK_EQUAL(v.isTrue(), false);
174  BOOST_CHECK_EQUAL(v.isFalse(), true);
175  BOOST_CHECK_EQUAL(v.getBool(), false);
176 
177  BOOST_CHECK(v.setBool(true));
178  BOOST_CHECK_EQUAL(v.isBool(), true);
179  BOOST_CHECK_EQUAL(v.isTrue(), true);
180  BOOST_CHECK_EQUAL(v.isFalse(), false);
181  BOOST_CHECK_EQUAL(v.getBool(), true);
182 
183  BOOST_CHECK(!v.setNumStr("zombocom"));
184 
185  BOOST_CHECK(v.setNull());
186  BOOST_CHECK(v.isNull());
187 }
188 
189 BOOST_AUTO_TEST_CASE(univalue_array)
190 {
192  arr.reserve(9);
193 
194  UniValue v((int64_t)1023LL);
195  BOOST_CHECK(arr.push_back(v));
196 
197  std::string vStr("zippy");
198  BOOST_CHECK(arr.push_back(vStr));
199 
200  const char *s = "pippy";
201  BOOST_CHECK(arr.push_back(s));
202 
203  std::vector<UniValue> vec;
204  v.setStr("boing");
205  vec.push_back(v);
206 
207  v.setStr("going");
208  vec.push_back(v);
209 
210  BOOST_CHECK(arr.push_backV(vec));
211 
212  BOOST_CHECK(arr.push_back((uint64_t) 400ULL));
213  BOOST_CHECK(arr.push_back((int64_t) -400LL));
214  BOOST_CHECK(arr.push_back((int) -401));
215  BOOST_CHECK(arr.push_back(-40.1));
216 
217  BOOST_CHECK_EQUAL(arr.empty(), false);
218  BOOST_CHECK_EQUAL(arr.size(), 9);
219 
220  BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023");
221  BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy");
222  BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy");
223  BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing");
224  BOOST_CHECK_EQUAL(arr[4].getValStr(), "going");
225  BOOST_CHECK_EQUAL(arr[5].getValStr(), "400");
226  BOOST_CHECK_EQUAL(arr[6].getValStr(), "-400");
227  BOOST_CHECK_EQUAL(arr[7].getValStr(), "-401");
228  BOOST_CHECK_EQUAL(arr[8].getValStr(), "-40.1");
229 
230  BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
231 
232  arr.clear();
233  BOOST_CHECK(arr.empty());
234  BOOST_CHECK_EQUAL(arr.size(), 0);
235 }
236 
237 BOOST_AUTO_TEST_CASE(univalue_object)
238 {
240  std::string strKey, strVal;
241  UniValue v;
242 
243  obj.reserve(11);
244 
245  strKey = "age";
246  v.setInt(100);
247  BOOST_CHECK(obj.pushKV(strKey, v));
248 
249  strKey = "first";
250  strVal = "John";
251  BOOST_CHECK(obj.pushKV(strKey, strVal));
252 
253  strKey = "last";
254  const char *cVal = "Smith";
255  BOOST_CHECK(obj.pushKV(strKey, cVal));
256 
257  strKey = "distance";
258  BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
259 
260  strKey = "time";
261  BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
262 
263  strKey = "calories";
264  BOOST_CHECK(obj.pushKV(strKey, (int) 12));
265 
266  strKey = "temperature";
267  BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
268 
269  strKey = "moon";
270  BOOST_CHECK(obj.pushKV(strKey, true));
271 
272  strKey = "spoon";
273  BOOST_CHECK(obj.pushKV(strKey, false));
274 
275  UniValue obj2(UniValue::VOBJ);
276  BOOST_CHECK(obj2.pushKV("cat1", 9000));
277  BOOST_CHECK(obj2.pushKV("cat2", 12345));
278 
279  BOOST_CHECK(obj.pushKVs(obj2));
280 
281  BOOST_CHECK_EQUAL(obj.empty(), false);
282  BOOST_CHECK_EQUAL(obj.size(), 11);
283 
284  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
285  BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
286  BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith");
287  BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25");
288  BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
289  BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
290  BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
291  BOOST_CHECK_EQUAL(obj["moon"].getValStr(), "1");
292  BOOST_CHECK_EQUAL(obj["spoon"].getValStr(), "");
293  BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
294  BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");
295 
296  BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
297 
298  BOOST_CHECK(obj.exists("age"));
299  BOOST_CHECK(obj.exists("first"));
300  BOOST_CHECK(obj.exists("last"));
301  BOOST_CHECK(obj.exists("distance"));
302  BOOST_CHECK(obj.exists("time"));
303  BOOST_CHECK(obj.exists("calories"));
304  BOOST_CHECK(obj.exists("temperature"));
305  BOOST_CHECK(obj.exists("moon"));
306  BOOST_CHECK(obj.exists("spoon"));
307  BOOST_CHECK(obj.exists("cat1"));
308  BOOST_CHECK(obj.exists("cat2"));
309 
310  BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
311 
312  std::map<std::string, UniValue::VType> objTypes;
313  objTypes["age"] = UniValue::VNUM;
314  objTypes["first"] = UniValue::VSTR;
315  objTypes["last"] = UniValue::VSTR;
316  objTypes["distance"] = UniValue::VNUM;
317  objTypes["time"] = UniValue::VNUM;
318  objTypes["calories"] = UniValue::VNUM;
319  objTypes["temperature"] = UniValue::VNUM;
320  objTypes["moon"] = UniValue::VBOOL;
321  objTypes["spoon"] = UniValue::VBOOL;
322  objTypes["cat1"] = UniValue::VNUM;
323  objTypes["cat2"] = UniValue::VNUM;
324  BOOST_CHECK(obj.checkObject(objTypes));
325 
326  objTypes["cat2"] = UniValue::VSTR;
327  BOOST_CHECK(!obj.checkObject(objTypes));
328 
329  obj.clear();
330  BOOST_CHECK(obj.empty());
331  BOOST_CHECK_EQUAL(obj.size(), 0);
333 
334  BOOST_CHECK_EQUAL(obj.setObject(), true);
335  UniValue uv;
336  uv.setInt(42);
337  obj.__pushKV("age", uv);
338  BOOST_CHECK_EQUAL(obj.size(), 1);
339  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "42");
340 
341  uv.setInt(43);
342  obj.pushKV("age", uv);
343  BOOST_CHECK_EQUAL(obj.size(), 1);
344  BOOST_CHECK_EQUAL(obj["age"].getValStr(), "43");
345 
346  obj.pushKV("name", "foo bar");
347 
348  std::map<std::string,UniValue> kv;
349  obj.getObjMap(kv);
350  BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43");
351  BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar");
352 
353 }
354 
355 static const char *json1 =
356 "[1.10000000,{\"key1\":\"str\\u0000\",\"key2\":800,\"key3\":{\"name\":\"martian http://test.com\"}}]";
357 
358 BOOST_AUTO_TEST_CASE(univalue_readwrite)
359 {
360  UniValue v;
361  BOOST_CHECK(v.read(json1));
362 
363  std::string strJson1(json1);
364  BOOST_CHECK(v.read(strJson1));
365 
366  BOOST_CHECK(v.isArray());
367  BOOST_CHECK_EQUAL(v.size(), 2);
368 
369  BOOST_CHECK_EQUAL(v[0].getValStr(), "1.10000000");
370 
371  UniValue obj = v[1];
372  BOOST_CHECK(obj.isObject());
373  BOOST_CHECK_EQUAL(obj.size(), 3);
374 
375  BOOST_CHECK(obj["key1"].isStr());
376  std::string correctValue("str");
377  correctValue.push_back('\0');
378  BOOST_CHECK_EQUAL(obj["key1"].getValStr(), correctValue);
379  BOOST_CHECK(obj["key2"].isNum());
380  BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800");
381  BOOST_CHECK(obj["key3"].isObject());
382 
383  BOOST_CHECK_EQUAL(strJson1, v.write());
384 
385  /* Check for (correctly reporting) a parsing error if the initial
386  JSON construct is followed by more stuff. Note that whitespace
387  is, of course, exempt. */
388 
389  BOOST_CHECK(v.read(" {}\n "));
390  BOOST_CHECK(v.isObject());
391  BOOST_CHECK(v.read(" []\n "));
392  BOOST_CHECK(v.isArray());
393 
394  BOOST_CHECK(!v.read("@{}"));
395  BOOST_CHECK(!v.read("{} garbage"));
396  BOOST_CHECK(!v.read("[]{}"));
397  BOOST_CHECK(!v.read("{}[]"));
398  BOOST_CHECK(!v.read("{} 42"));
399 }
400 
402 
403 int main (int argc, char *argv[])
404 {
405  univalue_constructor();
406  univalue_typecheck();
407  univalue_set();
408  univalue_array();
409  univalue_object();
410  univalue_readwrite();
411  return 0;
412 }
413 
bool isObject() const
Definition: univalue.h:96
bool setNumStr(const std::string &val)
Definition: univalue.cpp:46
void reserve(size_t n)
Definition: univalue.h:55
bool isBool() const
Definition: univalue.h:92
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
bool get_bool() const
int main(int argc, char *argv[])
Definition: object.cpp:403
#define BOOST_AUTO_TEST_CASE(funcName)
Definition: object.cpp:15
bool read(const char *raw, size_t len)
enum VType getType() const
Definition: univalue.h:76
bool isNum() const
Definition: univalue.h:94
bool isStr() const
Definition: univalue.h:93
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
const std::string & getValStr() const
Definition: univalue.h:77
static const char * json1
Definition: object.cpp:355
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool isFalse() const
Definition: univalue.h:91
void getObjMap(std::map< std::string, UniValue > &kv) const
Definition: univalue.cpp:157
bool exists(const std::string &key) const
Definition: univalue.h:87
bool empty() const
Definition: univalue.h:78
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool setFloat(double val)
Definition: univalue.cpp:75
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool setNull()
Definition: univalue.cpp:23
bool isNull() const
Definition: univalue.h:89
bool isTrue() const
Definition: univalue.h:90
bool getBool() const
Definition: univalue.h:82
bool checkObject(const std::map< std::string, UniValue::VType > &memberTypes) const
Definition: univalue.cpp:179
bool setStr(const std::string &val)
Definition: univalue.cpp:86
bool setInt(uint64_t val)
Definition: univalue.cpp:57
bool setArray()
Definition: univalue.cpp:94
#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:117
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
bool setObject()
Definition: univalue.cpp:101
bool setBool(bool val)
Definition: univalue.cpp:29
#define BOOST_CHECK_NO_THROW(stmt)
Definition: object.cpp:28
void clear()
Definition: univalue.cpp:15
size_t size() const
Definition: univalue.h:80
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
bool isArray() const
Definition: univalue.h:95
#define BOOST_CHECK(expr)
Definition: object.cpp:17