test_equal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <[email protected]>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include "util.h"
  8. #include <jansson.h>
  9. static void test_equal_simple() {
  10. json_t *value1, *value2;
  11. if (json_equal(NULL, NULL))
  12. fail("json_equal fails for two NULLs");
  13. value1 = json_true();
  14. if (json_equal(value1, NULL) || json_equal(NULL, value1))
  15. fail("json_equal fails for NULL");
  16. /* this covers true, false and null as they are singletons */
  17. if (!json_equal(value1, value1))
  18. fail("identical objects are not equal");
  19. json_decref(value1);
  20. /* integer */
  21. value1 = json_integer(1);
  22. value2 = json_integer(1);
  23. if (!value1 || !value2)
  24. fail("unable to create integers");
  25. if (!json_equal(value1, value2))
  26. fail("json_equal fails for two equal integers");
  27. json_decref(value2);
  28. value2 = json_integer(2);
  29. if (!value2)
  30. fail("unable to create an integer");
  31. if (json_equal(value1, value2))
  32. fail("json_equal fails for two inequal integers");
  33. json_decref(value1);
  34. json_decref(value2);
  35. /* real */
  36. value1 = json_real(1.2);
  37. value2 = json_real(1.2);
  38. if (!value1 || !value2)
  39. fail("unable to create reals");
  40. if (!json_equal(value1, value2))
  41. fail("json_equal fails for two equal reals");
  42. json_decref(value2);
  43. value2 = json_real(3.141592);
  44. if (!value2)
  45. fail("unable to create an real");
  46. if (json_equal(value1, value2))
  47. fail("json_equal fails for two inequal reals");
  48. json_decref(value1);
  49. json_decref(value2);
  50. /* string */
  51. value1 = json_string("foo");
  52. value2 = json_string("foo");
  53. if (!value1 || !value2)
  54. fail("unable to create strings");
  55. if (!json_equal(value1, value2))
  56. fail("json_equal fails for two equal strings");
  57. json_decref(value2);
  58. value2 = json_string("bar");
  59. if (!value2)
  60. fail("unable to create an string");
  61. if (json_equal(value1, value2))
  62. fail("json_equal fails for two inequal strings");
  63. json_decref(value2);
  64. value2 = json_string("bar2");
  65. if (!value2)
  66. fail("unable to create an string");
  67. if (json_equal(value1, value2))
  68. fail("json_equal fails for two inequal length strings");
  69. json_decref(value1);
  70. json_decref(value2);
  71. }
  72. static void test_equal_array() {
  73. json_t *array1, *array2;
  74. array1 = json_array();
  75. array2 = json_array();
  76. if (!array1 || !array2)
  77. fail("unable to create arrays");
  78. if (!json_equal(array1, array2))
  79. fail("json_equal fails for two empty arrays");
  80. json_array_append_new(array1, json_integer(1));
  81. json_array_append_new(array2, json_integer(1));
  82. json_array_append_new(array1, json_string("foo"));
  83. json_array_append_new(array2, json_string("foo"));
  84. json_array_append_new(array1, json_integer(2));
  85. json_array_append_new(array2, json_integer(2));
  86. if (!json_equal(array1, array2))
  87. fail("json_equal fails for two equal arrays");
  88. json_array_remove(array2, 2);
  89. if (json_equal(array1, array2))
  90. fail("json_equal fails for two inequal arrays");
  91. json_array_append_new(array2, json_integer(3));
  92. if (json_equal(array1, array2))
  93. fail("json_equal fails for two inequal arrays");
  94. json_decref(array1);
  95. json_decref(array2);
  96. }
  97. static void test_equal_object() {
  98. json_t *object1, *object2;
  99. object1 = json_object();
  100. object2 = json_object();
  101. if (!object1 || !object2)
  102. fail("unable to create objects");
  103. if (!json_equal(object1, object2))
  104. fail("json_equal fails for two empty objects");
  105. json_object_set_new(object1, "a", json_integer(1));
  106. json_object_set_new(object2, "a", json_integer(1));
  107. json_object_set_new(object1, "b", json_string("foo"));
  108. json_object_set_new(object2, "b", json_string("foo"));
  109. json_object_set_new(object1, "c", json_integer(2));
  110. json_object_set_new(object2, "c", json_integer(2));
  111. if (!json_equal(object1, object2))
  112. fail("json_equal fails for two equal objects");
  113. json_object_del(object2, "c");
  114. if (json_equal(object1, object2))
  115. fail("json_equal fails for two inequal objects");
  116. json_object_set_new(object2, "c", json_integer(3));
  117. if (json_equal(object1, object2))
  118. fail("json_equal fails for two inequal objects");
  119. json_object_del(object2, "c");
  120. json_object_set_new(object2, "d", json_integer(2));
  121. if (json_equal(object1, object2))
  122. fail("json_equal fails for two inequal objects");
  123. json_decref(object1);
  124. json_decref(object2);
  125. }
  126. static void test_equal_complex() {
  127. json_t *value1, *value2, *value3;
  128. const char *complex_json = "{"
  129. " \"integer\": 1, "
  130. " \"real\": 3.141592, "
  131. " \"string\": \"foobar\", "
  132. " \"true\": true, "
  133. " \"object\": {"
  134. " \"array-in-object\": [1,true,\"foo\",{}],"
  135. " \"object-in-object\": {\"foo\": \"bar\"}"
  136. " },"
  137. " \"array\": [\"foo\", false, null, 1.234]"
  138. "}";
  139. value1 = json_loads(complex_json, 0, NULL);
  140. value2 = json_loads(complex_json, 0, NULL);
  141. value3 = json_loads(complex_json, 0, NULL);
  142. if (!value1 || !value2)
  143. fail("unable to parse JSON");
  144. if (!json_equal(value1, value2))
  145. fail("json_equal fails for two equal objects");
  146. json_array_set_new(
  147. json_object_get(json_object_get(value2, "object"), "array-in-object"), 1,
  148. json_false());
  149. if (json_equal(value1, value2))
  150. fail("json_equal fails for two inequal objects");
  151. json_object_set_new(
  152. json_object_get(json_object_get(value3, "object"), "object-in-object"), "foo",
  153. json_string("baz"));
  154. if (json_equal(value1, value3))
  155. fail("json_equal fails for two inequal objects");
  156. json_decref(value1);
  157. json_decref(value2);
  158. json_decref(value3);
  159. }
  160. static void run_tests() {
  161. test_equal_simple();
  162. test_equal_array();
  163. test_equal_object();
  164. test_equal_complex();
  165. }