Directory: | ./ |
---|---|
File: | test/parse_arrays.c |
Date: | 2021-09-04 00:13:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 128 | 128 | 100.0% |
Branches: | 57 | 108 | 52.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************/ /** | ||
2 | |||
3 | @file parse_arrays.c | ||
4 | |||
5 | @author Stephen Brennan | ||
6 | |||
7 | @date Created Tuesday, 24 November 2015 | ||
8 | |||
9 | @brief Test array parsing. | ||
10 | |||
11 | @copyright Copyright (c) 2015, Stephen Brennan. Released under the | ||
12 | Revised BSD License. See LICENSE.txt for details. | ||
13 | |||
14 | *******************************************************************************/ | ||
15 | |||
16 | #include <unity.h> | ||
17 | |||
18 | #include "nosj.h" | ||
19 | |||
20 | 9 | void setUp(void) | |
21 | { | ||
22 | // set stuff up here | ||
23 | 9 | } | |
24 | |||
25 | 9 | void tearDown(void) | |
26 | { | ||
27 | // clean stuff up here | ||
28 | 9 | } | |
29 | |||
30 | 1 | static void test_empty_array(void) | |
31 | 1 | { | |
32 | 1 | char input[] = "[]"; | |
33 | 1 | size_t ntok = 1; | |
34 | 1 | struct json_token tokens[ntok]; | |
35 | 1 | struct json_parser p = json_parse(input, tokens, ntok); | |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == ntok); |
38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].type == JSON_ARRAY); |
40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 1); |
42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].length == 0); |
43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].child == 0); |
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].next == 0); |
45 | 1 | } | |
46 | |||
47 | 1 | static void test_single_element(void) | |
48 | 1 | { | |
49 | 1 | char input[] = "[1]"; | |
50 | 1 | size_t ntok = 2, i; | |
51 | 1 | struct json_token tokens[ntok]; | |
52 | 1 | struct json_token expected[] = { | |
53 | { .type = JSON_ARRAY, | ||
54 | .start = 0, | ||
55 | .end = 2, | ||
56 | .length = 1, | ||
57 | .child = 1, | ||
58 | .next = 0 }, | ||
59 | { .type = JSON_NUMBER, | ||
60 | .start = 1, | ||
61 | .end = 1, | ||
62 | .length = 0, | ||
63 | .child = 0, | ||
64 | .next = 0 }, | ||
65 | }; | ||
66 | 1 | struct json_parser p = json_parse(input, tokens, ntok); | |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == ntok); |
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
70 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ntok; i++) { |
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].type == expected[i].type); |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].start == expected[i].start); |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].end == expected[i].end); |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].child == expected[i].child); |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].next == expected[i].next); |
76 | } | ||
77 | 1 | } | |
78 | |||
79 | 1 | static void test_multiple_elements(void) | |
80 | 1 | { | |
81 | 1 | size_t ntok = 3, i; | |
82 | 1 | char input[] = "[1, 2]"; | |
83 | 1 | struct json_token tokens[ntok]; | |
84 | 1 | struct json_token expected[] = { | |
85 | { .type = JSON_ARRAY, | ||
86 | .start = 0, | ||
87 | .end = 5, | ||
88 | .length = 2, | ||
89 | .child = 1, | ||
90 | .next = 0 }, | ||
91 | { .type = JSON_NUMBER, | ||
92 | .start = 1, | ||
93 | .end = 1, | ||
94 | .length = 0, | ||
95 | .child = 0, | ||
96 | .next = 2 }, | ||
97 | { .type = JSON_NUMBER, | ||
98 | .start = 4, | ||
99 | .end = 4, | ||
100 | .length = 0, | ||
101 | .child = 0, | ||
102 | .next = 0 }, | ||
103 | }; | ||
104 | 1 | struct json_parser p = json_parse(input, tokens, ntok); | |
105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == ntok); |
107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
108 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (i = 0; i < ntok; i++) { |
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | TEST_ASSERT(tokens[i].type == expected[i].type); |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | TEST_ASSERT(tokens[i].start == expected[i].start); |
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | TEST_ASSERT(tokens[i].end == expected[i].end); |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | TEST_ASSERT(tokens[i].child == expected[i].child); |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | TEST_ASSERT(tokens[i].next == expected[i].next); |
114 | } | ||
115 | 1 | } | |
116 | |||
117 | 1 | static void test_extra_comma(void) | |
118 | 1 | { | |
119 | 1 | char input[] = "[1,]"; | |
120 | 1 | size_t ntok = 2, i; | |
121 | 1 | struct json_token tokens[ntok]; | |
122 | 1 | struct json_token expected[] = { | |
123 | { .type = JSON_ARRAY, | ||
124 | .start = 0, | ||
125 | .end = 3, | ||
126 | .length = 1, | ||
127 | .child = 1, | ||
128 | .next = 0 }, | ||
129 | { .type = JSON_NUMBER, | ||
130 | .start = 1, | ||
131 | .end = 1, | ||
132 | .length = 0, | ||
133 | .child = 0, | ||
134 | .next = 0 }, | ||
135 | }; | ||
136 | 1 | struct json_parser p = json_parse(input, tokens, ntok); | |
137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == ntok); |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
140 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ntok; i++) { |
141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].type == expected[i].type); |
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].start == expected[i].start); |
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].end == expected[i].end); |
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].child == expected[i].child); |
145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | TEST_ASSERT(tokens[i].next == expected[i].next); |
146 | } | ||
147 | 1 | } | |
148 | |||
149 | 1 | static void test_no_end(void) | |
150 | { | ||
151 | 1 | char input[] = "[1,"; | |
152 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_PREMATURE_EOF); |
154 | 1 | } | |
155 | |||
156 | 1 | static void test_error_within_list(void) | |
157 | { | ||
158 | 1 | char input[] = "[1e,"; | |
159 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_INVALID_NUMBER); |
161 | 1 | } | |
162 | |||
163 | 1 | static void test_no_comma(void) | |
164 | { | ||
165 | 1 | char input[] = "[1 2 3]"; | |
166 | 1 | struct json_parser p = json_parse(input, NULL, 0); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_EXPECTED_TOKEN); |
168 | 1 | TEST_ASSERT(p.errorarg = ','); | |
169 | 1 | } | |
170 | |||
171 | 1 | static void test_get(void) | |
172 | { | ||
173 | 1 | char input[] = "[1, null, true, \"hi\", {}]"; | |
174 | struct json_token tokens[7]; | ||
175 | 1 | struct json_parser p = json_parse(input, tokens, 7); | |
176 | size_t res; | ||
177 | |||
178 | // assertions for parsing correctly | ||
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 6); |
181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
182 | |||
183 | // assertions for the results of json_array_get() | ||
184 | 1 | res = json_array_get(input, tokens, 0, 0); | |
185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 1); |
186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[res].type == JSON_NUMBER); |
187 | |||
188 | 1 | res = json_array_get(input, tokens, 0, 1); | |
189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 2); |
190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[res].type == JSON_NULL); |
191 | |||
192 | 1 | res = json_array_get(input, tokens, 0, 2); | |
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 3); |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[res].type == JSON_TRUE); |
195 | |||
196 | 1 | res = json_array_get(input, tokens, 0, 3); | |
197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 4); |
198 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[res].type == JSON_STRING); |
199 | |||
200 | 1 | res = json_array_get(input, tokens, 0, 4); | |
201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 5); |
202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[res].type == JSON_OBJECT); |
203 | |||
204 | 1 | res = json_array_get(input, tokens, 0, 5); | |
205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(res == 0); |
206 | 1 | } | |
207 | |||
208 | 1 | static void test_get_empty(void) | |
209 | { | ||
210 | 1 | char input[] = "[]"; | |
211 | struct json_token tokens[1]; | ||
212 | 1 | json_parse(input, tokens, 1); | |
213 | |||
214 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | TEST_ASSERT(0 == json_array_get(input, tokens, 0, 0)); |
215 | 1 | } | |
216 | |||
217 | 1 | int main(void) | |
218 | { | ||
219 | 1 | UNITY_BEGIN(); | |
220 | |||
221 | 1 | RUN_TEST(test_empty_array); | |
222 | 1 | RUN_TEST(test_single_element); | |
223 | 1 | RUN_TEST(test_multiple_elements); | |
224 | 1 | RUN_TEST(test_extra_comma); | |
225 | 1 | RUN_TEST(test_no_end); | |
226 | 1 | RUN_TEST(test_error_within_list); | |
227 | 1 | RUN_TEST(test_no_comma); | |
228 | 1 | RUN_TEST(test_get); | |
229 | 1 | RUN_TEST(test_get_empty); | |
230 | |||
231 | 1 | return UNITY_END(); | |
232 | } | ||
233 |