compare.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. static const char* null = 0;
  7. template <typename T>
  8. void checkEquals(T a, T b) {
  9. DynamicJsonDocument doc(4096);
  10. JsonVariant variant = doc.to<JsonVariant>();
  11. variant.set(a);
  12. REQUIRE(b == variant);
  13. REQUIRE(variant == b);
  14. REQUIRE(b <= variant);
  15. REQUIRE(variant <= b);
  16. REQUIRE(b >= variant);
  17. REQUIRE(variant >= b);
  18. REQUIRE_FALSE(b != variant);
  19. REQUIRE_FALSE(variant != b);
  20. REQUIRE_FALSE(b > variant);
  21. REQUIRE_FALSE(variant > b);
  22. REQUIRE_FALSE(b < variant);
  23. REQUIRE_FALSE(variant < b);
  24. }
  25. template <typename T>
  26. void checkGreater(T a, T b) {
  27. DynamicJsonDocument doc(4096);
  28. JsonVariant variant = doc.to<JsonVariant>();
  29. variant.set(a);
  30. REQUIRE(variant > b);
  31. REQUIRE(b < variant);
  32. REQUIRE(variant != b);
  33. REQUIRE(b != variant);
  34. REQUIRE_FALSE(variant < b);
  35. REQUIRE_FALSE(b > variant);
  36. REQUIRE_FALSE(variant == b);
  37. REQUIRE_FALSE(b == variant);
  38. }
  39. template <typename T>
  40. void checkLower(T a, T b) {
  41. DynamicJsonDocument doc(4096);
  42. JsonVariant variant = doc.to<JsonVariant>();
  43. variant.set(a);
  44. REQUIRE(variant < b);
  45. REQUIRE(b > variant);
  46. REQUIRE(variant != b);
  47. REQUIRE(b != variant);
  48. REQUIRE_FALSE(variant > b);
  49. REQUIRE_FALSE(b < variant);
  50. REQUIRE_FALSE(variant == b);
  51. REQUIRE_FALSE(b == variant);
  52. }
  53. template <typename T>
  54. void checkComparisons(T low, T mid, T high) {
  55. checkEquals(mid, mid);
  56. checkGreater(mid, low);
  57. checkLower(mid, high);
  58. }
  59. TEST_CASE("JsonVariant comparisons") {
  60. SECTION("Double") {
  61. checkComparisons<double>(123.44, 123.45, 123.46);
  62. }
  63. SECTION("Float") {
  64. checkComparisons<float>(123.44f, 123.45f, 123.46f);
  65. }
  66. SECTION("SChar") {
  67. checkComparisons<signed char>(122, 123, 124);
  68. }
  69. SECTION("SInt") {
  70. checkComparisons<signed int>(122, 123, 124);
  71. }
  72. SECTION("SLong") {
  73. checkComparisons<signed long>(122L, 123L, 124L);
  74. }
  75. SECTION("SShort") {
  76. checkComparisons<signed short>(122, 123, 124);
  77. }
  78. SECTION("UChar") {
  79. checkComparisons<unsigned char>(122, 123, 124);
  80. }
  81. SECTION("UInt") {
  82. checkComparisons<unsigned int>(122, 123, 124);
  83. }
  84. SECTION("ULong") {
  85. checkComparisons<unsigned long>(122L, 123L, 124L);
  86. }
  87. SECTION("UShort") {
  88. checkComparisons<unsigned short>(122, 123, 124);
  89. }
  90. SECTION("null") {
  91. DynamicJsonDocument doc(4096);
  92. JsonVariant variant = doc.to<JsonVariant>();
  93. variant.set(null);
  94. REQUIRE(variant == variant);
  95. REQUIRE_FALSE(variant != variant);
  96. REQUIRE(variant == null);
  97. REQUIRE_FALSE(variant != null);
  98. REQUIRE(variant != "null");
  99. REQUIRE_FALSE(variant == "null");
  100. }
  101. SECTION("StringLiteral") {
  102. DynamicJsonDocument doc(4096);
  103. deserializeJson(doc, "\"hello\"");
  104. JsonVariant variant = doc.as<JsonVariant>();
  105. REQUIRE(variant == variant);
  106. REQUIRE_FALSE(variant != variant);
  107. REQUIRE(variant == "hello");
  108. REQUIRE_FALSE(variant != "hello");
  109. REQUIRE(variant != "world");
  110. REQUIRE_FALSE(variant == "world");
  111. REQUIRE(variant != null);
  112. REQUIRE_FALSE(variant == null);
  113. REQUIRE("hello" == variant);
  114. REQUIRE_FALSE("hello" != variant);
  115. REQUIRE("world" != variant);
  116. REQUIRE_FALSE("world" == variant);
  117. REQUIRE(null != variant);
  118. REQUIRE_FALSE(null == variant);
  119. }
  120. SECTION("String") {
  121. DynamicJsonDocument doc(4096);
  122. JsonVariant variant = doc.to<JsonVariant>();
  123. variant.set("hello");
  124. REQUIRE(variant == variant);
  125. REQUIRE_FALSE(variant != variant);
  126. REQUIRE(variant == std::string("hello"));
  127. REQUIRE_FALSE(variant != std::string("hello"));
  128. REQUIRE(variant != std::string("world"));
  129. REQUIRE_FALSE(variant == std::string("world"));
  130. REQUIRE(variant != null);
  131. REQUIRE_FALSE(variant == null);
  132. REQUIRE(std::string("hello") == variant);
  133. REQUIRE_FALSE(std::string("hello") != variant);
  134. REQUIRE(std::string("world") != variant);
  135. REQUIRE_FALSE(std::string("world") == variant);
  136. REQUIRE(null != variant);
  137. REQUIRE_FALSE(null == variant);
  138. }
  139. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  140. SECTION("VLA equals") {
  141. int i = 16;
  142. char vla[i];
  143. strcpy(vla, "hello");
  144. DynamicJsonDocument doc(4096);
  145. JsonVariant variant = doc.to<JsonVariant>();
  146. variant.set("hello");
  147. REQUIRE((vla == variant));
  148. REQUIRE((variant == vla));
  149. REQUIRE_FALSE((vla != variant));
  150. REQUIRE_FALSE((variant != vla));
  151. }
  152. SECTION("VLA differs") {
  153. int i = 16;
  154. char vla[i];
  155. strcpy(vla, "hello");
  156. DynamicJsonDocument doc(4096);
  157. JsonVariant variant = doc.to<JsonVariant>();
  158. variant.set("world");
  159. REQUIRE((vla != variant));
  160. REQUIRE((variant != vla));
  161. REQUIRE_FALSE((vla == variant));
  162. REQUIRE_FALSE((variant == vla));
  163. }
  164. #endif
  165. DynamicJsonDocument doc1(4096), doc2(4096), doc3(4096);
  166. JsonVariant variant1 = doc1.to<JsonVariant>();
  167. JsonVariant variant2 = doc2.to<JsonVariant>();
  168. JsonVariant variant3 = doc3.to<JsonVariant>();
  169. SECTION("Variants containing integers") {
  170. variant1.set(42);
  171. variant2.set(42);
  172. variant3.set(666);
  173. REQUIRE(variant1 == variant2);
  174. REQUIRE_FALSE(variant1 != variant2);
  175. REQUIRE(variant1 != variant3);
  176. REQUIRE_FALSE(variant1 == variant3);
  177. }
  178. SECTION("Variants containing linked strings") {
  179. variant1.set("0hello" + 1); // make sure they have
  180. variant2.set("1hello" + 1); // different addresses
  181. variant3.set("world");
  182. REQUIRE(variant1 == variant2);
  183. REQUIRE_FALSE(variant1 != variant2);
  184. REQUIRE(variant1 != variant3);
  185. REQUIRE_FALSE(variant1 == variant3);
  186. }
  187. SECTION("Variants containing owned strings") {
  188. variant1.set(std::string("hello"));
  189. variant2.set(std::string("hello"));
  190. variant3.set(std::string("world"));
  191. REQUIRE(variant1 == variant2);
  192. REQUIRE_FALSE(variant1 != variant2);
  193. REQUIRE(variant1 != variant3);
  194. REQUIRE_FALSE(variant1 == variant3);
  195. }
  196. SECTION("Variants containing linked raws") {
  197. variant1.set(serialized("0hello" + 1)); // make sure they have
  198. variant2.set(serialized("1hello" + 1)); // different addresses
  199. variant3.set(serialized("world"));
  200. REQUIRE(variant1 == variant2);
  201. REQUIRE_FALSE(variant1 != variant2);
  202. REQUIRE(variant1 != variant3);
  203. REQUIRE_FALSE(variant1 == variant3);
  204. }
  205. SECTION("Variants containing owned raws") {
  206. variant1.set(serialized(std::string("hello")));
  207. variant2.set(serialized(std::string("hello")));
  208. variant3.set(serialized(std::string("world")));
  209. REQUIRE(variant1 == variant2);
  210. REQUIRE_FALSE(variant1 != variant2);
  211. REQUIRE(variant1 != variant3);
  212. REQUIRE_FALSE(variant1 == variant3);
  213. }
  214. SECTION("Variants containing double") {
  215. variant1.set(42.0);
  216. variant2.set(42.0);
  217. variant3.set(666.0);
  218. REQUIRE(variant1 == variant2);
  219. REQUIRE_FALSE(variant1 != variant2);
  220. REQUIRE(variant1 != variant3);
  221. REQUIRE_FALSE(variant1 == variant3);
  222. }
  223. SECTION("BoolInVariant") {
  224. variant1.set(true);
  225. variant2.set(true);
  226. variant3.set(false);
  227. REQUIRE(variant1 == variant2);
  228. REQUIRE_FALSE(variant1 != variant2);
  229. REQUIRE(variant1 != variant3);
  230. REQUIRE_FALSE(variant1 == variant3);
  231. }
  232. SECTION("ArrayInVariant") {
  233. JsonArray array1 = variant1.to<JsonArray>();
  234. JsonArray array2 = variant2.to<JsonArray>();
  235. array1.add(42);
  236. array2.add(42);
  237. REQUIRE(variant1 == variant2);
  238. REQUIRE_FALSE(variant1 != variant2);
  239. REQUIRE(variant1 != variant3);
  240. REQUIRE_FALSE(variant1 == variant3);
  241. }
  242. SECTION("ObjectInVariant") {
  243. JsonObject obj1 = variant1.to<JsonObject>();
  244. JsonObject obj2 = variant2.to<JsonObject>();
  245. obj1["hello"] = "world";
  246. obj2["hello"] = "world";
  247. REQUIRE(variant1 == variant2);
  248. REQUIRE_FALSE(variant1 != variant2);
  249. REQUIRE(variant1 != variant3);
  250. REQUIRE_FALSE(variant1 == variant3);
  251. }
  252. // SECTION("VariantsOfDifferentTypes") {
  253. // DynamicJsonDocument doc1(4096);
  254. // JsonObject obj = doc1.to<JsonObject>();
  255. // DynamicJsonDocument doc2(4096);
  256. // JsonArray arr = doc2.to<JsonArray>();
  257. // JsonVariant variants[] = {
  258. // true, 42, 666.667, "hello", arr, obj,
  259. // };
  260. // size_t n = sizeof(variants) / sizeof(variants[0]);
  261. // for (size_t i = 0; i < n; i++) {
  262. // for (size_t j = i + 1; j < n; j++) {
  263. // REQUIRE(variants[i] != variants[j]);
  264. // REQUIRE_FALSE(variants[i] == variants[j]);
  265. // }
  266. // }
  267. // }
  268. }