compare.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("Compare JsonObject with JsonObject") {
  7. StaticJsonDocument<512> doc;
  8. SECTION("Compare with unbound") {
  9. JsonObject object = doc.to<JsonObject>();
  10. object["a"] = 1;
  11. object["b"] = "hello";
  12. JsonObject unbound;
  13. CHECK(object != unbound);
  14. CHECK_FALSE(object == unbound);
  15. CHECK_FALSE(object <= unbound);
  16. CHECK_FALSE(object >= unbound);
  17. CHECK_FALSE(object > unbound);
  18. CHECK_FALSE(object < unbound);
  19. CHECK(unbound != object);
  20. CHECK_FALSE(unbound == object);
  21. CHECK_FALSE(unbound <= object);
  22. CHECK_FALSE(unbound >= object);
  23. CHECK_FALSE(unbound > object);
  24. CHECK_FALSE(unbound < object);
  25. }
  26. SECTION("Compare with self") {
  27. JsonObject object = doc.to<JsonObject>();
  28. object["a"] = 1;
  29. object["b"] = "hello";
  30. CHECK(object == object);
  31. CHECK(object <= object);
  32. CHECK(object >= object);
  33. CHECK_FALSE(object != object);
  34. CHECK_FALSE(object > object);
  35. CHECK_FALSE(object < object);
  36. }
  37. SECTION("Compare with identical object") {
  38. JsonObject object1 = doc.createNestedObject();
  39. object1["a"] = 1;
  40. object1["b"] = "hello";
  41. object1["c"][0] = false;
  42. JsonObject object2 = doc.createNestedObject();
  43. object2["a"] = 1;
  44. object2["b"] = "hello";
  45. object2["c"][0] = false;
  46. CHECK(object1 == object2);
  47. CHECK(object1 <= object2);
  48. CHECK(object1 >= object2);
  49. CHECK_FALSE(object1 != object2);
  50. CHECK_FALSE(object1 > object2);
  51. CHECK_FALSE(object1 < object2);
  52. }
  53. SECTION("Compare with different object") {
  54. JsonObject object1 = doc.createNestedObject();
  55. object1["a"] = 1;
  56. object1["b"] = "hello1";
  57. object1["c"][0] = false;
  58. JsonObject object2 = doc.createNestedObject();
  59. object2["a"] = 1;
  60. object2["b"] = "hello2";
  61. object2["c"][0] = false;
  62. CHECK(object1 != object2);
  63. CHECK_FALSE(object1 == object2);
  64. CHECK_FALSE(object1 > object2);
  65. CHECK_FALSE(object1 < object2);
  66. CHECK_FALSE(object1 <= object2);
  67. CHECK_FALSE(object1 >= object2);
  68. }
  69. }
  70. TEST_CASE("Compare JsonObject with JsonVariant") {
  71. StaticJsonDocument<512> doc;
  72. SECTION("Compare with self") {
  73. JsonObject object = doc.to<JsonObject>();
  74. object["a"] = 1;
  75. object["b"] = "hello";
  76. JsonVariant variant = object;
  77. CHECK(object == variant);
  78. CHECK(object <= variant);
  79. CHECK(object >= variant);
  80. CHECK_FALSE(object != variant);
  81. CHECK_FALSE(object > variant);
  82. CHECK_FALSE(object < variant);
  83. CHECK(variant == object);
  84. CHECK(variant <= object);
  85. CHECK(variant >= object);
  86. CHECK_FALSE(variant != object);
  87. CHECK_FALSE(variant > object);
  88. CHECK_FALSE(variant < object);
  89. }
  90. SECTION("Compare with identical object") {
  91. JsonObject object = doc.createNestedObject();
  92. object["a"] = 1;
  93. object["b"] = "hello";
  94. object["c"][0] = false;
  95. JsonVariant variant = doc.createNestedObject();
  96. variant["a"] = 1;
  97. variant["b"] = "hello";
  98. variant["c"][0] = false;
  99. CHECK(object == variant);
  100. CHECK(object <= variant);
  101. CHECK(object >= variant);
  102. CHECK_FALSE(object != variant);
  103. CHECK_FALSE(object > variant);
  104. CHECK_FALSE(object < variant);
  105. CHECK(variant == object);
  106. CHECK(variant <= object);
  107. CHECK(variant >= object);
  108. CHECK_FALSE(variant != object);
  109. CHECK_FALSE(variant > object);
  110. CHECK_FALSE(variant < object);
  111. }
  112. SECTION("Compare with different object") {
  113. JsonObject object = doc.createNestedObject();
  114. object["a"] = 1;
  115. object["b"] = "hello1";
  116. object["c"][0] = false;
  117. JsonVariant variant = doc.createNestedObject();
  118. variant["a"] = 1;
  119. variant["b"] = "hello2";
  120. variant["c"][0] = false;
  121. CHECK(object != variant);
  122. CHECK_FALSE(object == variant);
  123. CHECK_FALSE(object > variant);
  124. CHECK_FALSE(object < variant);
  125. CHECK_FALSE(object <= variant);
  126. CHECK_FALSE(object >= variant);
  127. }
  128. }
  129. TEST_CASE("Compare JsonObject with JsonVariantConst") {
  130. StaticJsonDocument<512> doc;
  131. SECTION("Compare with unbound") {
  132. JsonObject object = doc.to<JsonObject>();
  133. object["a"] = 1;
  134. object["b"] = "hello";
  135. JsonVariantConst unbound;
  136. CHECK(object != unbound);
  137. CHECK_FALSE(object == unbound);
  138. CHECK_FALSE(object <= unbound);
  139. CHECK_FALSE(object >= unbound);
  140. CHECK_FALSE(object > unbound);
  141. CHECK_FALSE(object < unbound);
  142. CHECK(unbound != object);
  143. CHECK_FALSE(unbound == object);
  144. CHECK_FALSE(unbound <= object);
  145. CHECK_FALSE(unbound >= object);
  146. CHECK_FALSE(unbound > object);
  147. CHECK_FALSE(unbound < object);
  148. }
  149. SECTION("Compare with self") {
  150. JsonObject object = doc.to<JsonObject>();
  151. object["a"] = 1;
  152. object["b"] = "hello";
  153. JsonVariantConst variant = object;
  154. CHECK(object == variant);
  155. CHECK(object <= variant);
  156. CHECK(object >= variant);
  157. CHECK_FALSE(object != variant);
  158. CHECK_FALSE(object > variant);
  159. CHECK_FALSE(object < variant);
  160. CHECK(variant == object);
  161. CHECK(variant <= object);
  162. CHECK(variant >= object);
  163. CHECK_FALSE(variant != object);
  164. CHECK_FALSE(variant > object);
  165. CHECK_FALSE(variant < object);
  166. }
  167. SECTION("Compare with identical object") {
  168. JsonObject object = doc.createNestedObject();
  169. object["a"] = 1;
  170. object["b"] = "hello";
  171. object["c"][0] = false;
  172. JsonObject object2 = doc.createNestedObject();
  173. object2["a"] = 1;
  174. object2["b"] = "hello";
  175. object2["c"][0] = false;
  176. JsonVariantConst variant = object2;
  177. CHECK(object == variant);
  178. CHECK(object <= variant);
  179. CHECK(object >= variant);
  180. CHECK_FALSE(object != variant);
  181. CHECK_FALSE(object > variant);
  182. CHECK_FALSE(object < variant);
  183. CHECK(variant == object);
  184. CHECK(variant <= object);
  185. CHECK(variant >= object);
  186. CHECK_FALSE(variant != object);
  187. CHECK_FALSE(variant > object);
  188. CHECK_FALSE(variant < object);
  189. }
  190. SECTION("Compare with different object") {
  191. JsonObject object = doc.createNestedObject();
  192. object["a"] = 1;
  193. object["b"] = "hello1";
  194. object["c"][0] = false;
  195. JsonObject object2 = doc.createNestedObject();
  196. object2["a"] = 1;
  197. object2["b"] = "hello2";
  198. object2["c"][0] = false;
  199. JsonVariantConst variant = object2;
  200. CHECK(object != variant);
  201. CHECK_FALSE(object == variant);
  202. CHECK_FALSE(object > variant);
  203. CHECK_FALSE(object < variant);
  204. CHECK_FALSE(object <= variant);
  205. CHECK_FALSE(object >= variant);
  206. }
  207. }
  208. TEST_CASE("Compare JsonObject with JsonObjectConst") {
  209. StaticJsonDocument<512> doc;
  210. SECTION("Compare with unbound") {
  211. JsonObject object = doc.to<JsonObject>();
  212. object["a"] = 1;
  213. object["b"] = "hello";
  214. JsonObjectConst unbound;
  215. CHECK(object != unbound);
  216. CHECK_FALSE(object == unbound);
  217. CHECK_FALSE(object <= unbound);
  218. CHECK_FALSE(object >= unbound);
  219. CHECK_FALSE(object > unbound);
  220. CHECK_FALSE(object < unbound);
  221. CHECK(unbound != object);
  222. CHECK_FALSE(unbound == object);
  223. CHECK_FALSE(unbound <= object);
  224. CHECK_FALSE(unbound >= object);
  225. CHECK_FALSE(unbound > object);
  226. CHECK_FALSE(unbound < object);
  227. }
  228. SECTION("Compare with self") {
  229. JsonObject object = doc.to<JsonObject>();
  230. object["a"] = 1;
  231. object["b"] = "hello";
  232. JsonObjectConst cobject = object;
  233. CHECK(object == cobject);
  234. CHECK(object <= cobject);
  235. CHECK(object >= cobject);
  236. CHECK_FALSE(object != cobject);
  237. CHECK_FALSE(object > cobject);
  238. CHECK_FALSE(object < cobject);
  239. CHECK(cobject == object);
  240. CHECK(cobject <= object);
  241. CHECK(cobject >= object);
  242. CHECK_FALSE(cobject != object);
  243. CHECK_FALSE(cobject > object);
  244. CHECK_FALSE(cobject < object);
  245. }
  246. SECTION("Compare with identical object") {
  247. JsonObject object1 = doc.createNestedObject();
  248. object1["a"] = 1;
  249. object1["b"] = "hello";
  250. object1["c"][0] = false;
  251. JsonObject object2 = doc.createNestedObject();
  252. object2["a"] = 1;
  253. object2["b"] = "hello";
  254. object2["c"][0] = false;
  255. JsonObjectConst carray2 = object2;
  256. CHECK(object1 == carray2);
  257. CHECK(object1 <= carray2);
  258. CHECK(object1 >= carray2);
  259. CHECK_FALSE(object1 != carray2);
  260. CHECK_FALSE(object1 > carray2);
  261. CHECK_FALSE(object1 < carray2);
  262. CHECK(carray2 == object1);
  263. CHECK(carray2 <= object1);
  264. CHECK(carray2 >= object1);
  265. CHECK_FALSE(carray2 != object1);
  266. CHECK_FALSE(carray2 > object1);
  267. CHECK_FALSE(carray2 < object1);
  268. }
  269. SECTION("Compare with different object") {
  270. JsonObject object1 = doc.createNestedObject();
  271. object1["a"] = 1;
  272. object1["b"] = "hello1";
  273. object1["c"][0] = false;
  274. JsonObject object2 = doc.createNestedObject();
  275. object2["a"] = 1;
  276. object2["b"] = "hello2";
  277. object2["c"][0] = false;
  278. JsonObjectConst carray2 = object2;
  279. CHECK(object1 != carray2);
  280. CHECK_FALSE(object1 == carray2);
  281. CHECK_FALSE(object1 > carray2);
  282. CHECK_FALSE(object1 < carray2);
  283. CHECK_FALSE(object1 <= carray2);
  284. CHECK_FALSE(object1 >= carray2);
  285. CHECK(carray2 != object1);
  286. CHECK_FALSE(carray2 == object1);
  287. CHECK_FALSE(carray2 > object1);
  288. CHECK_FALSE(carray2 < object1);
  289. CHECK_FALSE(carray2 <= object1);
  290. CHECK_FALSE(carray2 >= object1);
  291. }
  292. }
  293. TEST_CASE("Compare JsonObjectConst with JsonObjectConst") {
  294. StaticJsonDocument<512> doc;
  295. SECTION("Compare with unbound") {
  296. JsonObject object = doc.to<JsonObject>();
  297. object["a"] = 1;
  298. object["b"] = "hello";
  299. JsonObjectConst cobject = object;
  300. JsonObjectConst unbound;
  301. CHECK(cobject != unbound);
  302. CHECK_FALSE(cobject == unbound);
  303. CHECK_FALSE(cobject <= unbound);
  304. CHECK_FALSE(cobject >= unbound);
  305. CHECK_FALSE(cobject > unbound);
  306. CHECK_FALSE(cobject < unbound);
  307. CHECK(unbound != cobject);
  308. CHECK_FALSE(unbound == cobject);
  309. CHECK_FALSE(unbound <= cobject);
  310. CHECK_FALSE(unbound >= cobject);
  311. CHECK_FALSE(unbound > cobject);
  312. CHECK_FALSE(unbound < cobject);
  313. }
  314. SECTION("Compare with self") {
  315. JsonObject object = doc.to<JsonObject>();
  316. object["a"] = 1;
  317. object["b"] = "hello";
  318. JsonObjectConst cobject = object;
  319. CHECK(cobject == cobject);
  320. CHECK(cobject <= cobject);
  321. CHECK(cobject >= cobject);
  322. CHECK_FALSE(cobject != cobject);
  323. CHECK_FALSE(cobject > cobject);
  324. CHECK_FALSE(cobject < cobject);
  325. }
  326. SECTION("Compare with identical object") {
  327. JsonObject object1 = doc.createNestedObject();
  328. object1["a"] = 1;
  329. object1["b"] = "hello";
  330. object1["c"][0] = false;
  331. JsonObjectConst carray1 = object1;
  332. JsonObject object2 = doc.createNestedObject();
  333. object2["a"] = 1;
  334. object2["b"] = "hello";
  335. object2["c"][0] = false;
  336. JsonObjectConst carray2 = object2;
  337. CHECK(carray1 == carray2);
  338. CHECK(carray1 <= carray2);
  339. CHECK(carray1 >= carray2);
  340. CHECK_FALSE(carray1 != carray2);
  341. CHECK_FALSE(carray1 > carray2);
  342. CHECK_FALSE(carray1 < carray2);
  343. }
  344. SECTION("Compare with different object") {
  345. JsonObject object1 = doc.createNestedObject();
  346. object1["a"] = 1;
  347. object1["b"] = "hello1";
  348. object1["c"][0] = false;
  349. JsonObjectConst carray1 = object1;
  350. JsonObject object2 = doc.createNestedObject();
  351. object2["a"] = 1;
  352. object2["b"] = "hello2";
  353. object2["c"][0] = false;
  354. JsonObjectConst carray2 = object2;
  355. CHECK(carray1 != carray2);
  356. CHECK_FALSE(carray1 == carray2);
  357. CHECK_FALSE(carray1 > carray2);
  358. CHECK_FALSE(carray1 < carray2);
  359. CHECK_FALSE(carray1 <= carray2);
  360. CHECK_FALSE(carray1 >= carray2);
  361. }
  362. }
  363. TEST_CASE("Compare JsonObjectConst with JsonVariant") {
  364. StaticJsonDocument<512> doc;
  365. SECTION("Compare with self") {
  366. JsonObject object = doc.to<JsonObject>();
  367. object["a"] = 1;
  368. object["b"] = "hello";
  369. JsonObjectConst cobject = object;
  370. JsonVariant variant = object;
  371. CHECK(cobject == variant);
  372. CHECK(cobject <= variant);
  373. CHECK(cobject >= variant);
  374. CHECK_FALSE(cobject != variant);
  375. CHECK_FALSE(cobject > variant);
  376. CHECK_FALSE(cobject < variant);
  377. CHECK(variant == cobject);
  378. CHECK(variant <= cobject);
  379. CHECK(variant >= cobject);
  380. CHECK_FALSE(variant != cobject);
  381. CHECK_FALSE(variant > cobject);
  382. CHECK_FALSE(variant < cobject);
  383. }
  384. SECTION("Compare with identical object") {
  385. JsonObject object1 = doc.createNestedObject();
  386. object1["a"] = 1;
  387. object1["b"] = "hello";
  388. object1["c"][0] = false;
  389. JsonObjectConst carray1 = object1;
  390. JsonObject object2 = doc.createNestedObject();
  391. object2["a"] = 1;
  392. object2["b"] = "hello";
  393. object2["c"][0] = false;
  394. JsonVariant variant2 = object2;
  395. CHECK(carray1 == variant2);
  396. CHECK(carray1 <= variant2);
  397. CHECK(carray1 >= variant2);
  398. CHECK_FALSE(carray1 != variant2);
  399. CHECK_FALSE(carray1 > variant2);
  400. CHECK_FALSE(carray1 < variant2);
  401. CHECK(variant2 == carray1);
  402. CHECK(variant2 <= carray1);
  403. CHECK(variant2 >= carray1);
  404. CHECK_FALSE(variant2 != carray1);
  405. CHECK_FALSE(variant2 > carray1);
  406. CHECK_FALSE(variant2 < carray1);
  407. }
  408. SECTION("Compare with different object") {
  409. JsonObject object1 = doc.createNestedObject();
  410. object1["a"] = 1;
  411. object1["b"] = "hello1";
  412. object1["c"][0] = false;
  413. JsonObjectConst carray1 = object1;
  414. JsonObject object2 = doc.createNestedObject();
  415. object2["a"] = 1;
  416. object2["b"] = "hello2";
  417. object2["c"][0] = false;
  418. JsonVariant variant2 = object2;
  419. CHECK(carray1 != variant2);
  420. CHECK_FALSE(carray1 == variant2);
  421. CHECK_FALSE(carray1 > variant2);
  422. CHECK_FALSE(carray1 < variant2);
  423. CHECK_FALSE(carray1 <= variant2);
  424. CHECK_FALSE(carray1 >= variant2);
  425. CHECK(variant2 != carray1);
  426. CHECK_FALSE(variant2 == carray1);
  427. CHECK_FALSE(variant2 > carray1);
  428. CHECK_FALSE(variant2 < carray1);
  429. CHECK_FALSE(variant2 <= carray1);
  430. CHECK_FALSE(variant2 >= carray1);
  431. }
  432. }