Expand test coverage
[jansson.git] / test / testprogs / test_object.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 int main()
12 {
13     json_t *object, *string, *other_string, *value;
14
15     object = json_object();
16     string = json_string("test");
17     other_string = json_string("other");
18
19     if(!object)
20         fail("unable to create object");
21     if(!string)
22         fail("unable to create string");
23     if(!other_string)
24         fail("unable to create string");
25
26     if(json_object_get(object, "a"))
27         fail("value for nonexisting key");
28
29     if(json_object_set(object, "a", string))
30         fail("unable to set value");
31
32     iter = json_object_iter(object);
33     if(!iter)
34         fail("unable to get iterator");
35
36     if(strcmp(json_object_iter_key(iter), "a"))
37         fail("iterating failed: wrong key");
38     if(json_object_iter_value(iter) != string)
39         fail("iterating failed: wrong value");
40     if(json_object_iter_next(object, iter) != NULL)
41         fail("able to iterate over the end");
42
43     /* invalid UTF-8 in key */
44     if(!json_object_set(object, "a\xefz", string))
45         fail("able to set invalid unicode key");
46
47     value = json_object_get(object, "a");
48     if(!value)
49         fail("no value for existing key");
50     if(value != string)
51         fail("got different value than what was added");
52
53     /* "a", "lp" and "px" collide in a five-bucket hashtable */
54     if(json_object_set(object, "b", string) ||
55        json_object_set(object, "lp", string) ||
56        json_object_set(object, "px", string))
57         fail("unable to set value");
58
59     value = json_object_get(object, "a");
60     if(!value)
61         fail("no value for existing key");
62     if(value != string)
63         fail("got different value than what was added");
64
65     if(json_object_set(object, "a", other_string))
66         fail("unable to replace an existing key");
67
68     value = json_object_get(object, "a");
69     if(!value)
70         fail("no value for existing key");
71     if(value != other_string)
72         fail("got different value than what was set");
73
74     if(!json_object_del(object, "nonexisting"))
75         fail("able to delete a nonexisting key");
76
77     if(json_object_del(object, "px"))
78         fail("unable to delete an existing key");
79
80     if(json_object_del(object, "a"))
81         fail("unable to delete an existing key");
82
83     if(json_object_del(object, "lp"))
84         fail("unable to delete an existing key");
85
86
87     /* add many keys to initiate rehashing */
88
89     if(json_object_set(object, "a", string))
90         fail("unable to set value");
91
92     if(json_object_set(object, "lp", string))
93         fail("unable to set value");
94
95     if(json_object_set(object, "px", string))
96         fail("unable to set value");
97
98     if(json_object_set(object, "c", string))
99         fail("unable to set value");
100
101     if(json_object_set(object, "d", string))
102         fail("unable to set value");
103
104     if(json_object_set(object, "e", string))
105         fail("unable to set value");
106
107
108     json_object_set_new(object, "foo", json_integer(123));
109     value = json_object_get(object, "foo");
110     if(!json_is_integer(value) || json_integer_value(value) != 123)
111       fail("json_object_set_new works incorrectly");
112
113     json_decref(string);
114     json_decref(other_string);
115     json_decref(object);
116
117     return 0;
118 }