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