992a61147e61168109eb38a032409bfeb91dacba
[jansson.git] / test / suites / api / test_unpack.c
1 /*
2  * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3  * Copyright (c) 2010-2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
4  *
5  * Jansson is free software; you can redistribute it and/or modify
6  * it under the terms of the MIT license. See LICENSE for details.
7  */
8
9 #include <string.h>
10 #include <jansson.h>
11 #include <stdio.h>
12 #include "util.h"
13
14 int main()
15 {
16     json_t *j, *j2;
17     int i1, i2, i3;
18     int rv;
19     double f;
20     char *s;
21
22     json_error_t error;
23
24     /*
25      * Simple, valid json_pack cases
26      */
27
28     /* true */
29     rv = json_unpack_ex(json_true(), &error, 0, "b", &i1);
30     if(rv || !i1)
31         fail("json_unpack boolean failed");
32
33     /* false */
34     rv = json_unpack_ex(json_false(), &error, 0, "b", &i1);
35     if(rv || i1)
36         fail("json_unpack boolean failed");
37
38     /* null */
39     if(json_unpack_ex(json_null(), &error, 0, "n"))
40         fail("json_unpack null failed");
41
42     /* integer */
43     j = json_integer(42);
44     rv = json_unpack_ex(j, &error, 0, "i", &i1);
45     if(rv || i1 != 42)
46         fail("json_unpack integer failed");
47     json_decref(j);
48
49     /* real */
50     j = json_real(1.7);
51     rv = json_unpack_ex(j, &error, 0, "f", &f);
52     if(rv || f != 1.7)
53         fail("json_unpack real failed");
54     json_decref(j);
55
56     /* string */
57     j = json_string("foo");
58     rv = json_unpack_ex(j, &error, 0, "s", &s);
59     if(rv || strcmp(s, "foo"))
60         fail("json_unpack string failed");
61     json_decref(j);
62
63     /* empty object */
64     j = json_object();
65     if(json_unpack_ex(j, &error, 0, "{}"))
66         fail("json_unpack empty object failed");
67     json_decref(j);
68
69     /* empty list */
70     j = json_array();
71     if(json_unpack_ex(j, &error, 0, "[]"))
72         fail("json_unpack empty list failed");
73     json_decref(j);
74
75     /* non-incref'd object */
76     j = json_object();
77     rv = json_unpack_ex(j, &error, 0, "o", &j2);
78     if(j2 != j || j->refcount != 1)
79         fail("json_unpack object failed");
80     json_decref(j);
81
82     /* incref'd object */
83     j = json_object();
84     rv = json_unpack_ex(j, &error, 0, "O", &j2);
85     if(j2 != j || j->refcount != 2)
86         fail("json_unpack object failed");
87     json_decref(j);
88     json_decref(j);
89
90     /* simple object */
91     j = json_pack("{s:i}", "foo", 42);
92     rv = json_unpack_ex(j, &error, 0, "{s:i}", "foo", &i1);
93     if(rv || i1 != 42)
94         fail("json_unpack simple object failed");
95     json_decref(j);
96
97     /* simple array */
98     j = json_pack("[iii]", 1, 2, 3);
99     rv = json_unpack_ex(j, &error, 0, "[i,i,i]", &i1, &i2, &i3);
100     if(rv || i1 != 1 || i2 != 2 || i3 != 3)
101         fail("json_unpack simple array failed");
102     json_decref(j);
103
104     /*
105      * Invalid cases
106      */
107
108     /* mismatched open/close array/object */
109     j = json_pack("[]");
110     if(!json_unpack_ex(j, &error, 0, "[}"))
111         fail("json_unpack failed to catch mismatched ']'");
112     json_decref(j);
113
114     j = json_pack("{}");
115     if(!json_unpack_ex(j, &error, 0, "{]"))
116         fail("json_unpack failed to catch mismatched '}'");
117     json_decref(j);
118
119     /* missing close array */
120     j = json_pack("[]");
121     if(!json_unpack_ex(j, &error, 0, "["))
122         fail("json_unpack failed to catch missing ']'");
123     json_decref(j);
124
125     /* missing close object */
126     j = json_pack("{}");
127     if(!json_unpack_ex(j, &error, 0, "{"))
128         fail("json_unpack failed to catch missing '}'");
129     json_decref(j);
130
131     /* NULL format string */
132     j = json_pack("[]");
133     if(!json_unpack_ex(j, &error, 0, NULL))
134         fail("json_unpack failed to catch null format string");
135     json_decref(j);
136
137     /* NULL string pointer */
138     j = json_string("foobie");
139     if(!json_unpack_ex(j, &error, 0, "s", NULL))
140         fail("json_unpack failed to catch null string pointer");
141     json_decref(j);
142
143     return 0;
144 }