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