c3c8180347c33a1cf1804aff3262371f6057ea85
[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     //void* v;
20     double f;
21     char *s;
22
23     json_error_t error;
24
25     /*
26      * Simple, valid json_pack cases
27      */
28
29     /* true */
30     rv = json_unpack(json_true(), &error, "b", &i1);
31     if(rv || !i1)
32         fail("json_unpack boolean failed");
33
34     /* false */
35     rv = json_unpack(json_false(), &error, "b", &i1);
36     if(rv || i1)
37         fail("json_unpack boolean failed");
38
39     /* null */
40     rv = json_unpack(json_null(), &error, "n");
41     if(rv)
42         fail("json_unpack null failed");
43
44     /* integer */
45     j = json_integer(1);
46     rv = json_unpack(j, &error, "i", &i1);
47     if(rv || i1 != 1)
48         fail("json_unpack integer failed");
49     json_decref(j);
50
51     /* real */
52     j = json_real(1.0);
53     rv = json_unpack(j, &error, "f", &f);
54     if(rv || f != 1.0)
55         fail("json_unpack real failed");
56     json_decref(j);
57
58     /* string */
59     j = json_string("foo");
60     rv = json_unpack(j, &error, "s", &s);
61     if(rv || strcmp(s, "foo"))
62         fail("json_unpack string failed");
63     json_decref(j);
64
65     /* empty object */
66     j = json_object();
67     rv = json_unpack(j, &error, "{}");
68     if(rv)
69         fail("json_unpack empty object failed");
70     json_decref(j);
71
72     /* empty list */
73     j = json_array();
74     rv = json_unpack(j, &error, "[]");
75     if(rv)
76         fail("json_unpack empty list failed");
77     json_decref(j);
78
79     /* non-incref'd object */
80     j = json_object();
81     rv = json_unpack(j, &error, "o", &j2);
82     if(j2 != j || j->refcount != (ssize_t)1)
83         fail("json_unpack object failed");
84     json_decref(j);
85
86     /* incref'd object */
87     j = json_object();
88     rv = json_unpack(j, &error, "O", &j2);
89     if(j2 != j || j->refcount != (ssize_t)2)
90         fail("json_unpack object failed");
91     json_decref(j);
92     json_decref(j);
93
94     /* simple object */
95     j = json_pack(&error, "{s:i}", "foo", 1);
96     rv = json_unpack(j, &error, "{s:i}", "foo", &i1);
97     if(rv || i1!=1)
98         fail("json_unpack simple object failed");
99     json_decref(j);
100
101     /* simple array */
102     j = json_pack(&error, "[iii]", 1, 2, 3);
103     rv = json_unpack(j, &error, "[i,i,i]", &i1, &i2, &i3);
104     if(rv || i1 != 1 || i2 != 2 || i3 != 3)
105         fail("json_unpack simple array failed");
106     json_decref(j);
107
108     /*
109      * Invalid cases
110      */
111
112     /* mismatched open/close array/object */
113     j = json_pack(&error, "[]");
114     rv = json_unpack(j, &error, "[}");
115     if(!rv)
116         fail("json_unpack failed to catch mismatched ']'");
117     json_decref(j);
118
119     j = json_pack(&error, "{}");
120     rv = json_unpack(j, &error, "{]");
121     if(!rv)
122         fail("json_unpack failed to catch mismatched '}'");
123     json_decref(j);
124
125     /* missing close array */
126     j = json_pack(&error, "[]");
127     rv = json_unpack(j, &error, "[");
128     if(rv >= 0)
129         fail("json_unpack failed to catch missing ']'");
130     json_decref(j);
131
132     /* missing close object */
133     j = json_pack(&error, "{}");
134     rv = json_unpack(j, &error, "{");
135     if(rv >= 0)
136         fail("json_unpack failed to catch missing '}'");
137     json_decref(j);
138
139     /* NULL format string */
140     j = json_pack(&error, "[]");
141     rv =json_unpack(j, &error, NULL);
142     if(rv >= 0)
143         fail("json_unpack failed to catch null format string");
144     json_decref(j);
145
146     /* NULL string pointer */
147     j = json_string("foobie");
148     rv =json_unpack(j, &error, "s", NULL);
149     if(rv >= 0)
150         fail("json_unpack failed to catch null string pointer");
151     json_decref(j);
152
153     return 0;
154
155     //fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
156 }
157
158 /* vim: ts=4:expandtab:sw=4
159  */