test_compare.c 4.6 KB

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