tests/test_json.c

Wed, 18 Dec 2024 15:36:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 18 Dec 2024 15:36:45 +0100
changeset 1025
439407fc29e1
parent 1015
a0922b925d2a
child 1032
aaad28e23dac
permissions
-rw-r--r--

add CHANGELOG entry for issue #531

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
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
29 #include "util_allocator.h"
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "cx/test.h"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include "cx/json.h"
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
33 #include "cx/mempool.h"
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
35 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
36 CxJson json;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
37 CX_TEST_DO {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
38 cxJsonInit(&json, NULL);
946
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.states == json.states_internal);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
40 CX_TEST_ASSERT(json.states_size == 1);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
41 CX_TEST_ASSERT(json.states_capacity >= 8);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
42 CX_TEST_ASSERT(json.vbuf == json.vbuf_internal);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
43 CX_TEST_ASSERT(json.vbuf_size == 0);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
44 CX_TEST_ASSERT(json.vbuf_capacity >= 8);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
45 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
46 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
47
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 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
49 cxstring text = cx_str(
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 "{\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 "\t\"message\":\"success\",\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 "\t\"position\":{\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 "\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
54 "\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
55 "\t},\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 "\t\"timestamp\":1729348561,\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 "\t\"alive\":true\n"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 "}"
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 );
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 CX_TEST_DO {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
62 CxJsonStatus result;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 CxJson json;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
65 cxJsonInit(&json, NULL);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
66 cxJsonFill(&json, text);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 // parse the big fat object
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 CxJsonValue *obj;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 result = cxJsonNext(&json, &obj);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
71 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
937
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 // check the contents
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_TEST_ASSERT(cxJsonIsObject(obj));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 CxJsonValue *message = cxJsonObjGet(obj, "message");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CX_TEST_ASSERT(cxJsonIsString(message));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 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
79 cxJsonAsCxString(message),
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 cx_str("success"))
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 );
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 CxJsonValue *position = cxJsonObjGet(obj, "position");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 CX_TEST_ASSERT(cxJsonIsObject(position));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 CxJsonValue *longitude = cxJsonObjGet(position, "longitude");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 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
87 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
88 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
89 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
90 CxJsonValue *latitude = cxJsonObjGet(position, "latitude");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 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
92 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
93 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
94 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
95
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 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
98 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
99 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
100 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
101
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 CxJsonValue *alive = cxJsonObjGet(obj, "alive");
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 CX_TEST_ASSERT(cxJsonIsBool(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 CX_TEST_ASSERT(cxJsonIsTrue(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 CX_TEST_ASSERT(!cxJsonIsFalse(alive));
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 CX_TEST_ASSERT(cxJsonAsBool(alive));
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 // this recursively frees everything else
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 cxJsonValueFree(obj);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 // 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
112 result = cxJsonNext(&json, &obj);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
113 CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
937
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 cxJsonDestroy(&json);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
119 CX_TEST(test_json_escaped_strings) {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
120 cxstring text = cx_str(
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
121 "{\n"
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
122 "\t\"object\":\"{\\n\\t\\\"object\\\":null\\n}\"}\"\n"
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
123 "}"
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
124 );
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
125
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
126 CxJson json;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
127 cxJsonInit(&json, NULL);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
128 CX_TEST_DO {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
129 cxJsonFill(&json, text);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
130 CxJsonValue *obj;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
131 CxJsonStatus result = cxJsonNext(&json, &obj);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
132 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
133 CX_TEST_ASSERT(cxJsonIsObject(obj));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
134 CxJsonValue *object = cxJsonObjGet(obj, "object");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
135 CX_TEST_ASSERT(cxJsonIsString(object));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
136 CX_TEST_ASSERT(0 == cx_strcmp(
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
137 cxJsonAsCxString(object),
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
138 CX_STR("{\n\t\"object\":null\n}"))
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
139 );
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
140 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
141 cxJsonDestroy(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
142 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
143
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
144 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
145 cxstring text = cx_str(
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
146 "{\"message\":\"success\" , \"__timestamp\":1729348561}");
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
147 cxstring parts[16];
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
148 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
149 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
150 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
151 }
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 CX_TEST_DO {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
154 CxJsonStatus result;
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
155
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
156 CxJson json;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
157 cxJsonInit(&json, NULL);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
158 CxJsonValue *obj;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
159
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
160 size_t part = 0;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
161 while(part < nparts - 1) {
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
162 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
163 part++;
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
164 result = cxJsonNext(&json, &obj);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
165 CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
166 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
167 cxJsonFill(&json, parts[nparts - 1]);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
168 result = cxJsonNext(&json, &obj);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
169 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
170 CX_TEST_ASSERT(cxJsonIsObject(obj));
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
171
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
172 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
173 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
174 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
175 cxJsonAsCxString(message),
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
176 cx_str("success"))
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
177 );
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
178 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
179 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
180 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
181 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
182
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
183 // 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
184 cxJsonValueFree(obj);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
185
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
186 // now there is everything read
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
187 result = cxJsonNext(&json, &obj);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
188 CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
189
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
190 cxJsonDestroy(&json);
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
191 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
192 }
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
193
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
194 CX_TEST(test_json_token_wrongly_completed) {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
195 cxstring text = cx_str("{\"number\": 47110815!}");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
196 cxstring part1 = cx_strsubsl(text, 0, 16);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
197 cxstring part2 = cx_strsubs(text, 16);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
198
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
199 CxJson json;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
200 cxJsonInit(&json, NULL);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
201 CX_TEST_DO {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
202 CxJsonStatus result;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
203 CxJsonValue *obj;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
204
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
205 cxJsonFill(&json, part1);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
206 result = cxJsonNext(&json, &obj);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
207 CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
208 cxJsonFill(&json, part2);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
209 result = cxJsonNext(&json, &obj);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
210 CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_NUMBER);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
211 CX_TEST_ASSERT(obj->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
212 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
213 cxJsonDestroy(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
214 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
215
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
216 CX_TEST(test_json_subsequent_fill) {
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
217 cxstring text = cx_str(
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
218 "{\"message\":\"success\" , \"__timestamp\":1729348561}");
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
219
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
220 cxstring part1 = cx_strsubsl(text, 0, 25);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
221 cxstring part2 = cx_strsubs(text, 25);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
222
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
223 CX_TEST_DO {
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
224 CxJson json;
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
225 cxJsonInit(&json, NULL);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
226 CxJsonValue *obj;
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
227
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
228 cxJsonFill(&json, part1);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
229 cxJsonFill(&json, part2);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
230 CxJsonStatus result = cxJsonNext(&json, &obj);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
231 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
232 CX_TEST_ASSERT(cxJsonIsObject(obj));
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
233
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
234 CxJsonValue *message = cxJsonObjGet(obj, "message");
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
235 CX_TEST_ASSERT(cxJsonIsString(message));
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
236 CX_TEST_ASSERT(0 == cx_strcmp(
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
237 cxJsonAsCxString(message),
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
238 cx_str("success"))
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
239 );
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
240 CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp");
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
241 CX_TEST_ASSERT(message->type == CX_JSON_STRING);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
242 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
243 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
244
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
245 cxJsonValueFree(obj);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
246 result = cxJsonNext(&json, &obj);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
247 CX_TEST_ASSERT(result == CX_JSON_NO_DATA);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
248
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
249 cxJsonDestroy(&json);
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
250 }
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
251 }
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
252
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
253 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
254 cxstring text0 = cx_str(
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
255 "{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
256 "\t\"message\":\"success\",\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
257 "\t\"data\":{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
258 "\t\t\"obj\":{\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
259 "\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
260 "\t\t\"}\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
261 "\t},\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
262 "\t\"timestamp\":1729348561,\n"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
263 "}"
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
264 );
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
265 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
266 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
267 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
268 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
269
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
270 cxstring tests[] = { text0, text1, text2, text3, text4 };
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
271 CxJsonStatus errors[] = {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
272 CX_JSON_FORMAT_ERROR_NUMBER,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
273 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
274 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
275 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
276 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
277 };
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
278
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
279 CX_TEST_DO {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
280 CxJsonStatus result;
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
281 CxJson json;
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
282 CxJsonValue *obj = NULL;
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
283
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
284 for(int i=0;i<5;i++) {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
285 cxJsonInit(&json, NULL);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
286 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
287 result = cxJsonNext(&json, &obj);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
288
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
289 CX_TEST_ASSERT(result == errors[i]);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
290 CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING);
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
291 cxJsonDestroy(&json);
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
292 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
293 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
294 }
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
295
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
296 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
297 CxJson json;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
298 CxJsonValue *d1;
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
299 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
300 CX_TEST_DO {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
301 cxJsonInit(&json, NULL);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
302 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
303 cxJsonNext(&json, &d1);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
304
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328 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
329 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
330 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
331 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
332 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
333
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
334 CX_TEST_ASSERT(json.states != json.states_internal);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
335 CX_TEST_ASSERT(json.states_capacity > cx_nmemb(json.states_internal));
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
336
978
1da52af18ac3 fix memory leaks in json tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 977
diff changeset
337 cxJsonValueFree(d1);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
338 cxJsonDestroy(&json);
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
339 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
340 }
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
341
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
342 CX_TEST(test_json_number) {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
343 CxJson json;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
344 cxJsonInit(&json, NULL);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
345 CX_TEST_DO {
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
346 CxJsonValue *v;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
347 CxJsonStatus result;
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
348
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
349 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
350 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
351 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
352 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
353 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
354 cxJsonValueFree(v);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
355
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
356 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
357 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
358 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
975
96bf02371aef adds test case for numbers - currently fails due to issue #482
Mike Becker <universe@uap-core.de>
parents: 973
diff changeset
359 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
360 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
361 cxJsonValueFree(v);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
362
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
363 cxJsonFill(&json, "0.815e-3 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
364 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
365 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
366 CX_TEST_ASSERT(cxJsonIsNumber(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
367 CX_TEST_ASSERT(cxJsonAsDouble(v) == 0.000815);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
368
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
369 cxJsonFill(&json, "1.23E4 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
370 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
371 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
372 CX_TEST_ASSERT(cxJsonIsNumber(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
373 CX_TEST_ASSERT(cxJsonAsInteger(v) == 12300);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
374 CX_TEST_ASSERT(cxJsonAsDouble(v) == 12300.0);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
375
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
376 cxJsonValueFree(v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
377 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
378 cxJsonDestroy(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
379 }
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
380
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
381 CX_TEST(test_json_number_format_errors) {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
382 CxJson json;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
383 cxJsonInit(&json, NULL);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
384 CX_TEST_DO {
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
385 CxJsonValue *v;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
386 CxJsonStatus result;
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
387
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
388 cxJsonFill(&json, "+3.1415 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
389 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
390 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
391 "leading plus is not RFC-8259 compliant");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
392 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
393 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
394
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
395 #if 0 // TODO: discuss if it is intended that this produces -47 as valid value
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
396 cxJsonFill(&json, "-47,11e2 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
397 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
398 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
399 "decimal separator cannot be a comma");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
400 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
401 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
402 #endif
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
403
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
404 cxJsonFill(&json, "0.815e-3.0 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
405 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
406 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
407 "exponent must be an integer");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
408 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
409 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
410
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
411 cxJsonFill(&json, "3.14e ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
412 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
413 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
414 "exponent cannot be empty");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
415 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
416 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
417
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
418 cxJsonFill(&json, "3.14e~7 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
419 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
420 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
421 "exponent cannot start with bullshit");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
422 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
423 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
424
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
425 cxJsonFill(&json, "1.23e4f ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
426 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
427 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
428 "non-digits in exponent");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
429 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
430 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
431
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
432 cxJsonFill(&json, "1.23f ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
433 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
434 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
435 "non-digits in value");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
436 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
437 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
438
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
439 cxJsonFill(&json, "1.23.45 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
440 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
441 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
442 "multiple decimal separators");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
443 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
444 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
445
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
446 cxJsonFill(&json, "184467440737095516150123456789 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
447 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
448 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
449 "30 digit int does not fit into 64-bit int");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
450 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
451 cxJsonReset(&json);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
452
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
453 cxJsonFill(&json, "18446744073709551615.0123456789 ");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
454 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
455 CX_TEST_ASSERTM(result == CX_JSON_FORMAT_ERROR_NUMBER,
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
456 "numbers with more than 30 characters are unsupported in general");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
457 CX_TEST_ASSERT(v->type == CX_JSON_NOTHING);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
458 cxJsonReset(&json);
973
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
459 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
460 cxJsonDestroy(&json);
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
461 }
05910a8994f7 add UCX string support to cxJsonFill()
Mike Becker <universe@uap-core.de>
parents: 966
diff changeset
462
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
463 CX_TEST(test_json_multiple_values) {
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
464 CxJson json;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
465 cxJsonInit(&json, NULL);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
466 CX_TEST_DO {
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
467 CxJsonValue *v;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
468 CxJsonStatus result;
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
469
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
470 // read number
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
471 cxJsonFill(&json, "10\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
472 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
473 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
474 CX_TEST_ASSERT(cxJsonIsNumber(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
475 CX_TEST_ASSERT(cxJsonAsInteger(v) == 10);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
476 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
477 // read remaining '\n'
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
478 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
479 CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
480 // read string
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
481 cxJsonFill(&json, "\"hello world\"\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
482 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
483 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
484 CX_TEST_ASSERT(cxJsonIsString(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
485 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
486 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
487 // 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
488 // read obj
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
489 cxJsonFill(&json, "{ \"value\": \"test\" }\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
490 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
491 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
492 CX_TEST_ASSERT(cxJsonIsObject(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
493 CxJsonValue *value = cxJsonObjGet(v, "value");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
494 CX_TEST_ASSERT(cxJsonAsString(value));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
495 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
496 // read array
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
497 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
498 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
499 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
500 CX_TEST_ASSERT(cxJsonIsArray(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
501 CxJsonValue *a0 = cxJsonArrGet(v, 0);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
502 CxJsonValue *a3 = cxJsonArrGet(v, 3);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
503 CX_TEST_ASSERT(cxJsonIsNumber(a0));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
504 CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
505 CX_TEST_ASSERT(cxJsonIsNumber(a3));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
506 CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
507 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
508 // read literal
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
509 cxJsonFill(&json, "true\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
510 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
511 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
512 CX_TEST_ASSERT(cxJsonIsLiteral(v));
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
513 CX_TEST_ASSERT(cxJsonIsBool(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
514 CX_TEST_ASSERT(cxJsonIsTrue(v));
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
515 CX_TEST_ASSERT(cxJsonAsBool(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
516 cxJsonValueFree(v);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
517 cxJsonFill(&json, "false\n");
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
518 result = cxJsonNext(&json, &v);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
519 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
520 CX_TEST_ASSERT(cxJsonIsLiteral(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
521 CX_TEST_ASSERT(cxJsonIsBool(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
522 CX_TEST_ASSERT(cxJsonIsFalse(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
523 CX_TEST_ASSERT(!cxJsonAsBool(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
524 cxJsonValueFree(v);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
525 cxJsonFill(&json, "null\n");
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
526 result = cxJsonNext(&json, &v);
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
527 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
528 CX_TEST_ASSERT(cxJsonIsLiteral(v));
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
529 CX_TEST_ASSERT(!cxJsonIsBool(v));
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
530 CX_TEST_ASSERT(cxJsonIsNull(v));
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
531 cxJsonValueFree(v);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
532 }
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
533 cxJsonDestroy(&json);
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
534 }
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
535
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
536 CX_TEST(test_json_allocator) {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
537 CxTestingAllocator talloc;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
538 cx_testing_allocator_init(&talloc);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
539 CxAllocator *allocator = &talloc.base;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
540
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
541 cxstring text = cx_str(
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
542 "{\n"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
543 "\t\"message\":\"success\",\n"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
544 "\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
545 "}"
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
546 );
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
547
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
548 CX_TEST_DO {
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
549 CxJson json;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
550 cxJsonInit(&json, allocator);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
551 cxJsonFill(&json, text);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
552
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
553 CxJsonValue *obj;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
554 CxJsonStatus result = cxJsonNext(&json, &obj);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
555 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
556 CX_TEST_ASSERT(obj->allocator == allocator);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
557
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
558 // this recursively frees everything
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
559 cxJsonValueFree(obj);
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
560 cxJsonDestroy(&json);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
561
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
562 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
563 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
564 cx_testing_allocator_destroy(&talloc);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
565 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
566
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
567 CX_TEST(test_json_allocator_parse_error) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
568 CxTestingAllocator talloc;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
569 cx_testing_allocator_init(&talloc);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
570 CxAllocator *allocator = &talloc.base;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
571
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
572 cxstring text = cx_str(
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
573 "{\n"
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
574 "\t\"message\":\"success\"\n" // <-- missing comma
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
575 "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n"
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
576 "}"
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
577 );
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
578
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
579 CX_TEST_DO {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
580 CxJson json;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
581 cxJsonInit(&json, allocator);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
582 cxJsonFill(&json, text);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
583
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
584 CxJsonValue *obj = NULL;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
585 CxJsonStatus result = cxJsonNext(&json, &obj);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
586 CX_TEST_ASSERT(result == CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
587 CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
588
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
589 // clean-up any left-over memory
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
590 cxJsonDestroy(&json);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
591
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
592 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
593 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
594 cx_testing_allocator_destroy(&talloc);
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
595 }
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 978
diff changeset
596
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
597 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
598 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
599
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
600 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
601 cx_test_register(suite, test_json_simple_object);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
602 cx_test_register(suite, test_json_escaped_strings);
939
0bb7258366a0 add test for parsing json with multiple incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 937
diff changeset
603 cx_test_register(suite, test_json_object_incomplete_token);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
604 cx_test_register(suite, test_json_token_wrongly_completed);
941
9077724b75a0 fix incomplete json object not resulting in syntax error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 939
diff changeset
605 cx_test_register(suite, test_json_object_error);
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
606 cx_test_register(suite, test_json_subsequent_fill);
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 941
diff changeset
607 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
608 cx_test_register(suite, test_json_number);
1015
a0922b925d2a increase test coverage
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
609 cx_test_register(suite, test_json_number_format_errors);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
610 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
611 cx_test_register(suite, test_json_allocator);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 997
diff changeset
612 cx_test_register(suite, test_json_allocator_parse_error);
977
a941b01b684f add test for reading multiple json values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 975
diff changeset
613
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
614 return suite;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
615 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
616

mercurial