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 5.4 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Tests if json_object_equal behaves correct.
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.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 *uint1 = json_object_new_uint64(0);
  16. struct json_object *uint2 = json_object_new_uint64(1);
  17. struct json_object *uint3 = json_object_new_uint64(1);
  18. if (!json_object_equal(int1, int2))
  19. printf("JSON integer comparison is correct\n");
  20. else
  21. printf("JSON integer comparison failed\n");
  22. if (json_object_equal(int1, int1))
  23. printf("JSON same object comparison is correct\n");
  24. else
  25. printf("JSON same object comparison failed\n");
  26. if (json_object_equal(int2, int3))
  27. printf("JSON same integer comparison is correct\n");
  28. else
  29. printf("JSON same integer comparison failed\n");
  30. if (!json_object_equal(uint1, uint2))
  31. printf("JSON usigned integer comparison is correct\n");
  32. else
  33. printf("JSON usigned integer comparison failed\n");
  34. if (json_object_equal(uint1, uint1))
  35. printf("JSON same usigned object comparison is correct\n");
  36. else
  37. printf("JSON same usigned object comparison failed\n");
  38. if (json_object_equal(uint2, uint3))
  39. printf("JSON same usigned integer comparison is correct\n");
  40. else
  41. printf("JSON same usigned integer comparison failed\n");
  42. json_object_put(int1);
  43. json_object_put(int2);
  44. json_object_put(int3);
  45. json_object_put(uint1);
  46. json_object_put(uint2);
  47. json_object_put(uint3);
  48. /* string tests */
  49. struct json_object *str1 = json_object_new_string("TESTSTRING");
  50. struct json_object *str2 = json_object_new_string("TESTSTRING");
  51. struct json_object *str3 = json_object_new_string("DIFFERENT");
  52. if (json_object_equal(str1, str2))
  53. printf("Comparing equal strings is correct\n");
  54. else
  55. printf("Comparing equal strings failed\n");
  56. if (!json_object_equal(str1, str3))
  57. printf("Comparing different strings is correct\n");
  58. else
  59. printf("Comparing different strings failed\n");
  60. json_object_put(str1);
  61. json_object_put(str2);
  62. json_object_put(str3);
  63. /* double tests */
  64. struct json_object *dbl1 = json_object_new_double(3.14159);
  65. struct json_object *dbl2 = json_object_new_double(3.14159);
  66. struct json_object *dbl3 = json_object_new_double(3.0);
  67. if (json_object_equal(dbl1, dbl2))
  68. printf("Comparing equal doubles is correct\n");
  69. else
  70. printf("Comparing equal doubles failed\n");
  71. if (!json_object_equal(dbl1, dbl3))
  72. printf("Comparing different doubles is correct\n");
  73. else
  74. printf("Comparing different doubles failed\n");
  75. json_object_put(dbl1);
  76. json_object_put(dbl2);
  77. json_object_put(dbl3);
  78. /* array tests */
  79. struct json_object *ar1 = json_object_new_array();
  80. struct json_object *ar2 = json_object_new_array();
  81. struct json_object *ar3 = json_object_new_array();
  82. struct json_object *ar4 = json_object_new_array();
  83. json_object_array_add(ar1, json_object_new_int(1));
  84. json_object_array_add(ar1, json_object_new_int(2));
  85. json_object_array_add(ar2, json_object_new_int(1));
  86. json_object_array_add(ar2, json_object_new_int(2));
  87. json_object_array_add(ar3, json_object_new_int(1));
  88. json_object_array_add(ar3, json_object_new_int(1));
  89. if (json_object_equal(ar1, ar2))
  90. printf("Comparing equal arrays is correct\n");
  91. else
  92. printf("Comparing equal arrays failed\n");
  93. json_object_array_add(ar2, json_object_new_int(1));
  94. if (!json_object_equal(ar1, ar2))
  95. printf("Comparing arrays of different len is correct\n");
  96. else
  97. printf("Comparing arrays of different len failed\n");
  98. if (!json_object_equal(ar1, ar3))
  99. printf("Comparing different arrays is correct\n");
  100. else
  101. printf("Comparing different arrays failed\n");
  102. if (!json_object_equal(ar1, ar4))
  103. printf("Comparing different arrays (one empty) is correct\n");
  104. else
  105. printf("Comparing different arrays (one empty) failed\n");
  106. json_object_put(ar1);
  107. json_object_put(ar2);
  108. json_object_put(ar3);
  109. json_object_put(ar4);
  110. /* object tests */
  111. struct json_object *obj1 = json_object_new_object();
  112. struct json_object *obj2 = json_object_new_object();
  113. json_object_object_add(obj1, "test1", json_object_new_int(123));
  114. json_object_object_add(obj1, "test2", json_object_new_int(321));
  115. json_object_object_add(obj1, "test3", json_object_new_int(320));
  116. json_object_object_add(obj1, "test4", json_object_new_int(319));
  117. json_object_object_add(obj1, "test5", json_object_new_int(318));
  118. json_object_object_add(obj2, "test5", json_object_new_int(318));
  119. json_object_object_add(obj2, "test4", json_object_new_int(319));
  120. json_object_object_add(obj2, "test3", json_object_new_int(320));
  121. json_object_object_add(obj2, "test2", json_object_new_int(321));
  122. json_object_object_add(obj2, "test1", json_object_new_int(123));
  123. /* key-order is different between obj1 and obj2, should still be equal */
  124. if (json_object_equal(obj1, obj2))
  125. printf("Comparing JSON object with different key order is correct\n");
  126. else
  127. printf("Comparing JSON object with different key order is incorrect\n");
  128. /* make obj2 look different to obj1 */
  129. json_object_object_add(obj2, "test3", json_object_new_int(234));
  130. if (!json_object_equal(obj1, obj2))
  131. printf("Comparing different objects is correct\n");
  132. else
  133. printf("Comparing different objects is incorrect\n");
  134. json_object_put(obj1);
  135. json_object_put(obj2);
  136. return 0;
  137. }