compare_tests.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "unity/examples/unity_config.h"
  20. #include "unity/src/unity.h"
  21. #include "common.h"
  22. static cJSON_bool compare_from_string(const char * const a, const char * const b, const cJSON_bool case_sensitive)
  23. {
  24. cJSON *a_json = NULL;
  25. cJSON *b_json = NULL;
  26. cJSON_bool result = false;
  27. a_json = cJSON_Parse(a);
  28. TEST_ASSERT_NOT_NULL_MESSAGE(a_json, "Failed to parse a.");
  29. b_json = cJSON_Parse(b);
  30. TEST_ASSERT_NOT_NULL_MESSAGE(b_json, "Failed to parse b.");
  31. result = cJSON_Compare(a_json, b_json, case_sensitive);
  32. cJSON_Delete(a_json);
  33. cJSON_Delete(b_json);
  34. return result;
  35. }
  36. static void cjson_compare_should_compare_null_pointer_as_not_equal(void)
  37. {
  38. TEST_ASSERT_FALSE(cJSON_Compare(NULL, NULL, true));
  39. TEST_ASSERT_FALSE(cJSON_Compare(NULL, NULL, false));
  40. }
  41. static void cjson_compare_should_compare_invalid_as_not_equal(void)
  42. {
  43. cJSON invalid[1];
  44. memset(invalid, '\0', sizeof(invalid));
  45. TEST_ASSERT_FALSE(cJSON_Compare(invalid, invalid, false));
  46. TEST_ASSERT_FALSE(cJSON_Compare(invalid, invalid, true));
  47. }
  48. static void cjson_compare_should_compare_numbers(void)
  49. {
  50. TEST_ASSERT_TRUE(compare_from_string("1", "1", true));
  51. TEST_ASSERT_TRUE(compare_from_string("1", "1", false));
  52. TEST_ASSERT_TRUE(compare_from_string("0.0001", "0.0001", true));
  53. TEST_ASSERT_TRUE(compare_from_string("0.0001", "0.0001", false));
  54. TEST_ASSERT_TRUE(compare_from_string("1E100", "10E99", false));
  55. TEST_ASSERT_FALSE(compare_from_string("0.5E-100", "0.5E-101", false));
  56. TEST_ASSERT_FALSE(compare_from_string("1", "2", true));
  57. TEST_ASSERT_FALSE(compare_from_string("1", "2", false));
  58. }
  59. static void cjson_compare_should_compare_booleans(void)
  60. {
  61. /* true */
  62. TEST_ASSERT_TRUE(compare_from_string("true", "true", true));
  63. TEST_ASSERT_TRUE(compare_from_string("true", "true", false));
  64. /* false */
  65. TEST_ASSERT_TRUE(compare_from_string("false", "false", true));
  66. TEST_ASSERT_TRUE(compare_from_string("false", "false", false));
  67. /* mixed */
  68. TEST_ASSERT_FALSE(compare_from_string("true", "false", true));
  69. TEST_ASSERT_FALSE(compare_from_string("true", "false", false));
  70. TEST_ASSERT_FALSE(compare_from_string("false", "true", true));
  71. TEST_ASSERT_FALSE(compare_from_string("false", "true", false));
  72. }
  73. static void cjson_compare_should_compare_null(void)
  74. {
  75. TEST_ASSERT_TRUE(compare_from_string("null", "null", true));
  76. TEST_ASSERT_TRUE(compare_from_string("null", "null", false));
  77. TEST_ASSERT_FALSE(compare_from_string("null", "true", true));
  78. TEST_ASSERT_FALSE(compare_from_string("null", "true", false));
  79. }
  80. static void cjson_compare_should_not_accept_invalid_types(void)
  81. {
  82. cJSON invalid[1];
  83. memset(invalid, '\0', sizeof(invalid));
  84. invalid->type = cJSON_Number | cJSON_String;
  85. TEST_ASSERT_FALSE(cJSON_Compare(invalid, invalid, true));
  86. TEST_ASSERT_FALSE(cJSON_Compare(invalid, invalid, false));
  87. }
  88. static void cjson_compare_should_compare_strings(void)
  89. {
  90. TEST_ASSERT_TRUE(compare_from_string("\"abcdefg\"", "\"abcdefg\"", true));
  91. TEST_ASSERT_TRUE(compare_from_string("\"abcdefg\"", "\"abcdefg\"", false));
  92. TEST_ASSERT_FALSE(compare_from_string("\"ABCDEFG\"", "\"abcdefg\"", true));
  93. TEST_ASSERT_FALSE(compare_from_string("\"ABCDEFG\"", "\"abcdefg\"", false));
  94. }
  95. static void cjson_compare_should_compare_raw(void)
  96. {
  97. cJSON *raw1 = NULL;
  98. cJSON *raw2 = NULL;
  99. raw1 = cJSON_Parse("\"[true, false]\"");
  100. TEST_ASSERT_NOT_NULL(raw1);
  101. raw2 = cJSON_Parse("\"[true, false]\"");
  102. TEST_ASSERT_NOT_NULL(raw2);
  103. raw1->type = cJSON_Raw;
  104. raw2->type = cJSON_Raw;
  105. TEST_ASSERT_TRUE(cJSON_Compare(raw1, raw2, true));
  106. TEST_ASSERT_TRUE(cJSON_Compare(raw1, raw2, false));
  107. cJSON_Delete(raw1);
  108. cJSON_Delete(raw2);
  109. }
  110. static void cjson_compare_should_compare_arrays(void)
  111. {
  112. TEST_ASSERT_TRUE(compare_from_string("[]", "[]", true));
  113. TEST_ASSERT_TRUE(compare_from_string("[]", "[]", false));
  114. TEST_ASSERT_TRUE(compare_from_string("[false,true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", true));
  115. TEST_ASSERT_TRUE(compare_from_string("[false,true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", false));
  116. TEST_ASSERT_TRUE(compare_from_string("[[[1], 2]]", "[[[1], 2]]", true));
  117. TEST_ASSERT_TRUE(compare_from_string("[[[1], 2]]", "[[[1], 2]]", false));
  118. TEST_ASSERT_FALSE(compare_from_string("[true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", true));
  119. TEST_ASSERT_FALSE(compare_from_string("[true,null,42,\"string\",[],{}]", "[false, true, null, 42, \"string\", [], {}]", false));
  120. /* Arrays that are a prefix of another array */
  121. TEST_ASSERT_FALSE(compare_from_string("[1,2,3]", "[1,2]", true));
  122. TEST_ASSERT_FALSE(compare_from_string("[1,2,3]", "[1,2]", false));
  123. }
  124. static void cjson_compare_should_compare_objects(void)
  125. {
  126. TEST_ASSERT_TRUE(compare_from_string("{}", "{}", true));
  127. TEST_ASSERT_TRUE(compare_from_string("{}", "{}", false));
  128. TEST_ASSERT_TRUE(compare_from_string(
  129. "{\"false\": false, \"true\": true, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  130. "{\"true\": true, \"false\": false, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  131. true));
  132. TEST_ASSERT_FALSE(compare_from_string(
  133. "{\"False\": false, \"true\": true, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  134. "{\"true\": true, \"false\": false, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  135. true));
  136. TEST_ASSERT_TRUE(compare_from_string(
  137. "{\"False\": false, \"true\": true, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  138. "{\"true\": true, \"false\": false, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  139. false));
  140. TEST_ASSERT_FALSE(compare_from_string(
  141. "{\"Flse\": false, \"true\": true, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  142. "{\"true\": true, \"false\": false, \"null\": null, \"number\": 42, \"string\": \"string\", \"array\": [], \"object\": {}}",
  143. false));
  144. /* test objects that are a subset of each other */
  145. TEST_ASSERT_FALSE(compare_from_string(
  146. "{\"one\": 1, \"two\": 2}",
  147. "{\"one\": 1, \"two\": 2, \"three\": 3}",
  148. true))
  149. TEST_ASSERT_FALSE(compare_from_string(
  150. "{\"one\": 1, \"two\": 2}",
  151. "{\"one\": 1, \"two\": 2, \"three\": 3}",
  152. false))
  153. }
  154. int CJSON_CDECL main(void)
  155. {
  156. UNITY_BEGIN();
  157. RUN_TEST(cjson_compare_should_compare_null_pointer_as_not_equal);
  158. RUN_TEST(cjson_compare_should_compare_invalid_as_not_equal);
  159. RUN_TEST(cjson_compare_should_compare_numbers);
  160. RUN_TEST(cjson_compare_should_compare_booleans);
  161. RUN_TEST(cjson_compare_should_compare_null);
  162. RUN_TEST(cjson_compare_should_not_accept_invalid_types);
  163. RUN_TEST(cjson_compare_should_compare_strings);
  164. RUN_TEST(cjson_compare_should_compare_raw);
  165. RUN_TEST(cjson_compare_should_compare_arrays);
  166. RUN_TEST(cjson_compare_should_compare_objects);
  167. return UNITY_END();
  168. }