Refactor decoder input stream
[jansson.git] / test / suites / api / test_pack.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 *value;
17     int i;
18     json_error_t error;
19
20     /*
21      * Simple, valid json_pack cases
22      */
23
24     /* true */
25     value = json_pack_ex(&error, 0, "b", 1);
26     if(!json_is_true(value))
27         fail("json_pack boolean failed");
28     if(value->refcount != (ssize_t)-1)
29         fail("json_pack boolean refcount failed");
30     json_decref(value);
31
32     /* false */
33     value = json_pack_ex(&error, 0, "b", 0);
34     if(!json_is_false(value))
35         fail("json_pack boolean failed");
36     if(value->refcount != (ssize_t)-1)
37         fail("json_pack boolean refcount failed");
38     json_decref(value);
39
40     /* null */
41     value = json_pack_ex(&error, 0, "n");
42     if(!json_is_null(value))
43         fail("json_pack null failed");
44     if(value->refcount != (ssize_t)-1)
45         fail("json_pack null refcount failed");
46     json_decref(value);
47
48     /* integer */
49     value = json_pack_ex(&error, 0, "i", 1);
50     if(!json_is_integer(value) || json_integer_value(value) != 1)
51         fail("json_pack integer failed");
52     if(value->refcount != (ssize_t)1)
53         fail("json_pack integer refcount failed");
54     json_decref(value);
55
56
57     /* real */
58     value = json_pack_ex(&error, 0, "f", 1.0);
59     if(!json_is_real(value) || json_real_value(value) != 1.0)
60         fail("json_pack real failed");
61     if(value->refcount != (ssize_t)1)
62         fail("json_pack real refcount failed");
63     json_decref(value);
64
65     /* string */
66     value = json_pack_ex(&error, 0, "s", "test");
67     if(!json_is_string(value) || strcmp("test", json_string_value(value)))
68         fail("json_pack string failed");
69     if(value->refcount != (ssize_t)1)
70         fail("json_pack string refcount failed");
71     json_decref(value);
72
73     /* empty object */
74     value = json_pack_ex(&error, 0, "{}", 1.0);
75     if(!json_is_object(value) || json_object_size(value) != 0)
76         fail("json_pack empty object failed");
77     if(value->refcount != (ssize_t)1)
78         fail("json_pack empty object refcount failed");
79     json_decref(value);
80
81     /* empty list */
82     value = json_pack_ex(&error, 0, "[]", 1.0);
83     if(!json_is_array(value) || json_array_size(value) != 0)
84         fail("json_pack empty list failed");
85     if(value->refcount != (ssize_t)1)
86         fail("json_pack empty list failed");
87     json_decref(value);
88
89     /* non-incref'd object */
90     value = json_pack_ex(&error, 0, "o", json_integer(1));
91     if(!json_is_integer(value) || json_integer_value(value) != 1)
92         fail("json_pack object failed");
93     if(value->refcount != (ssize_t)1)
94         fail("json_pack integer refcount failed");
95     json_decref(value);
96
97     /* incref'd object */
98     value = json_pack_ex(&error, 0, "O", json_integer(1));
99     if(!json_is_integer(value) || json_integer_value(value) != 1)
100         fail("json_pack object failed");
101     if(value->refcount != (ssize_t)2)
102         fail("json_pack integer refcount failed");
103     json_decref(value);
104     json_decref(value);
105
106     /* simple object */
107     value = json_pack_ex(&error, 0, "{s:[]}", "foo");
108     if(!json_is_object(value) || json_object_size(value) != 1)
109         fail("json_pack array failed");
110     if(!json_is_array(json_object_get(value, "foo")))
111         fail("json_pack array failed");
112     if(json_object_get(value, "foo")->refcount != (ssize_t)1)
113         fail("json_pack object refcount failed");
114     json_decref(value);
115
116     /* simple array */
117     value = json_pack_ex(&error, 0, "[i,i,i]", 0, 1, 2);
118     if(!json_is_array(value) || json_array_size(value) != 3)
119         fail("json_pack object failed");
120     for(i=0; i<3; i++)
121     {
122         if(!json_is_integer(json_array_get(value, i)) ||
123            json_integer_value(json_array_get(value, i)) != i)
124
125             fail("json_pack integer array failed");
126     }
127     json_decref(value);
128
129     /* Whitespace; regular string */
130     value = json_pack_ex(&error, 0, " s ", "test");
131     if(!json_is_string(value) || strcmp("test", json_string_value(value)))
132         fail("json_pack string (with whitespace) failed");
133     json_decref(value);
134
135     /* Whitespace; empty array */
136     value = json_pack_ex(&error, 0, "[ ]");
137     if(!json_is_array(value) || json_array_size(value) != 0)
138         fail("json_pack empty array (with whitespace) failed");
139     json_decref(value);
140
141     /* Whitespace; array */
142     value = json_pack_ex(&error, 0, "[ i , i,  i ] ", 1, 2, 3);
143     if(!json_is_array(value) || json_array_size(value) != 3)
144         fail("json_pack array (with whitespace) failed");
145     json_decref(value);
146
147     /*
148      * Invalid cases
149      */
150
151     /* mismatched open/close array/object */
152     if(json_pack_ex(&error, 0, "[}"))
153         fail("json_pack failed to catch mismatched '}'");
154     if(error.line != 1 || error.column != 2)
155         fail("json_pack didn't get the error coordinates right!");
156
157     if(json_pack_ex(&error, 0, "{]"))
158         fail("json_pack failed to catch mismatched ']'");
159     if(error.line != 1 || error.column != 2)
160         fail("json_pack didn't get the error coordinates right!");
161
162     /* missing close array */
163     if(json_pack_ex(&error, 0, "["))
164         fail("json_pack failed to catch missing ']'");
165     if(error.line != 1 || error.column != 2)
166         fail("json_pack didn't get the error coordinates right!");
167
168     /* missing close object */
169     if(json_pack_ex(&error, 0, "{"))
170         fail("json_pack failed to catch missing '}'");
171     if(error.line != 1 || error.column != 2)
172         fail("json_pack didn't get the error coordinates right!");
173
174     /* NULL string */
175     if(json_pack_ex(&error, 0, "s", NULL))
176         fail("json_pack failed to catch null argument string");
177     if(error.line != 1 || error.column != 1)
178         fail("json_pack didn't get the error coordinates right!");
179
180     /* NULL format */
181     if(json_pack_ex(&error, 0, NULL))
182         fail("json_pack failed to catch NULL format string");
183     if(error.line != -1 || error.column != -1)
184         fail("json_pack didn't get the error coordinates right!");
185
186     /* More complicated checks for row/columns */
187     if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
188         fail("json_pack failed to catch object as key");
189     if(error.line != 1 || error.column != 3)
190         fail("json_pack didn't get the error coordinates right!");
191
192     if(json_pack_ex(&error, 0, "{ s: {},  s:[ii{} }", "foo", "bar", 12, 13))
193         fail("json_pack failed to catch missing ]");
194     if(error.line != 1 || error.column != 19)
195         fail("json_pack didn't get the error coordinates right!");
196
197     if(json_pack_ex(&error, 0, "[[[[[   [[[[[  [[[[ }]]]] ]]]] ]]]]]"))
198         fail("json_pack failed to catch extra }");
199     if(error.line != 1 || error.column != 21)
200         fail("json_pack didn't get the error coordinates right!");
201
202     return 0;
203 }