test_equal.c 5.2 KB

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