GCC Code Coverage Report


Directory: ./
File: test/compare_strings.c
Date: 2021-09-04 00:13:15
Exec Total Coverage
Lines: 207 207 100.0%
Branches: 96 192 50.0%

Line Branch Exec Source
1 /***************************************************************************/ /**
2
3 @file compare_strings.c
4
5 @author Stephen Brennan
6
7 @date Created Wednesday, 9 December 2015
8
9 @brief Tests for comparing strings.
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 17 void setUp(void)
21 {
22 // set stuff up here
23 17 }
24
25 17 void tearDown(void)
26 {
27 // clean stuff up here
28 17 }
29
30 1 static void test_normal_string(void)
31 {
32 1 char input[] = "\"hello\"";
33 1 char string[] = "hello";
34 struct json_token tokens[1];
35 1 struct json_parser p = json_parse(input, tokens, 1);
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 == 1);
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 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
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 == 6);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 5);
43 1 }
44
45 1 static void test_normal_nomatch(void)
46 {
47 1 char input[] = "\"hello\"";
48 1 char string[] = "hellO";
49 struct json_token tokens[1];
50 1 struct json_parser p = json_parse(input, tokens, 1);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(!json_string_match(input, tokens, 0, string));
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 6);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 5);
58 1 }
59
60 1 static void test_normal_too_long(void)
61 {
62 1 char input[] = "\"hello\"";
63 1 char string[] = "hello there";
64 struct json_token tokens[1];
65 1 struct json_parser p = json_parse(input, tokens, 1);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(!json_string_match(input, tokens, 0, string));
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 6);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 5);
73 1 }
74
75 1 static void test_normal_too_short(void)
76 {
77 1 char input[] = "\"hello\"";
78 1 char string[] = "he";
79 struct json_token tokens[1];
80 1 struct json_parser p = json_parse(input, tokens, 1);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(!json_string_match(input, tokens, 0, string));
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 6);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 5);
88 1 }
89
90 1 static void test_escape_quote(void)
91 {
92 1 char input[] = "\"he\\\"llo\"";
93 1 char string[] = "he\"llo";
94 struct json_token tokens[1];
95 1 struct json_parser p = json_parse(input, tokens, 1);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
103 1 }
104
105 1 static void test_escape_backslash(void)
106 {
107 1 char input[] = "\"he\\\\llo\"";
108 1 char string[] = "he\\llo";
109 struct json_token tokens[1];
110 1 struct json_parser p = json_parse(input, tokens, 1);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
118 1 }
119
120 1 static void test_escape_slash(void)
121 {
122 1 char input[] = "\"he\\/llo\"";
123 1 char string[] = "he/llo";
124 struct json_token tokens[1];
125 1 struct json_parser p = json_parse(input, tokens, 1);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
133 1 }
134
135 1 static void test_escape_backspace(void)
136 {
137 1 char input[] = "\"he\\bllo\"";
138 1 char string[] = "he\bllo";
139 struct json_token tokens[1];
140 1 struct json_parser p = json_parse(input, tokens, 1);
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
148 1 }
149
150 1 static void test_escape_formfeed(void)
151 {
152 1 char input[] = "\"he\\fllo\"";
153 1 char string[] = "he\fllo";
154 struct json_token tokens[1];
155 1 struct json_parser p = json_parse(input, tokens, 1);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
163 1 }
164
165 1 static void test_escape_newline(void)
166 {
167 1 char input[] = "\"he\\nllo\"";
168 1 char string[] = "he\nllo";
169 struct json_token tokens[1];
170 1 struct json_parser p = json_parse(input, tokens, 1);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
178 1 }
179
180 1 static void test_escape_carriage_return(void)
181 {
182 1 char input[] = "\"he\\rllo\"";
183 1 char string[] = "he\rllo";
184 struct json_token tokens[1];
185 1 struct json_parser p = json_parse(input, tokens, 1);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
193 1 }
194
195 1 static void test_escape_tab(void)
196 {
197 1 char input[] = "\"he\\tllo\"";
198 1 char string[] = "he\tllo";
199 struct json_token tokens[1];
200 1 struct json_parser p = json_parse(input, tokens, 1);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 8);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 6);
208 1 }
209
210 1 static void test_unicode_escape(void)
211 {
212 1 char input[] = "\"he\\u006Clo\"";
213 1 char string[] = "hello";
214 struct json_token tokens[1];
215 1 struct json_parser p = json_parse(input, tokens, 1);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_NO_ERROR);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.tokenidx == 1);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].start == 0);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].end == 11);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(tokens[0].length == 5);
223 1 }
224
225 1 static void test_surrogate_pair(void)
226 {
227 1 char input[] = "\"\\uD83D\\uDCA9\"";
228 1 char string[] = "💩"; // directly included poop :)
229 struct json_token tokens[1];
230 1 struct json_parser p = json_parse(input, tokens, 1);
231 1 TEST_ASSERT_EQUAL_INT(JSONERR_NO_ERROR, p.error);
232 1 TEST_ASSERT_EQUAL_INT(1, p.tokenidx);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1);
234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 TEST_ASSERT(json_string_match(input, tokens, 0, string));
235 1 TEST_ASSERT_EQUAL_INT(0, tokens[0].start);
236 1 TEST_ASSERT_EQUAL_INT(13, tokens[0].end);
237 1 TEST_ASSERT_EQUAL_INT(4, tokens[0].length);
238 1 }
239
240 1 static void test_incomplete_surrogate(void)
241 {
242 1 char input[] = "\"\\uD83D\"";
243 struct json_token tokens[1];
244 1 struct json_parser p = json_parse(input, tokens, 1);
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_INVALID_SURROGATE);
246 1 }
247
248 1 static void test_invalid_surrogate_char(void)
249 {
250 1 char input[] = "\"\\uD83Da\"";
251 struct json_token tokens[1];
252 1 struct json_parser p = json_parse(input, tokens, 1);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_INVALID_SURROGATE);
254 1 }
255
256 1 static void test_invalid_surrogate_pair(void)
257 {
258 1 char input[] = "\"\\uD83D\\u1234\""; // 1234 is not a valid
259 // surrogate to follow
260 struct json_token tokens[1];
261 1 struct json_parser p = json_parse(input, tokens, 1);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 TEST_ASSERT(p.error == JSONERR_INVALID_SURROGATE);
263 1 }
264
265 1 int main(void)
266 {
267 1 UNITY_BEGIN();
268 1 RUN_TEST(test_normal_string);
269 1 RUN_TEST(test_normal_nomatch);
270 1 RUN_TEST(test_normal_too_long);
271 1 RUN_TEST(test_normal_too_short);
272 1 RUN_TEST(test_escape_quote);
273 1 RUN_TEST(test_escape_backslash);
274 1 RUN_TEST(test_escape_slash);
275 1 RUN_TEST(test_escape_backspace);
276 1 RUN_TEST(test_escape_formfeed);
277 1 RUN_TEST(test_escape_newline);
278 1 RUN_TEST(test_escape_carriage_return);
279 1 RUN_TEST(test_escape_tab);
280 1 RUN_TEST(test_unicode_escape);
281 1 RUN_TEST(test_surrogate_pair);
282 1 RUN_TEST(test_incomplete_surrogate);
283 1 RUN_TEST(test_invalid_surrogate_char);
284 1 RUN_TEST(test_invalid_surrogate_pair);
285 1 return UNITY_END();
286 }
287