1 /* 2 * Tests if json_object_equal behaves correct. 3 */ 4 5 #include "config.h" 6 #include <stdio.h> 7 #include <string.h> 8 9 #include "json_inttypes.h" 10 #include "json_object.h" 11 12 int main() 13 { 14 /* integer tests */ 15 struct json_object *int1 = json_object_new_int(0); 16 struct json_object *int2 = json_object_new_int(1); 17 struct json_object *int3 = json_object_new_int(1); 18 struct json_object *int4 = json_object_new_int(-1); 19 struct json_object *uint1 = json_object_new_uint64(0); 20 struct json_object *uint2 = json_object_new_uint64(1); 21 struct json_object *uint3 = json_object_new_uint64(1); 22 struct json_object *uint4 = json_object_new_uint64((uint64_t)INT64_MAX + 100); 23 24 if (!json_object_equal(int1, int2)) 25 printf("JSON integer comparison is correct\n"); 26 else 27 printf("JSON integer comparison failed\n"); 28 29 if (json_object_equal(int1, int1)) 30 printf("JSON same object comparison is correct\n"); 31 else 32 printf("JSON same object comparison failed\n"); 33 34 if (json_object_equal(int2, int3)) 35 printf("JSON same integer comparison is correct\n"); 36 else 37 printf("JSON same integer comparison failed\n"); 38 39 if (!json_object_equal(uint1, uint2)) 40 printf("JSON usigned integer comparison is correct\n"); 41 else 42 printf("JSON usigned integer comparison failed\n"); 43 44 if (json_object_equal(uint1, uint1)) 45 printf("JSON same usigned object comparison is correct\n"); 46 else 47 printf("JSON same usigned object comparison failed\n"); 48 49 if (json_object_equal(uint2, uint3)) 50 printf("JSON same usigned integer comparison is correct\n"); 51 else 52 printf("JSON same usigned integer comparison failed\n"); 53 54 if (json_object_equal(int2, uint2)) 55 printf("JSON integer & usigned integer comparison is correct\n"); 56 else 57 printf("JSON integer & usigned integer comparison failed\n"); 58 59 if (!json_object_equal(int2, uint4)) 60 printf("JSON integer & usigned integer comparison is correct\n"); 61 else 62 printf("JSON integer & usigned integer comparison failed\n"); 63 64 if (!json_object_equal(int4, uint2)) 65 printf("JSON integer & usigned integer comparison is correct\n"); 66 else 67 printf("JSON integer & usigned integer comparison failed\n"); 68 69 if (!json_object_equal(int4, uint4)) 70 printf("JSON integer & usigned integer comparison is correct\n"); 71 else 72 printf("JSON integer & usigned integer comparison failed\n"); 73 74 if (json_object_equal(uint2, int2)) 75 printf("JSON usigned integer & integer comparison is correct\n"); 76 else 77 printf("JSON usigned integer & integer comparison failed\n"); 78 79 if (!json_object_equal(uint2, int4)) 80 printf("JSON usigned integer & integer comparison is correct\n"); 81 else 82 printf("JSON usigned integer & integer comparison failed\n"); 83 84 if (!json_object_equal(uint4, int2)) 85 printf("JSON usigned integer & integer comparison is correct\n"); 86 else 87 printf("JSON usigned integer & integer comparison failed\n"); 88 89 if (!json_object_equal(uint4, int4)) 90 printf("JSON usigned integer & integer comparison is correct\n"); 91 else 92 printf("JSON usigned integer & integer comparison failed\n"); 93 94 json_object_put(int1); 95 json_object_put(int2); 96 json_object_put(int3); 97 json_object_put(int4); 98 json_object_put(uint1); 99 json_object_put(uint2); 100 json_object_put(uint3); 101 json_object_put(uint4); 102 103 /* string tests */ 104 struct json_object *str1 = json_object_new_string("TESTSTRING"); 105 struct json_object *str2 = json_object_new_string("TESTSTRING"); 106 struct json_object *str3 = json_object_new_string("DIFFERENT"); 107 108 if (json_object_equal(str1, str2)) 109 printf("Comparing equal strings is correct\n"); 110 else 111 printf("Comparing equal strings failed\n"); 112 113 if (!json_object_equal(str1, str3)) 114 printf("Comparing different strings is correct\n"); 115 else 116 printf("Comparing different strings failed\n"); 117 118 json_object_put(str1); 119 json_object_put(str2); 120 json_object_put(str3); 121 122 /* double tests */ 123 struct json_object *dbl1 = json_object_new_double(3.14159); 124 struct json_object *dbl2 = json_object_new_double(3.14159); 125 struct json_object *dbl3 = json_object_new_double(3.0); 126 127 if (json_object_equal(dbl1, dbl2)) 128 printf("Comparing equal doubles is correct\n"); 129 else 130 printf("Comparing equal doubles failed\n"); 131 132 if (!json_object_equal(dbl1, dbl3)) 133 printf("Comparing different doubles is correct\n"); 134 else 135 printf("Comparing different doubles failed\n"); 136 137 json_object_put(dbl1); 138 json_object_put(dbl2); 139 json_object_put(dbl3); 140 141 /* array tests */ 142 struct json_object *ar1 = json_object_new_array(); 143 struct json_object *ar2 = json_object_new_array(); 144 struct json_object *ar3 = json_object_new_array(); 145 struct json_object *ar4 = json_object_new_array(); 146 147 json_object_array_add(ar1, json_object_new_int(1)); 148 json_object_array_add(ar1, json_object_new_int(2)); 149 150 json_object_array_add(ar2, json_object_new_int(1)); 151 json_object_array_add(ar2, json_object_new_int(2)); 152 153 json_object_array_add(ar3, json_object_new_int(1)); 154 json_object_array_add(ar3, json_object_new_int(1)); 155 156 if (json_object_equal(ar1, ar2)) 157 printf("Comparing equal arrays is correct\n"); 158 else 159 printf("Comparing equal arrays failed\n"); 160 161 json_object_array_add(ar2, json_object_new_int(1)); 162 if (!json_object_equal(ar1, ar2)) 163 printf("Comparing arrays of different len is correct\n"); 164 else 165 printf("Comparing arrays of different len failed\n"); 166 167 if (!json_object_equal(ar1, ar3)) 168 printf("Comparing different arrays is correct\n"); 169 else 170 printf("Comparing different arrays failed\n"); 171 172 if (!json_object_equal(ar1, ar4)) 173 printf("Comparing different arrays (one empty) is correct\n"); 174 else 175 printf("Comparing different arrays (one empty) failed\n"); 176 177 json_object_put(ar1); 178 json_object_put(ar2); 179 json_object_put(ar3); 180 json_object_put(ar4); 181 182 /* object tests */ 183 struct json_object *obj1 = json_object_new_object(); 184 struct json_object *obj2 = json_object_new_object(); 185 186 json_object_object_add(obj1, "test1", json_object_new_int(123)); 187 json_object_object_add(obj1, "test2", json_object_new_int(321)); 188 json_object_object_add(obj1, "test3", json_object_new_int(320)); 189 json_object_object_add(obj1, "test4", json_object_new_int(319)); 190 json_object_object_add(obj1, "test5", json_object_new_int(318)); 191 192 json_object_object_add(obj2, "test5", json_object_new_int(318)); 193 json_object_object_add(obj2, "test4", json_object_new_int(319)); 194 json_object_object_add(obj2, "test3", json_object_new_int(320)); 195 json_object_object_add(obj2, "test2", json_object_new_int(321)); 196 json_object_object_add(obj2, "test1", json_object_new_int(123)); 197 198 /* key-order is different between obj1 and obj2, should still be equal */ 199 if (json_object_equal(obj1, obj2)) 200 printf("Comparing JSON object with different key order is correct\n"); 201 else 202 printf("Comparing JSON object with different key order is incorrect\n"); 203 204 /* make obj2 look different to obj1 */ 205 json_object_object_add(obj2, "test3", json_object_new_int(234)); 206 if (!json_object_equal(obj1, obj2)) 207 printf("Comparing different objects is correct\n"); 208 else 209 printf("Comparing different objects is incorrect\n"); 210 211 /* iterate over jso2 keys to see if any exist that are not in jso1 */ 212 json_object_object_add(obj2, "test3", json_object_new_int(320)); 213 json_object_object_add(obj2, "test6", json_object_new_int(321)); 214 if (!json_object_equal(obj1, obj2)) 215 printf("Comparing different objects is correct\n"); 216 else 217 printf("Comparing different objects is incorrect\n"); 218 219 /* iterate over jso1 keys and see if they exist in jso1 */ 220 json_object_object_add(obj1, "test6", json_object_new_int(321)); 221 if (json_object_equal(obj1, obj2)) 222 printf("Comparing different objects is correct\n"); 223 else 224 printf("Comparing different objects is incorrect\n"); 225 json_object_object_add(obj1, "test7", json_object_new_int(322)); 226 if (!json_object_equal(obj1, obj2)) 227 printf("Comparing different objects is correct\n"); 228 else 229 printf("Comparing different objects is incorrect\n"); 230 231 json_object_put(obj1); 232 json_object_put(obj2); 233 234 /* different types tests */ 235 struct json_object *int5 = json_object_new_int(0); 236 struct json_object *dbl5 = json_object_new_double(3.14159); 237 238 if (!json_object_equal(int5, NULL)) 239 printf("JSON integer and NULL comparison is correct\n"); 240 else 241 printf("JSON integer and NULL comparison failed\n"); 242 243 if (!json_object_equal(NULL, dbl5)) 244 printf("JSON NULL and double comparison is correct\n"); 245 else 246 printf("JSON NULL and double comparison failed\n"); 247 248 if (!json_object_equal(int5, dbl5)) 249 printf("JSON integer and double comparison is correct\n"); 250 else 251 printf("JSON integer and double comparison failed\n"); 252 253 json_object_put(int5); 254 json_object_put(dbl5); 255 256 return 0; 257 } 258
This page was automatically generated by LXR 0.3.1. • OpenWrt