You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_compare.c 8.6 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Tests if json_object_equal behaves correct.
  3. */
  4. #ifdef NDEBUG
  5. #undef NDEBUG
  6. #endif
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "json_inttypes.h"
  11. #include "json_object.h"
  12. int main(int argc, char **argv)
  13. {
  14. /* integer tests */
  15. struct json_object *int1 = json_object_new_int(0);
  16. struct json_object *int2 = json_object_new_int(1);
  17. struct json_object *int3 = json_object_new_int(1);
  18. struct json_object *int4 = json_object_new_int(-1);
  19. struct json_object *uint1 = json_object_new_uint64(0);
  20. struct json_object *uint2 = json_object_new_uint64(1);
  21. struct json_object *uint3 = json_object_new_uint64(1);
  22. struct json_object *uint4 = json_object_new_uint64((uint64_t)INT64_MAX + 100);
  23. if (!json_object_equal(int1, int2))
  24. printf("JSON integer comparison is correct\n");
  25. else
  26. printf("JSON integer comparison failed\n");
  27. if (json_object_equal(int1, int1))
  28. printf("JSON same object comparison is correct\n");
  29. else
  30. printf("JSON same object comparison failed\n");
  31. if (json_object_equal(int2, int3))
  32. printf("JSON same integer comparison is correct\n");
  33. else
  34. printf("JSON same integer comparison failed\n");
  35. if (!json_object_equal(uint1, uint2))
  36. printf("JSON usigned integer comparison is correct\n");
  37. else
  38. printf("JSON usigned integer comparison failed\n");
  39. if (json_object_equal(uint1, uint1))
  40. printf("JSON same usigned object comparison is correct\n");
  41. else
  42. printf("JSON same usigned object comparison failed\n");
  43. if (json_object_equal(uint2, uint3))
  44. printf("JSON same usigned integer comparison is correct\n");
  45. else
  46. printf("JSON same usigned integer comparison failed\n");
  47. if (json_object_equal(int2, uint2))
  48. printf("JSON integer & usigned integer comparison is correct\n");
  49. else
  50. printf("JSON integer & usigned integer comparison failed\n");
  51. if (!json_object_equal(int2, uint4))
  52. printf("JSON integer & usigned integer comparison is correct\n");
  53. else
  54. printf("JSON integer & usigned integer comparison failed\n");
  55. if (!json_object_equal(int4, uint2))
  56. printf("JSON integer & usigned integer comparison is correct\n");
  57. else
  58. printf("JSON integer & usigned integer comparison failed\n");
  59. if (!json_object_equal(int4, uint4))
  60. printf("JSON integer & usigned integer comparison is correct\n");
  61. else
  62. printf("JSON integer & usigned integer comparison failed\n");
  63. if (json_object_equal(uint2, int2))
  64. printf("JSON usigned integer & integer comparison is correct\n");
  65. else
  66. printf("JSON usigned integer & integer comparison failed\n");
  67. if (!json_object_equal(uint2, int4))
  68. printf("JSON usigned integer & integer comparison is correct\n");
  69. else
  70. printf("JSON usigned integer & integer comparison failed\n");
  71. if (!json_object_equal(uint4, int2))
  72. printf("JSON usigned integer & integer comparison is correct\n");
  73. else
  74. printf("JSON usigned integer & integer comparison failed\n");
  75. if (!json_object_equal(uint4, int4))
  76. printf("JSON usigned integer & integer comparison is correct\n");
  77. else
  78. printf("JSON usigned integer & integer comparison failed\n");
  79. json_object_put(int1);
  80. json_object_put(int2);
  81. json_object_put(int3);
  82. json_object_put(int4);
  83. json_object_put(uint1);
  84. json_object_put(uint2);
  85. json_object_put(uint3);
  86. json_object_put(uint4);
  87. /* string tests */
  88. struct json_object *str1 = json_object_new_string("TESTSTRING");
  89. struct json_object *str2 = json_object_new_string("TESTSTRING");
  90. struct json_object *str3 = json_object_new_string("DIFFERENT");
  91. if (json_object_equal(str1, str2))
  92. printf("Comparing equal strings is correct\n");
  93. else
  94. printf("Comparing equal strings failed\n");
  95. if (!json_object_equal(str1, str3))
  96. printf("Comparing different strings is correct\n");
  97. else
  98. printf("Comparing different strings failed\n");
  99. json_object_put(str1);
  100. json_object_put(str2);
  101. json_object_put(str3);
  102. /* double tests */
  103. struct json_object *dbl1 = json_object_new_double(3.14159);
  104. struct json_object *dbl2 = json_object_new_double(3.14159);
  105. struct json_object *dbl3 = json_object_new_double(3.0);
  106. if (json_object_equal(dbl1, dbl2))
  107. printf("Comparing equal doubles is correct\n");
  108. else
  109. printf("Comparing equal doubles failed\n");
  110. if (!json_object_equal(dbl1, dbl3))
  111. printf("Comparing different doubles is correct\n");
  112. else
  113. printf("Comparing different doubles failed\n");
  114. json_object_put(dbl1);
  115. json_object_put(dbl2);
  116. json_object_put(dbl3);
  117. /* array tests */
  118. struct json_object *ar1 = json_object_new_array();
  119. struct json_object *ar2 = json_object_new_array();
  120. struct json_object *ar3 = json_object_new_array();
  121. struct json_object *ar4 = json_object_new_array();
  122. json_object_array_add(ar1, json_object_new_int(1));
  123. json_object_array_add(ar1, json_object_new_int(2));
  124. json_object_array_add(ar2, json_object_new_int(1));
  125. json_object_array_add(ar2, json_object_new_int(2));
  126. json_object_array_add(ar3, json_object_new_int(1));
  127. json_object_array_add(ar3, json_object_new_int(1));
  128. if (json_object_equal(ar1, ar2))
  129. printf("Comparing equal arrays is correct\n");
  130. else
  131. printf("Comparing equal arrays failed\n");
  132. json_object_array_add(ar2, json_object_new_int(1));
  133. if (!json_object_equal(ar1, ar2))
  134. printf("Comparing arrays of different len is correct\n");
  135. else
  136. printf("Comparing arrays of different len failed\n");
  137. if (!json_object_equal(ar1, ar3))
  138. printf("Comparing different arrays is correct\n");
  139. else
  140. printf("Comparing different arrays failed\n");
  141. if (!json_object_equal(ar1, ar4))
  142. printf("Comparing different arrays (one empty) is correct\n");
  143. else
  144. printf("Comparing different arrays (one empty) failed\n");
  145. json_object_put(ar1);
  146. json_object_put(ar2);
  147. json_object_put(ar3);
  148. json_object_put(ar4);
  149. /* object tests */
  150. struct json_object *obj1 = json_object_new_object();
  151. struct json_object *obj2 = json_object_new_object();
  152. json_object_object_add(obj1, "test1", json_object_new_int(123));
  153. json_object_object_add(obj1, "test2", json_object_new_int(321));
  154. json_object_object_add(obj1, "test3", json_object_new_int(320));
  155. json_object_object_add(obj1, "test4", json_object_new_int(319));
  156. json_object_object_add(obj1, "test5", json_object_new_int(318));
  157. json_object_object_add(obj2, "test5", json_object_new_int(318));
  158. json_object_object_add(obj2, "test4", json_object_new_int(319));
  159. json_object_object_add(obj2, "test3", json_object_new_int(320));
  160. json_object_object_add(obj2, "test2", json_object_new_int(321));
  161. json_object_object_add(obj2, "test1", json_object_new_int(123));
  162. /* key-order is different between obj1 and obj2, should still be equal */
  163. if (json_object_equal(obj1, obj2))
  164. printf("Comparing JSON object with different key order is correct\n");
  165. else
  166. printf("Comparing JSON object with different key order is incorrect\n");
  167. /* make obj2 look different to obj1 */
  168. json_object_object_add(obj2, "test3", json_object_new_int(234));
  169. if (!json_object_equal(obj1, obj2))
  170. printf("Comparing different objects is correct\n");
  171. else
  172. printf("Comparing different objects is incorrect\n");
  173. /* iterate over jso2 keys to see if any exist that are not in jso1 */
  174. json_object_object_add(obj2, "test3", json_object_new_int(320));
  175. json_object_object_add(obj2, "test6", json_object_new_int(321));
  176. if (!json_object_equal(obj1, obj2))
  177. printf("Comparing different objects is correct\n");
  178. else
  179. printf("Comparing different objects is incorrect\n");
  180. /* iterate over jso1 keys and see if they exist in jso1 */
  181. json_object_object_add(obj1, "test6", json_object_new_int(321));
  182. if (json_object_equal(obj1, obj2))
  183. printf("Comparing different objects is correct\n");
  184. else
  185. printf("Comparing different objects is incorrect\n");
  186. json_object_object_add(obj1, "test7", json_object_new_int(322));
  187. if (!json_object_equal(obj1, obj2))
  188. printf("Comparing different objects is correct\n");
  189. else
  190. printf("Comparing different objects is incorrect\n");
  191. json_object_put(obj1);
  192. json_object_put(obj2);
  193. /* different types tests */
  194. struct json_object *int5 = json_object_new_int(0);
  195. struct json_object *dbl5 = json_object_new_double(3.14159);
  196. if (!json_object_equal(int5, NULL))
  197. printf("JSON integer and NULL comparison is correct\n");
  198. else
  199. printf("JSON integer and NULL comparison failed\n");
  200. if (!json_object_equal(NULL, dbl5))
  201. printf("JSON NULL and double comparison is correct\n");
  202. else
  203. printf("JSON NULL and double comparison failed\n");
  204. if (!json_object_equal(int5, dbl5))
  205. printf("JSON integer and double comparison is correct\n");
  206. else
  207. printf("JSON integer and double comparison failed\n");
  208. json_object_put(int5);
  209. json_object_put(dbl5);
  210. return 0;
  211. }