tests/test_json.c

Thu, 28 Nov 2024 20:59:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Nov 2024 20:59:11 +0100
changeset 997
d14f3d5f47d1
parent 996
333155f234c4
permissions
-rw-r--r--

fix missing cxMempoolFree in json allocator test

937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/test.h"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include "cx/json.h"
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
32 #include "cx/mempool.h"
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
34 CX_TEST(test_json_init_default) {
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
35 CxJson json;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
36 CX_TEST_DO {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
37 cxJsonInit(NULL, &json);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
38 CX_TEST_ASSERT(json.states == json.states_internal);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
39 CX_TEST_ASSERT(json.nstates == 0);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
40 CX_TEST_ASSERT(json.states_alloc == 8);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
41 CX_TEST_ASSERT(json.reader_array_alloc == 8);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
42 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
43 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
44
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 CX_TEST(test_json_simple_object) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 cxstring text = cx_str(
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 "{\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 "\t\"message\":\"success\",\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 "\t\"position\":{\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 "\t\t\"longitude\":-94.7099,\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 "\t\t\"latitude\":51.5539\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 "\t},\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 "\t\"timestamp\":1729348561,\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 "\t\"alive\":true\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 "}"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 );
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 CX_TEST_DO {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 int result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CxJson json;
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
62 cxJsonInit(NULL, &json);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
63 cxJsonFill(&json, text);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 // parse the big fat object
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CxJsonValue *obj;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 result = cxJsonNext(&json, &obj);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 CX_TEST_ASSERT(result == 1);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 // check the contents
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 CX_TEST_ASSERT(cxJsonIsObject(obj));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CxJsonValue *message = cxJsonObjGet(obj, "message");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_TEST_ASSERT(cxJsonIsString(message));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 CX_TEST_ASSERT(0 == cx_strcmp(
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
76 cxJsonAsCxString(message),
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 cx_str("success"))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 );
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 CxJsonValue *position = cxJsonObjGet(obj, "position");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 CX_TEST_ASSERT(cxJsonIsObject(position));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 CxJsonValue *longitude = cxJsonObjGet(position, "longitude");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 CX_TEST_ASSERT(cxJsonIsNumber(longitude));
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
84 CX_TEST_ASSERT(!cxJsonIsInteger(longitude));
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 CX_TEST_ASSERT(cxJsonAsDouble(longitude) == -94.7099);
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
86 CX_TEST_ASSERT(cxJsonAsInteger(longitude) == -94);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 CxJsonValue *latitude = cxJsonObjGet(position, "latitude");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 CX_TEST_ASSERT(cxJsonIsNumber(latitude));
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
89 CX_TEST_ASSERT(!cxJsonIsInteger(latitude));
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 CX_TEST_ASSERT(cxJsonAsDouble(latitude) == 51.5539);
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
91 CX_TEST_ASSERT(cxJsonAsInteger(latitude) == 51);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
95 CX_TEST_ASSERT(cxJsonIsNumber(timestamp));
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
97 CX_TEST_ASSERT(cxJsonAsDouble(timestamp) == 1729348561.0);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 CxJsonValue *alive = cxJsonObjGet(obj, "alive");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 CX_TEST_ASSERT(cxJsonIsBool(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 CX_TEST_ASSERT(cxJsonIsTrue(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 CX_TEST_ASSERT(!cxJsonIsFalse(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 CX_TEST_ASSERT(cxJsonAsBool(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 // this recursively frees everything else
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 cxJsonValueFree(obj);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 // we only have one object that already contained all the data
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 result = cxJsonNext(&json, &obj);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 CX_TEST_ASSERT(result == 0);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 cxJsonDestroy(&json);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
116 CX_TEST(test_json_object_incomplete_token) {
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
117 cxstring text = cx_str(
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
118 "{\"message\":\"success\" , \"__timestamp\":1729348561}");
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
119 cxstring parts[16];
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
120 size_t nparts = 0; // split the json text into mulple parts
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
121 for(size_t i=0;i<text.length;i+=4) {
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
122 parts[nparts++] = cx_strsubsl(text, i, 4);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
123 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
124
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
125 CX_TEST_DO {
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
126 int result;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
127
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
128 CxJson json;
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
129 cxJsonInit(NULL, &json);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
130 CxJsonValue *obj;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
131
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
132 size_t part = 0;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
133 while(part < nparts) {
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
134 cxJsonFill(&json, parts[part]);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
135 part++;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
136 result = cxJsonNext(&json, &obj);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
137
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
138 if(result != 0) {
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
139 break;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
140 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
141 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
142
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
143 CX_TEST_ASSERT(result == 1);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
144 CX_TEST_ASSERT(part == nparts);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
145 CX_TEST_ASSERT(obj);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
146
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
147 CxJsonValue *message = cxJsonObjGet(obj, "message");
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
148 CX_TEST_ASSERT(cxJsonIsString(message));
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
149 CX_TEST_ASSERT(0 == cx_strcmp(
966
1aa7ec3e46e7 treat integers and doubles both as JSON numbers
Mike Becker <universe@uap-core.de>
parents: 946
diff changeset
150 cxJsonAsCxString(message),
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
151 cx_str("success"))
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
152 );
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
153 CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp");
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
154 CX_TEST_ASSERT(message->type == CX_JSON_STRING);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
155 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
156 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
157
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
158 // this recursively frees everything else
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
159 cxJsonValueFree(obj);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
160
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
161 // we only have one object that already contained all the data
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
162 result = cxJsonNext(&json, &obj);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
163 CX_TEST_ASSERT(result == 0);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
164
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
165 cxJsonDestroy(&json);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
166 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
167 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
168
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
169 CX_TEST(test_json_object_error) {
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
170 cxstring text0 = cx_str(
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
171 "{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
172 "\t\"message\":\"success\",\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
173 "\t\"data\":{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
174 "\t\t\"obj\":{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
175 "\t\t\t\"array\": [1, 2, 3, ?syntaxerror? ]\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
176 "\t\t\"}\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
177 "\t},\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
178 "\t\"timestamp\":1729348561,\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
179 "}"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
180 );
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
181 cxstring text1 = cx_str("{ \"string\" }");
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
182 cxstring text2 = cx_str("{ \"a\" : }");
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
183 cxstring text3 = cx_str("{ \"a\" : \"b\" ]");
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
184 cxstring text4 = cx_str("{ \"name\": \"value\" ]");
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
185
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
186 cxstring tests[] = { text0, text1, text2, text3, text4 };
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
187
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
188 CX_TEST_DO {
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
189 int result;
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
190 CxJson json;
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
191 CxJsonValue *obj = NULL;
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
192
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
193 for(int i=0;i<5;i++) {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
194 cxJsonInit(NULL, &json);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
195 cxJsonFill(&json, tests[i]);
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
196 result = cxJsonNext(&json, &obj);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
197
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
198 CX_TEST_ASSERT(result == -1);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
199 CX_TEST_ASSERT(obj == NULL);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
200 cxJsonDestroy(&json);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
201 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
202 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
203 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
204
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
205 CX_TEST(test_json_large_nesting_depth) {
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
206 CxJson json;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
207 CxJsonValue *d1;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
208 cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}");
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
209 CX_TEST_DO {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
210 cxJsonInit(NULL, &json);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
211 cxJsonFill(&json, text);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
212 cxJsonNext(&json, &d1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
213
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
214 CX_TEST_ASSERT(d1 != NULL);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
215 CX_TEST_ASSERT(cxJsonIsObject(d1));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
216 CxJsonValue *d2 = cxJsonObjGet(d1, "test");
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
217 CX_TEST_ASSERT(cxJsonIsArray(d2));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
218 CX_TEST_ASSERT(cxJsonArrSize(d2) == 2);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
219 CxJsonValue *d3 = cxJsonArrGet(d2, 1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
220 CX_TEST_ASSERT(cxJsonIsObject(d3));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
221 CxJsonValue *d4 = cxJsonObjGet(d3, "foo");
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
222 CX_TEST_ASSERT(cxJsonIsArray(d4));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
223 CX_TEST_ASSERT(cxJsonArrSize(d4) == 1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
224 CxJsonValue *d5 = cxJsonArrGet(d4, 0);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
225 CX_TEST_ASSERT(cxJsonIsArray(d5));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
226 CX_TEST_ASSERT(cxJsonArrSize(d5) == 1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
227 CxJsonValue *d6 = cxJsonArrGet(d5, 0);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
228 CX_TEST_ASSERT(cxJsonIsObject(d6));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
229 CxJsonValue *d7 = cxJsonObjGet(d6, "bar");
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
230 CX_TEST_ASSERT(cxJsonIsArray(d7));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
231 CX_TEST_ASSERT(cxJsonArrSize(d7) == 3);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
232 CxJsonValue *d8 = cxJsonArrGet(d7, 2);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
233 CX_TEST_ASSERT(cxJsonIsArray(d8));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
234 CX_TEST_ASSERT(cxJsonArrSize(d8) == 2);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
235 CxJsonValue *d9a = cxJsonArrGet(d8, 0);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
236 CX_TEST_ASSERT(cxJsonIsNull(d9a));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
237 CxJsonValue *d9b = cxJsonArrGet(d8, 1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
238 CX_TEST_ASSERT(cxJsonIsObject(d9b));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
239 CxJsonValue *d10 = cxJsonObjGet(d9b, "key");
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
240 CX_TEST_ASSERT(cxJsonIsInteger(d10));
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
241 CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
242
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
243 CX_TEST_ASSERT(json.states != json.states_internal);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
244 CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal));
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
245
978
1da52af18ac3 fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 977
diff changeset
246 cxJsonValueFree(d1);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
247 cxJsonDestroy(&json);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
248 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
249 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
250
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
251 CX_TEST(test_json_number) {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
252 CxJson json;
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
253 cxJsonInit(NULL, &json);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
254 CX_TEST_DO {
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
255 // TODO: find a better way to terminate values that are not arrays/objects
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
256 CxJsonValue *v;
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
257 int result;
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
258 cxJsonFill(&json, "3.1415 ");
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
259 result = cxJsonNext(&json, &v);
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
260 CX_TEST_ASSERT(result == 1);
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
261 CX_TEST_ASSERT(cxJsonIsNumber(v));
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
262 CX_TEST_ASSERT(cxJsonAsDouble(v) == 3.1415);
978
1da52af18ac3 fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 977
diff changeset
263 cxJsonValueFree(v);
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
264 cxJsonFill(&json, "-47.11e2 ");
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
265 result = cxJsonNext(&json, &v);
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
266 CX_TEST_ASSERT(result == 1);
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
267 CX_TEST_ASSERT(cxJsonIsNumber(v));
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
268 CX_TEST_ASSERT(cxJsonAsDouble(v) == -4711.0);
978
1da52af18ac3 fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 977
diff changeset
269 cxJsonValueFree(v);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
270 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
271 cxJsonDestroy(&json);
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
272 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
273
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
274 CX_TEST(test_json_multiple_values) {
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
275 CxJson json;
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
276 cxJsonInit(NULL, &json);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
277 CX_TEST_DO {
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
278 CxJsonValue *v;
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
279 int result;
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
280
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
281 // read number
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
282 cxJsonFill(&json, "10\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
283 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
284 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
285 CX_TEST_ASSERT(cxJsonIsNumber(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
286 CX_TEST_ASSERT(cxJsonAsInteger(v) == 10);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
287 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
288 // read remaining '\n'
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
289 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
290 CX_TEST_ASSERT(result == 0);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
291 // read string
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
292 cxJsonFill(&json, "\"hello world\"\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
293 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
294 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
295 CX_TEST_ASSERT(cxJsonIsString(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
296 CX_TEST_ASSERT(!cx_strcmp(cxJsonAsCxString(v), CX_STR("hello world")));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
297 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
298 // don't process the remaining newline this time
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
299 // read obj
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
300 cxJsonFill(&json, "{ \"value\": \"test\" }\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
301 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
302 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
303 CX_TEST_ASSERT(cxJsonIsObject(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
304 CxJsonValue *value = cxJsonObjGet(v, "value");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
305 CX_TEST_ASSERT(cxJsonAsString(value));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
306 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
307 // read array
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
308 cxJsonFill(&json, "[ 0, 1, 2, 3, 4, 5 ]\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
309 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
310 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
311 CX_TEST_ASSERT(cxJsonIsArray(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
312 CxJsonValue *a0 = cxJsonArrGet(v, 0);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
313 CxJsonValue *a3 = cxJsonArrGet(v, 3);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
314 CX_TEST_ASSERT(cxJsonIsNumber(a0));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
315 CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
316 CX_TEST_ASSERT(cxJsonIsNumber(a3));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
317 CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
318 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
319 // read literal
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
320 cxJsonFill(&json, "true\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
321 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
322 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
323 CX_TEST_ASSERT(cxJsonIsLiteral(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
324 CX_TEST_ASSERT(cxJsonAsBool(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
325 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
326 // read null
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
327 cxJsonFill(&json, "null\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
328 result = cxJsonNext(&json, &v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
329 CX_TEST_ASSERT(result == 1);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
330 CX_TEST_ASSERT(cxJsonIsNull(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
331 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
332 }
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
333 cxJsonDestroy(&json);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
334 }
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
335
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
336 CX_TEST(test_json_allocator) {
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
337 CxMempool *mp = cxMempoolCreate(64, NULL);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
338 CxJson json;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
339 cxJsonInit(mp->allocator, &json);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
340
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
341 cxstring text = cx_str(
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
342 "{\n"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
343 "\t\"message\":\"success\",\n"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
344 "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
345 "}"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
346 );
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
347
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
348 CX_TEST_DO {
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
349 int result;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
350
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
351 CxJson json;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
352 cxJsonInit(mp->allocator, &json);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
353 cxJsonFill(&json, text);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
354
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
355 CxJsonValue *obj;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
356 result = cxJsonNext(&json, &obj);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
357 CX_TEST_ASSERT(result == 1);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
358 CX_TEST_ASSERT(obj->allocator == mp->allocator);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
359
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
360 // this recursively frees everything
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
361 cxJsonValueFree(obj);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
362 cxJsonDestroy(&json);
997
d14f3d5f47d1 fix missing cxMempoolFree in json allocator test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 996
diff changeset
363 cxMempoolFree(mp);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
364 }
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
365 }
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
366
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 CxTestSuite *cx_test_suite_json(void) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 CxTestSuite *suite = cx_test_suite_new("json");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
370 cx_test_register(suite, test_json_init_default);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 cx_test_register(suite, test_json_simple_object);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
372 cx_test_register(suite, test_json_object_incomplete_token);
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
373 cx_test_register(suite, test_json_object_error);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
374 cx_test_register(suite, test_json_large_nesting_depth);
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
375 cx_test_register(suite, test_json_number);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
376 cx_test_register(suite, test_json_multiple_values);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
377 cx_test_register(suite, test_json_allocator);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
378
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
379 return suite;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
381

mercurial