You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

univalue_tests.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2014 BitPay, Inc.
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <stdint.h>
  5. #include <vector>
  6. #include <string>
  7. #include <map>
  8. #include "univalue/univalue.h"
  9. #include "test/test_bitcoin.h"
  10. #include <boost/test/unit_test.hpp>
  11. using namespace std;
  12. BOOST_FIXTURE_TEST_SUITE(univalue_tests, BasicTestingSetup)
  13. BOOST_AUTO_TEST_CASE(univalue_constructor)
  14. {
  15. UniValue v1;
  16. BOOST_CHECK(v1.isNull());
  17. UniValue v2(UniValue::VSTR);
  18. BOOST_CHECK(v2.isStr());
  19. UniValue v3(UniValue::VSTR, "foo");
  20. BOOST_CHECK(v3.isStr());
  21. BOOST_CHECK_EQUAL(v3.getValStr(), "foo");
  22. UniValue numTest;
  23. BOOST_CHECK(numTest.setNumStr("82"));
  24. BOOST_CHECK(numTest.isNum());
  25. BOOST_CHECK_EQUAL(numTest.getValStr(), "82");
  26. uint64_t vu64 = 82;
  27. UniValue v4(vu64);
  28. BOOST_CHECK(v4.isNum());
  29. BOOST_CHECK_EQUAL(v4.getValStr(), "82");
  30. int64_t vi64 = -82;
  31. UniValue v5(vi64);
  32. BOOST_CHECK(v5.isNum());
  33. BOOST_CHECK_EQUAL(v5.getValStr(), "-82");
  34. int vi = -688;
  35. UniValue v6(vi);
  36. BOOST_CHECK(v6.isNum());
  37. BOOST_CHECK_EQUAL(v6.getValStr(), "-688");
  38. double vd = -7.21;
  39. UniValue v7(vd);
  40. BOOST_CHECK(v7.isNum());
  41. BOOST_CHECK_EQUAL(v7.getValStr(), "-7.21");
  42. string vs("yawn");
  43. UniValue v8(vs);
  44. BOOST_CHECK(v8.isStr());
  45. BOOST_CHECK_EQUAL(v8.getValStr(), "yawn");
  46. const char *vcs = "zappa";
  47. UniValue v9(vcs);
  48. BOOST_CHECK(v9.isStr());
  49. BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
  50. }
  51. BOOST_AUTO_TEST_CASE(univalue_set)
  52. {
  53. UniValue v(UniValue::VSTR, "foo");
  54. v.clear();
  55. BOOST_CHECK(v.isNull());
  56. BOOST_CHECK_EQUAL(v.getValStr(), "");
  57. BOOST_CHECK(v.setObject());
  58. BOOST_CHECK(v.isObject());
  59. BOOST_CHECK_EQUAL(v.count(), 0);
  60. BOOST_CHECK_EQUAL(v.getType(), UniValue::VOBJ);
  61. BOOST_CHECK(v.empty());
  62. BOOST_CHECK(v.setArray());
  63. BOOST_CHECK(v.isArray());
  64. BOOST_CHECK_EQUAL(v.count(), 0);
  65. BOOST_CHECK(v.setStr("zum"));
  66. BOOST_CHECK(v.isStr());
  67. BOOST_CHECK_EQUAL(v.getValStr(), "zum");
  68. BOOST_CHECK(v.setFloat(-1.01));
  69. BOOST_CHECK(v.isNum());
  70. BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");
  71. BOOST_CHECK(v.setInt((int)1023));
  72. BOOST_CHECK(v.isNum());
  73. BOOST_CHECK_EQUAL(v.getValStr(), "1023");
  74. BOOST_CHECK(v.setInt((int64_t)-1023LL));
  75. BOOST_CHECK(v.isNum());
  76. BOOST_CHECK_EQUAL(v.getValStr(), "-1023");
  77. BOOST_CHECK(v.setInt((uint64_t)1023ULL));
  78. BOOST_CHECK(v.isNum());
  79. BOOST_CHECK_EQUAL(v.getValStr(), "1023");
  80. BOOST_CHECK(v.setNumStr("-688"));
  81. BOOST_CHECK(v.isNum());
  82. BOOST_CHECK_EQUAL(v.getValStr(), "-688");
  83. BOOST_CHECK(v.setBool(false));
  84. BOOST_CHECK_EQUAL(v.isBool(), true);
  85. BOOST_CHECK_EQUAL(v.isTrue(), false);
  86. BOOST_CHECK_EQUAL(v.isFalse(), true);
  87. BOOST_CHECK_EQUAL(v.getBool(), false);
  88. BOOST_CHECK(v.setBool(true));
  89. BOOST_CHECK_EQUAL(v.isBool(), true);
  90. BOOST_CHECK_EQUAL(v.isTrue(), true);
  91. BOOST_CHECK_EQUAL(v.isFalse(), false);
  92. BOOST_CHECK_EQUAL(v.getBool(), true);
  93. BOOST_CHECK(!v.setNumStr("zombocom"));
  94. BOOST_CHECK(v.setNull());
  95. BOOST_CHECK(v.isNull());
  96. }
  97. BOOST_AUTO_TEST_CASE(univalue_array)
  98. {
  99. UniValue arr(UniValue::VARR);
  100. UniValue v((int64_t)1023LL);
  101. BOOST_CHECK(arr.push_back(v));
  102. string vStr("zippy");
  103. BOOST_CHECK(arr.push_back(vStr));
  104. const char *s = "pippy";
  105. BOOST_CHECK(arr.push_back(s));
  106. vector<UniValue> vec;
  107. v.setStr("boing");
  108. vec.push_back(v);
  109. v.setStr("going");
  110. vec.push_back(v);
  111. BOOST_CHECK(arr.push_backV(vec));
  112. BOOST_CHECK_EQUAL(arr.empty(), false);
  113. BOOST_CHECK_EQUAL(arr.count(), 5);
  114. BOOST_CHECK_EQUAL(arr[0].getValStr(), "1023");
  115. BOOST_CHECK_EQUAL(arr[1].getValStr(), "zippy");
  116. BOOST_CHECK_EQUAL(arr[2].getValStr(), "pippy");
  117. BOOST_CHECK_EQUAL(arr[3].getValStr(), "boing");
  118. BOOST_CHECK_EQUAL(arr[4].getValStr(), "going");
  119. BOOST_CHECK_EQUAL(arr[999].getValStr(), "");
  120. arr.clear();
  121. BOOST_CHECK(arr.empty());
  122. BOOST_CHECK_EQUAL(arr.count(), 0);
  123. }
  124. BOOST_AUTO_TEST_CASE(univalue_object)
  125. {
  126. UniValue obj(UniValue::VOBJ);
  127. string strKey, strVal;
  128. UniValue v;
  129. strKey = "age";
  130. v.setInt(100);
  131. BOOST_CHECK(obj.pushKV(strKey, v));
  132. strKey = "first";
  133. strVal = "John";
  134. BOOST_CHECK(obj.pushKV(strKey, strVal));
  135. strKey = "last";
  136. const char *cVal = "Smith";
  137. BOOST_CHECK(obj.pushKV(strKey, cVal));
  138. strKey = "distance";
  139. BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
  140. strKey = "time";
  141. BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
  142. strKey = "calories";
  143. BOOST_CHECK(obj.pushKV(strKey, (int) 12));
  144. strKey = "temperature";
  145. BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
  146. UniValue obj2(UniValue::VOBJ);
  147. BOOST_CHECK(obj2.pushKV("cat1", 9000));
  148. BOOST_CHECK(obj2.pushKV("cat2", 12345));
  149. BOOST_CHECK(obj.pushKVs(obj2));
  150. BOOST_CHECK_EQUAL(obj.empty(), false);
  151. BOOST_CHECK_EQUAL(obj.count(), 9);
  152. BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
  153. BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
  154. BOOST_CHECK_EQUAL(obj["last"].getValStr(), "Smith");
  155. BOOST_CHECK_EQUAL(obj["distance"].getValStr(), "25");
  156. BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
  157. BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
  158. BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
  159. BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
  160. BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");
  161. BOOST_CHECK_EQUAL(obj["nyuknyuknyuk"].getValStr(), "");
  162. BOOST_CHECK(obj.exists("age"));
  163. BOOST_CHECK(obj.exists("first"));
  164. BOOST_CHECK(obj.exists("last"));
  165. BOOST_CHECK(obj.exists("distance"));
  166. BOOST_CHECK(obj.exists("time"));
  167. BOOST_CHECK(obj.exists("calories"));
  168. BOOST_CHECK(obj.exists("temperature"));
  169. BOOST_CHECK(obj.exists("cat1"));
  170. BOOST_CHECK(obj.exists("cat2"));
  171. BOOST_CHECK(!obj.exists("nyuknyuknyuk"));
  172. map<string, UniValue::VType> objTypes;
  173. objTypes["age"] = UniValue::VNUM;
  174. objTypes["first"] = UniValue::VSTR;
  175. objTypes["last"] = UniValue::VSTR;
  176. objTypes["distance"] = UniValue::VNUM;
  177. objTypes["time"] = UniValue::VNUM;
  178. objTypes["calories"] = UniValue::VNUM;
  179. objTypes["temperature"] = UniValue::VNUM;
  180. objTypes["cat1"] = UniValue::VNUM;
  181. objTypes["cat2"] = UniValue::VNUM;
  182. BOOST_CHECK(obj.checkObject(objTypes));
  183. objTypes["cat2"] = UniValue::VSTR;
  184. BOOST_CHECK(!obj.checkObject(objTypes));
  185. obj.clear();
  186. BOOST_CHECK(obj.empty());
  187. BOOST_CHECK_EQUAL(obj.count(), 0);
  188. }
  189. static const char *json1 =
  190. "[1.1,{\"key1\":\"str\",\"key2\":800,\"key3\":{\"name\":\"martian\"}}]";
  191. BOOST_AUTO_TEST_CASE(univalue_readwrite)
  192. {
  193. UniValue v;
  194. BOOST_CHECK(v.read(json1));
  195. string strJson1(json1);
  196. BOOST_CHECK(v.read(strJson1));
  197. BOOST_CHECK(v.isArray());
  198. BOOST_CHECK_EQUAL(v.count(), 2);
  199. BOOST_CHECK_EQUAL(v[0].getValStr(), "1.1");
  200. UniValue obj = v[1];
  201. BOOST_CHECK(obj.isObject());
  202. BOOST_CHECK_EQUAL(obj.count(), 3);
  203. BOOST_CHECK(obj["key1"].isStr());
  204. BOOST_CHECK_EQUAL(obj["key1"].getValStr(), "str");
  205. BOOST_CHECK(obj["key2"].isNum());
  206. BOOST_CHECK_EQUAL(obj["key2"].getValStr(), "800");
  207. BOOST_CHECK(obj["key3"].isObject());
  208. BOOST_CHECK_EQUAL(strJson1, v.write());
  209. }
  210. BOOST_AUTO_TEST_SUITE_END()