Add equality test for JSON values
[jansson.git] / test / suites / api / test_equal.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <jansson.h>
9 #include "util.h"
10
11 static void test_equal_simple()
12 {
13     json_t *value1, *value2;
14
15     if(json_equal(NULL, NULL))
16         fail("json_equal fails for two NULLs");
17
18     value1 = json_true();
19     if(json_equal(value1, NULL) || json_equal(NULL, value1))
20         fail("json_equal fails for NULL");
21
22     /* this covers true, false and null as they are singletons */
23     if(!json_equal(value1, value1))
24         fail("identical objects are not equal");
25     json_decref(value1);
26
27     /* integer */
28     value1 = json_integer(1);
29     value2 = json_integer(1);
30     if(!value1 || !value2)
31         fail("unable to create integers");
32     if(!json_equal(value1, value2))
33         fail("json_equal fails for two equal integers");
34     json_decref(value2);
35
36     value2 = json_integer(2);
37     if(!value2)
38         fail("unable to create an integer");
39     if(json_equal(value1, value2))
40         fail("json_equal fails for two inequal integers");
41
42     json_decref(value1);
43     json_decref(value2);
44
45     /* real */
46     value1 = json_real(1.2);
47     value2 = json_real(1.2);
48     if(!value1 || !value2)
49         fail("unable to create reals");
50     if(!json_equal(value1, value2))
51         fail("json_equal fails for two equal reals");
52     json_decref(value2);
53
54     value2 = json_real(3.141592);
55     if(!value2)
56         fail("unable to create an real");
57     if(json_equal(value1, value2))
58         fail("json_equal fails for two inequal reals");
59
60     json_decref(value1);
61     json_decref(value2);
62
63     /* string */
64     value1 = json_string("foo");
65     value2 = json_string("foo");
66     if(!value1 || !value2)
67         fail("unable to create strings");
68     if(!json_equal(value1, value2))
69         fail("json_equal fails for two equal strings");
70     json_decref(value2);
71
72     value2 = json_string("bar");
73     if(!value2)
74         fail("unable to create an string");
75     if(json_equal(value1, value2))
76         fail("json_equal fails for two inequal strings");
77
78     json_decref(value1);
79     json_decref(value2);
80 }
81
82 static void test_equal_array()
83 {
84     json_t *array1, *array2;
85
86     array1 = json_array();
87     array2 = json_array();
88     if(!array1 || !array2)
89         fail("unable to create arrays");
90
91     if(!json_equal(array1, array2))
92         fail("json_equal fails for two empty arrays");
93
94     json_array_append_new(array1, json_integer(1));
95     json_array_append_new(array2, json_integer(1));
96     json_array_append_new(array1, json_string("foo"));
97     json_array_append_new(array2, json_string("foo"));
98     json_array_append_new(array1, json_integer(2));
99     json_array_append_new(array2, json_integer(2));
100     if(!json_equal(array1, array2))
101         fail("json_equal fails for two equal arrays");
102
103     json_array_remove(array2, 2);
104     if(json_equal(array1, array2))
105         fail("json_equal fails for two inequal arrays");
106
107     json_array_append_new(array2, json_integer(3));
108     if(json_equal(array1, array2))
109         fail("json_equal fails for two inequal arrays");
110
111     json_decref(array1);
112     json_decref(array2);
113 }
114
115 static void test_equal_object()
116 {
117     json_t *object1, *object2;
118
119     object1 = json_object();
120     object2 = json_object();
121     if(!object1 || !object2)
122         fail("unable to create objects");
123
124     if(!json_equal(object1, object2))
125         fail("json_equal fails for two empty objects");
126
127     json_object_set_new(object1, "a", json_integer(1));
128     json_object_set_new(object2, "a", json_integer(1));
129     json_object_set_new(object1, "b", json_string("foo"));
130     json_object_set_new(object2, "b", json_string("foo"));
131     json_object_set_new(object1, "c", json_integer(2));
132     json_object_set_new(object2, "c", json_integer(2));
133     if(!json_equal(object1, object2))
134         fail("json_equal fails for two equal objects");
135
136     json_object_del(object2, "c");
137     if(json_equal(object1, object2))
138         fail("json_equal fails for two inequal objects");
139
140     json_object_set(object2, "c", json_integer(3));
141     if(json_equal(object1, object2))
142         fail("json_equal fails for two inequal objects");
143
144     json_object_del(object2, "c");
145     json_object_set(object2, "d", json_integer(2));
146     if(json_equal(object1, object2))
147         fail("json_equal fails for two inequal objects");
148
149     json_decref(object1);
150     json_decref(object2);
151 }
152
153 static void test_equal_complex()
154 {
155     json_t *value1, *value2;
156
157     const char *complex_json =
158 "{"
159 "    \"integer\": 1, "
160 "    \"real\": 3.141592, "
161 "    \"string\": \"foobar\", "
162 "    \"true\": true, "
163 "    \"object\": {"
164 "        \"array-in-object\": [1,true,\"foo\",{}],"
165 "        \"object-in-object\": {\"foo\": \"bar\"}"
166 "    },"
167 "    \"array\": [\"foo\", false, null, 1.234]"
168 "}";
169
170     value1 = json_loads(complex_json, NULL);
171     value2 = json_loads(complex_json, NULL);
172     if(!value1 || !value2)
173         fail("unable to parse JSON");
174     if(!json_equal(value1, value2))
175         fail("json_equal fails for two inequal strings");
176
177     /* TODO: There's no negative test case here */
178 }
179
180 int main()
181 {
182     test_equal_simple();
183     test_equal_array();
184     test_equal_object();
185     test_equal_complex();
186     return 0;
187 }