JSONTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "tests/Test.h"
  8. #include "include/core/SkStream.h"
  9. #include "include/core/SkString.h"
  10. #include "src/core/SkArenaAlloc.h"
  11. #include "src/utils/SkJSON.h"
  12. using namespace skjson;
  13. DEF_TEST(JSON_Parse, reporter) {
  14. static constexpr struct {
  15. const char* in;
  16. const char* out;
  17. } g_tests[] = {
  18. { "" , nullptr },
  19. { "[" , nullptr },
  20. { "]" , nullptr },
  21. { "[[]" , nullptr },
  22. { "[]]" , nullptr },
  23. { "[]f" , nullptr },
  24. { "{" , nullptr },
  25. { "}" , nullptr },
  26. { "{{}" , nullptr },
  27. { "{}}" , nullptr },
  28. { "{}f" , nullptr },
  29. { "{]" , nullptr },
  30. { "[}" , nullptr },
  31. { "{\"}" , nullptr },
  32. { "[\"]" , nullptr },
  33. { "1" , nullptr },
  34. { "true" , nullptr },
  35. { "false", nullptr },
  36. { "null" , nullptr },
  37. { "[nulll]" , nullptr },
  38. { "[false2]", nullptr },
  39. { "[true:]" , nullptr },
  40. { "[1 2]" , nullptr },
  41. { "[1,,2]" , nullptr },
  42. { "[1,2,]" , nullptr },
  43. { "[,1,2]" , nullptr },
  44. { "[ \"foo" , nullptr },
  45. { "[ \"fo\0o\" ]" , nullptr },
  46. { "{\"\":{}" , nullptr },
  47. { "{ null }" , nullptr },
  48. { "{ \"k\" : }" , nullptr },
  49. { "{ : null }" , nullptr },
  50. { "{ \"k\" : : null }" , nullptr },
  51. { "{ \"k\" : null , }" , nullptr },
  52. { "{ \"k\" : null \"k\" : 1 }", nullptr },
  53. {R"zzz(["\)zzz" , nullptr},
  54. {R"zzz(["\])zzz" , nullptr},
  55. {R"zzz(["\"])zzz" , nullptr},
  56. {R"zzz(["\z"])zzz" , nullptr},
  57. {R"zzz(["\u"])zzz" , nullptr},
  58. {R"zzz(["\u0"])zzz" , nullptr},
  59. {R"zzz(["\u00"])zzz" , nullptr},
  60. {R"zzz(["\u000"])zzz", nullptr},
  61. { "[]" , "[]" },
  62. { " \n\r\t [ \n\r\t ] \n\r\t " , "[]" },
  63. { "[[]]" , "[[]]" },
  64. { "[ null ]" , "[null]" },
  65. { "[ true ]" , "[true]" },
  66. { "[ false ]" , "[false]" },
  67. { "[ 0 ]" , "[0]" },
  68. { "[ 1 ]" , "[1]" },
  69. { "[ 1.248 ]" , "[1.248]" },
  70. { "[ \"\" ]" , "[\"\"]" },
  71. { "[ \"foo{bar}baz\" ]" , "[\"foo{bar}baz\"]" },
  72. { "[ \" f o o \" ]" , "[\" f o o \"]" },
  73. { "[ \"123456\" ]" , "[\"123456\"]" },
  74. { "[ \"1234567\" ]" , "[\"1234567\"]" },
  75. { "[ \"12345678\" ]" , "[\"12345678\"]" },
  76. { "[ \"123456789\" ]" , "[\"123456789\"]" },
  77. { "[ null , true, false,0,12.8 ]", "[null,true,false,0,12.8]" },
  78. { "{}" , "{}" },
  79. { " \n\r\t { \n\r\t } \n\r\t " , "{}" },
  80. { "{ \"k\" : null }" , "{\"k\":null}" },
  81. { "{ \"foo{\" : \"bar}baz\" }" , "{\"foo{\":\"bar}baz\"}" },
  82. { "{ \"k1\" : null, \"k2 \":0 }", "{\"k1\":null,\"k2 \":0}" },
  83. { "{ \"k1\" : null, \"k1\":0 }" , "{\"k1\":null,\"k1\":0}" },
  84. { "{ \"k1\" : null, \n\
  85. \"k2\" : 0, \n\
  86. \"k3\" : [ \n\
  87. true, \r\n\
  88. { \"kk1\" : \"foo\" , \n\
  89. \"kk2\" : \"bar\" , \n\
  90. \"kk3\" : 1.28 , \n\
  91. \"kk4\" : [ 42 ] \n\
  92. } , \n\
  93. \"boo\" , \n\
  94. null \n\
  95. ] \n\
  96. }",
  97. "{\"k1\":null,\"k2\":0,\"k3\":[true,"
  98. "{\"kk1\":\"foo\",\"kk2\":\"bar\",\"kk3\":1.28,\"kk4\":[42]},\"boo\",null]}" },
  99. {R"zzz(["\""])zzz" , "[\"\"\"]"},
  100. {R"zzz(["\\"])zzz" , "[\"\\\"]"},
  101. {R"zzz(["\/"])zzz" , "[\"/\"]" },
  102. {R"zzz(["\b"])zzz" , "[\"\b\"]"},
  103. {R"zzz(["\f"])zzz" , "[\"\f\"]"},
  104. {R"zzz(["\n"])zzz" , "[\"\n\"]"},
  105. {R"zzz(["\r"])zzz" , "[\"\r\"]"},
  106. {R"zzz(["\t"])zzz" , "[\"\t\"]"},
  107. {R"zzz(["\u1234"])zzz", "[\"\u1234\"]"},
  108. {R"zzz(["foo\"bar"])zzz" , "[\"foo\"bar\"]"},
  109. {R"zzz(["foo\\bar"])zzz" , "[\"foo\\bar\"]"},
  110. {R"zzz(["foo\/bar"])zzz" , "[\"foo/bar\"]" },
  111. {R"zzz(["foo\bbar"])zzz" , "[\"foo\bbar\"]"},
  112. {R"zzz(["foo\fbar"])zzz" , "[\"foo\fbar\"]"},
  113. {R"zzz(["foo\nbar"])zzz" , "[\"foo\nbar\"]"},
  114. {R"zzz(["foo\rbar"])zzz" , "[\"foo\rbar\"]"},
  115. {R"zzz(["foo\tbar"])zzz" , "[\"foo\tbar\"]"},
  116. {R"zzz(["foo\u1234bar"])zzz", "[\"foo\u1234bar\"]"},
  117. };
  118. for (const auto& tst : g_tests) {
  119. DOM dom(tst.in, strlen(tst.in));
  120. const auto success = !dom.root().is<NullValue>();
  121. REPORTER_ASSERT(reporter, success == (tst.out != nullptr));
  122. if (!success) continue;
  123. SkDynamicMemoryWStream str;
  124. dom.write(&str);
  125. str.write8('\0');
  126. auto data = str.detachAsData();
  127. REPORTER_ASSERT(reporter, !strcmp(tst.out, static_cast<const char*>(data->data())));
  128. }
  129. }
  130. template <typename T, typename VT>
  131. static void check_primitive(skiatest::Reporter* reporter, const Value& v, T pv,
  132. bool is_type) {
  133. REPORTER_ASSERT(reporter, v.is<VT>() == is_type);
  134. const VT* cast_t = v;
  135. REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_type);
  136. if (is_type) {
  137. REPORTER_ASSERT(reporter, &v.as<VT>() == cast_t);
  138. REPORTER_ASSERT(reporter, *v.as<VT>() == pv);
  139. }
  140. }
  141. template <typename T>
  142. static void check_vector(skiatest::Reporter* reporter, const Value& v, size_t expected_size,
  143. bool is_vector) {
  144. REPORTER_ASSERT(reporter, v.is<T>() == is_vector);
  145. const T* cast_t = v;
  146. REPORTER_ASSERT(reporter, (cast_t != nullptr) == is_vector);
  147. if (is_vector) {
  148. const auto& vec = v.as<T>();
  149. REPORTER_ASSERT(reporter, &vec == cast_t);
  150. REPORTER_ASSERT(reporter, vec.size() == expected_size);
  151. REPORTER_ASSERT(reporter, vec.begin() != nullptr);
  152. REPORTER_ASSERT(reporter, vec.end() == vec.begin() + expected_size);
  153. }
  154. }
  155. static void check_string(skiatest::Reporter* reporter, const Value& v, const char* s) {
  156. check_vector<StringValue>(reporter, v, s ? strlen(s) : 0, !!s);
  157. if (s) {
  158. REPORTER_ASSERT(reporter, !strcmp(v.as<StringValue>().begin(), s));
  159. }
  160. }
  161. DEF_TEST(JSON_DOM_visit, reporter) {
  162. static constexpr char json[] = "{ \n\
  163. \"k1\": null, \n\
  164. \"k2\": false, \n\
  165. \"k3\": true, \n\
  166. \"k4\": 42, \n\
  167. \"k5\": .75, \n\
  168. \"k6\": \"foo\", \n\
  169. \"k7\": [ 1, true, \"bar\" ], \n\
  170. \"k8\": { \"kk1\": 2, \"kk2\": false, \"kk1\": \"baz\" } \n\
  171. }";
  172. DOM dom(json, strlen(json));
  173. const auto& jroot = dom.root().as<ObjectValue>();
  174. REPORTER_ASSERT(reporter, jroot.is<ObjectValue>());
  175. {
  176. const auto& v = jroot["k1"];
  177. REPORTER_ASSERT(reporter, v.is<NullValue>());
  178. check_primitive<bool, BoolValue>(reporter, v, false, false);
  179. check_primitive<float, NumberValue>(reporter, v, 0, false);
  180. check_string(reporter, v, nullptr);
  181. check_vector<ArrayValue >(reporter, v, 0, false);
  182. check_vector<ObjectValue>(reporter, v, 0, false);
  183. }
  184. {
  185. const auto& v = jroot["k2"];
  186. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  187. check_primitive<bool, BoolValue>(reporter, v, false, true);
  188. check_primitive<float, NumberValue>(reporter, v, 0, false);
  189. check_string(reporter, v, nullptr);
  190. check_vector<ArrayValue >(reporter, v, 0, false);
  191. check_vector<ObjectValue>(reporter, v, 0, false);
  192. }
  193. {
  194. const auto& v = jroot["k3"];
  195. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  196. check_primitive<bool, BoolValue>(reporter, v, true, true);
  197. check_primitive<float, NumberValue>(reporter, v, 0, false);
  198. check_string(reporter, v, nullptr);
  199. check_vector<ArrayValue >(reporter, v, 0, false);
  200. check_vector<ObjectValue>(reporter, v, 0, false);
  201. }
  202. {
  203. const auto& v = jroot["k4"];
  204. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  205. check_primitive<bool, BoolValue>(reporter, v, false, false);
  206. check_primitive<float, NumberValue>(reporter, v, 42, true);
  207. check_string(reporter, v, nullptr);
  208. check_vector<ArrayValue >(reporter, v, 0, false);
  209. check_vector<ObjectValue>(reporter, v, 0, false);
  210. }
  211. {
  212. const auto& v = jroot["k5"];
  213. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  214. check_primitive<bool, BoolValue>(reporter, v, false, false);
  215. check_primitive<float, NumberValue>(reporter, v, .75f, true);
  216. check_string(reporter, v, nullptr);
  217. check_vector<ArrayValue >(reporter, v, 0, false);
  218. check_vector<ObjectValue>(reporter, v, 0, false);
  219. }
  220. {
  221. const auto& v = jroot["k6"];
  222. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  223. check_primitive<bool, BoolValue>(reporter, v, false, false);
  224. check_primitive<float, NumberValue>(reporter, v, 0, false);
  225. check_string(reporter, v, "foo");
  226. check_vector<ArrayValue >(reporter, v, 0, false);
  227. check_vector<ObjectValue>(reporter, v, 0, false);
  228. }
  229. {
  230. const auto& v = jroot["k7"];
  231. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  232. check_primitive<bool, BoolValue>(reporter, v, false, false);
  233. check_primitive<float, NumberValue>(reporter, v, 0, false);
  234. check_string(reporter, v, nullptr);
  235. check_vector<ObjectValue>(reporter, v, 0, false);
  236. check_vector<ArrayValue >(reporter, v, 3, true);
  237. check_primitive<float, NumberValue>(reporter, v.as<ArrayValue>()[0], 1, true);
  238. check_primitive<bool, BoolValue>(reporter, v.as<ArrayValue>()[1], true, true);
  239. check_vector<StringValue>(reporter, v.as<ArrayValue>()[2], 3, true);
  240. }
  241. {
  242. const auto& v = jroot["k8"];
  243. REPORTER_ASSERT(reporter, !v.is<NullValue>());
  244. check_primitive<bool, BoolValue>(reporter, v, false, false);
  245. check_primitive<float, NumberValue>(reporter, v, 0, false);
  246. check_string(reporter, v, nullptr);
  247. check_vector<ArrayValue >(reporter, v, 0, false);
  248. check_vector<ObjectValue>(reporter, v, 3, true);
  249. const auto& m0 = v.as<ObjectValue>().begin()[0];
  250. check_string(reporter, m0.fKey, "kk1");
  251. check_primitive<float, NumberValue>(reporter, m0.fValue, 2, true);
  252. const auto& m1 = v.as<ObjectValue>().begin()[1];
  253. check_string(reporter, m1.fKey, "kk2");
  254. check_primitive<bool, BoolValue>(reporter, m1.fValue, false, true);
  255. const auto& m2 = v.as<ObjectValue>().begin()[2];
  256. check_string(reporter, m2.fKey, "kk1");
  257. check_string(reporter, m2.fValue, "baz");
  258. REPORTER_ASSERT(reporter, v.as<ObjectValue>()[""].is<NullValue>());
  259. REPORTER_ASSERT(reporter, v.as<ObjectValue>()["nosuchkey"].is<NullValue>());
  260. check_string(reporter, v.as<ObjectValue>()["kk1"], "baz");
  261. check_primitive<bool, BoolValue>(reporter, v.as<ObjectValue>()["kk2"], false, true);
  262. }
  263. }
  264. template <typename T>
  265. void check_value(skiatest::Reporter* reporter, const Value& v, const char* expected_string) {
  266. REPORTER_ASSERT(reporter, v.is<T>());
  267. const T* cast_t = v;
  268. REPORTER_ASSERT(reporter, cast_t == &v.as<T>());
  269. const auto vstr = v.toString();
  270. REPORTER_ASSERT(reporter, 0 == strcmp(expected_string, vstr.c_str()));
  271. }
  272. DEF_TEST(JSON_DOM_build, reporter) {
  273. SkArenaAlloc alloc(4096);
  274. const auto v0 = NullValue();
  275. check_value<NullValue>(reporter, v0, "null");
  276. const auto v1 = BoolValue(true);
  277. check_value<BoolValue>(reporter, v1, "true");
  278. const auto v2 = BoolValue(false);
  279. check_value<BoolValue>(reporter, v2, "false");
  280. const auto v3 = NumberValue(0);
  281. check_value<NumberValue>(reporter, v3, "0");
  282. const auto v4 = NumberValue(42);
  283. check_value<NumberValue>(reporter, v4, "42");
  284. const auto v5 = NumberValue(42.75f);
  285. check_value<NumberValue>(reporter, v5, "42.75");
  286. const auto v6 = StringValue(nullptr, 0, alloc);
  287. check_value<StringValue>(reporter, v6, "\"\"");
  288. const auto v7 = StringValue(" foo ", 5, alloc);
  289. check_value<StringValue>(reporter, v7, "\" foo \"");
  290. const auto v8 = StringValue(" foo bar baz ", 13, alloc);
  291. check_value<StringValue>(reporter, v8, "\" foo bar baz \"");
  292. const auto v9 = ArrayValue(nullptr, 0, alloc);
  293. check_value<ArrayValue>(reporter, v9, "[]");
  294. const Value values0[] = { v0, v3, v9 };
  295. const auto v10 = ArrayValue(values0, SK_ARRAY_COUNT(values0), alloc);
  296. check_value<ArrayValue>(reporter, v10, "[null,0,[]]");
  297. const auto v11 = ObjectValue(nullptr, 0, alloc);
  298. check_value<ObjectValue>(reporter, v11, "{}");
  299. const Member members0[] = {
  300. { StringValue("key_0", 5, alloc), v1 },
  301. { StringValue("key_1", 5, alloc), v4 },
  302. { StringValue("key_2", 5, alloc), v11 },
  303. };
  304. const auto v12 = ObjectValue(members0, SK_ARRAY_COUNT(members0), alloc);
  305. check_value<ObjectValue>(reporter, v12, "{"
  306. "\"key_0\":true,"
  307. "\"key_1\":42,"
  308. "\"key_2\":{}"
  309. "}");
  310. const Value values1[] = { v2, v6, v12 };
  311. const auto v13 = ArrayValue(values1, SK_ARRAY_COUNT(values1), alloc);
  312. check_value<ArrayValue>(reporter, v13, "["
  313. "false,"
  314. "\"\","
  315. "{"
  316. "\"key_0\":true,"
  317. "\"key_1\":42,"
  318. "\"key_2\":{}"
  319. "}"
  320. "]");
  321. const Member members1[] = {
  322. { StringValue("key_00", 6, alloc), v5 },
  323. { StringValue("key_01", 6, alloc), v7 },
  324. { StringValue("key_02", 6, alloc), v13 },
  325. };
  326. const auto v14 = ObjectValue(members1, SK_ARRAY_COUNT(members1), alloc);
  327. check_value<ObjectValue>(reporter, v14, "{"
  328. "\"key_00\":42.75,"
  329. "\"key_01\":\" foo \","
  330. "\"key_02\":["
  331. "false,"
  332. "\"\","
  333. "{"
  334. "\"key_0\":true,"
  335. "\"key_1\":42,"
  336. "\"key_2\":{}"
  337. "}"
  338. "]"
  339. "}");
  340. }
  341. DEF_TEST(JSON_ParseNumber, reporter) {
  342. static constexpr struct {
  343. const char* string;
  344. SkScalar value,
  345. tolerance;
  346. } gTests[] = {
  347. { "0", 0, 0 },
  348. { "1", 1, 0 },
  349. { "00000000", 0, 0 },
  350. { "00000001", 1, 0 },
  351. { "0.001", 0.001f, 0 },
  352. { "1.001", 1.001f, 0 },
  353. { "0.000001" , 0.000001f, 0 },
  354. { "1.000001" , 1.000001f, 0 },
  355. { "1000.000001", 1000.000001f, 0 },
  356. { "0.0000000001" , 0.0000000001f, 0 },
  357. { "1.0000000001" , 1.0000000001f, 0 },
  358. { "1000.0000000001", 1000.0000000001f, 0 },
  359. { "20.001111814444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444473",
  360. 20.001f, 0.001f },
  361. };
  362. for (const auto& test : gTests) {
  363. const auto json = SkStringPrintf("{ \"key\": %s }", test.string);
  364. const DOM dom(json.c_str(), json.size());
  365. const ObjectValue* jroot = dom.root();
  366. REPORTER_ASSERT(reporter, jroot);
  367. const NumberValue* jnumber = (*jroot)["key"];
  368. REPORTER_ASSERT(reporter, jnumber);
  369. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(**jnumber, test.value, test.tolerance));
  370. }
  371. }