summaryrefslogtreecommitdiff
path: root/tests/test_compare.c
blob: ed697cd153ea6c7de1444b5be46b8512166fd2f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Tests if json_object_equal behaves correct.
*/

#ifdef NDEBUG
#undef NDEBUG
#endif
#include "config.h"
#include <stdio.h>
#include <string.h>

#include "json_inttypes.h"
#include "json_object.h"

int main(int argc, char **argv)
{
	/* integer tests */
	struct json_object *int1 = json_object_new_int(0);
	struct json_object *int2 = json_object_new_int(1);
	struct json_object *int3 = json_object_new_int(1);
	struct json_object *int4 = json_object_new_int(-1);
	struct json_object *uint1 = json_object_new_uint64(0);
	struct json_object *uint2 = json_object_new_uint64(1);
	struct json_object *uint3 = json_object_new_uint64(1);
	struct json_object *uint4 = json_object_new_uint64((uint64_t)INT64_MAX + 100);

	if (!json_object_equal(int1, int2))
		printf("JSON integer comparison is correct\n");
	else
		printf("JSON integer comparison failed\n");

	if (json_object_equal(int1, int1))
		printf("JSON same object comparison is correct\n");
	else
		printf("JSON same object comparison failed\n");

	if (json_object_equal(int2, int3))
		printf("JSON same integer comparison is correct\n");
	else
		printf("JSON same integer comparison failed\n");

	if (!json_object_equal(uint1, uint2))
		printf("JSON usigned integer comparison is correct\n");
	else
		printf("JSON usigned integer comparison failed\n");

	if (json_object_equal(uint1, uint1))
		printf("JSON same usigned object comparison is correct\n");
	else
		printf("JSON same usigned object comparison failed\n");

	if (json_object_equal(uint2, uint3))
		printf("JSON same usigned integer comparison is correct\n");
	else
		printf("JSON same usigned integer comparison failed\n");

	if (json_object_equal(int2, uint2))
		printf("JSON integer & usigned integer comparison is correct\n");
	else
		printf("JSON integer & usigned integer comparison failed\n");

	if (!json_object_equal(int2, uint4))
		printf("JSON integer & usigned integer comparison is correct\n");
	else
		printf("JSON integer & usigned integer comparison failed\n");

	if (!json_object_equal(int4, uint2))
		printf("JSON integer & usigned integer comparison is correct\n");
	else
		printf("JSON integer & usigned integer comparison failed\n");

	if (!json_object_equal(int4, uint4))
		printf("JSON integer & usigned integer comparison is correct\n");
	else
		printf("JSON integer & usigned integer comparison failed\n");

	if (json_object_equal(uint2, int2))
		printf("JSON usigned integer & integer comparison is correct\n");
	else
		printf("JSON usigned integer & integer comparison failed\n");

	if (!json_object_equal(uint2, int4))
		printf("JSON usigned integer & integer comparison is correct\n");
	else
		printf("JSON usigned integer & integer comparison failed\n");

	if (!json_object_equal(uint4, int2))
		printf("JSON usigned integer & integer comparison is correct\n");
	else
		printf("JSON usigned integer & integer comparison failed\n");

	if (!json_object_equal(uint4, int4))
		printf("JSON usigned integer & integer comparison is correct\n");
	else
		printf("JSON usigned integer & integer comparison failed\n");

	json_object_put(int1);
	json_object_put(int2);
	json_object_put(int3);
	json_object_put(int4);
	json_object_put(uint1);
	json_object_put(uint2);
	json_object_put(uint3);
	json_object_put(uint4);

	/* string tests */
	struct json_object *str1 = json_object_new_string("TESTSTRING");
	struct json_object *str2 = json_object_new_string("TESTSTRING");
	struct json_object *str3 = json_object_new_string("DIFFERENT");

	if (json_object_equal(str1, str2))
		printf("Comparing equal strings is correct\n");
	else
		printf("Comparing equal strings failed\n");

	if (!json_object_equal(str1, str3))
		printf("Comparing different strings is correct\n");
	else
		printf("Comparing different strings failed\n");

	json_object_put(str1);
	json_object_put(str2);
	json_object_put(str3);

	/* double tests */
	struct json_object *dbl1 = json_object_new_double(3.14159);
	struct json_object *dbl2 = json_object_new_double(3.14159);
	struct json_object *dbl3 = json_object_new_double(3.0);

	if (json_object_equal(dbl1, dbl2))
		printf("Comparing equal doubles is correct\n");
	else
		printf("Comparing equal doubles failed\n");

	if (!json_object_equal(dbl1, dbl3))
		printf("Comparing different doubles is correct\n");
	else
		printf("Comparing different doubles failed\n");

	json_object_put(dbl1);
	json_object_put(dbl2);
	json_object_put(dbl3);

	/* array tests */
	struct json_object *ar1 = json_object_new_array();
	struct json_object *ar2 = json_object_new_array();
	struct json_object *ar3 = json_object_new_array();
	struct json_object *ar4 = json_object_new_array();

	json_object_array_add(ar1, json_object_new_int(1));
	json_object_array_add(ar1, json_object_new_int(2));

	json_object_array_add(ar2, json_object_new_int(1));
	json_object_array_add(ar2, json_object_new_int(2));

	json_object_array_add(ar3, json_object_new_int(1));
	json_object_array_add(ar3, json_object_new_int(1));

	if (json_object_equal(ar1, ar2))
		printf("Comparing equal arrays is correct\n");
	else
		printf("Comparing equal arrays failed\n");

	json_object_array_add(ar2, json_object_new_int(1));
	if (!json_object_equal(ar1, ar2))
		printf("Comparing arrays of different len is correct\n");
	else
		printf("Comparing arrays of different len failed\n");

	if (!json_object_equal(ar1, ar3))
		printf("Comparing different arrays is correct\n");
	else
		printf("Comparing different arrays failed\n");

	if (!json_object_equal(ar1, ar4))
		printf("Comparing different arrays (one empty) is correct\n");
	else
		printf("Comparing different arrays (one empty) failed\n");

	json_object_put(ar1);
	json_object_put(ar2);
	json_object_put(ar3);
	json_object_put(ar4);

	/* object tests */
	struct json_object *obj1 = json_object_new_object();
	struct json_object *obj2 = json_object_new_object();

	json_object_object_add(obj1, "test1", json_object_new_int(123));
	json_object_object_add(obj1, "test2", json_object_new_int(321));
	json_object_object_add(obj1, "test3", json_object_new_int(320));
	json_object_object_add(obj1, "test4", json_object_new_int(319));
	json_object_object_add(obj1, "test5", json_object_new_int(318));

	json_object_object_add(obj2, "test5", json_object_new_int(318));
	json_object_object_add(obj2, "test4", json_object_new_int(319));
	json_object_object_add(obj2, "test3", json_object_new_int(320));
	json_object_object_add(obj2, "test2", json_object_new_int(321));
	json_object_object_add(obj2, "test1", json_object_new_int(123));

	/* key-order is different between obj1 and obj2, should still be equal */
	if (json_object_equal(obj1, obj2))
		printf("Comparing JSON object with different key order is correct\n");
	else
		printf("Comparing JSON object with different key order is incorrect\n");

	/* make obj2 look different to obj1 */
	json_object_object_add(obj2, "test3", json_object_new_int(234));
	if (!json_object_equal(obj1, obj2))
		printf("Comparing different objects is correct\n");
	else
		printf("Comparing different objects is incorrect\n");

	/* iterate over jso2 keys to see if any exist that are not in jso1 */
	json_object_object_add(obj2, "test3", json_object_new_int(320));
	json_object_object_add(obj2, "test6", json_object_new_int(321));
	if (!json_object_equal(obj1, obj2))
		printf("Comparing different objects is correct\n");
	else
		printf("Comparing different objects is incorrect\n");

	/* iterate over jso1 keys and see if they exist in jso1 */
	json_object_object_add(obj1, "test6", json_object_new_int(321));
	if (json_object_equal(obj1, obj2))
		printf("Comparing different objects is correct\n");
	else
		printf("Comparing different objects is incorrect\n");
	json_object_object_add(obj1, "test7", json_object_new_int(322));
	if (!json_object_equal(obj1, obj2))
		printf("Comparing different objects is correct\n");
	else
		printf("Comparing different objects is incorrect\n");

	json_object_put(obj1);
	json_object_put(obj2);

	/* different types tests */
	struct json_object *int5 = json_object_new_int(0);
	struct json_object *dbl5 = json_object_new_double(3.14159);

	if (!json_object_equal(int5, NULL))
		printf("JSON integer and NULL comparison is correct\n");
	else
		printf("JSON integer and NULL comparison failed\n");

	if (!json_object_equal(NULL, dbl5))
		printf("JSON NULL and double comparison is correct\n");
	else
		printf("JSON NULL and double comparison failed\n");

	if (!json_object_equal(int5, dbl5))
		printf("JSON integer and double comparison is correct\n");
	else
		printf("JSON integer and double comparison failed\n");

	json_object_put(int5);
	json_object_put(dbl5);

	return 0;
}