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

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