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