Directory: | ./ |
---|---|
File: | test/load_strings.c |
Date: | 2021-09-04 00:13:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 91 | 91 | 100.0% |
Branches: | 21 | 42 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************/ /** | ||
2 | |||
3 | @file load_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 | 6 | void setUp(void) | |
21 | { | ||
22 | // set stuff up here | ||
23 | 6 | } | |
24 | |||
25 | 6 | void tearDown(void) | |
26 | { | ||
27 | // clean stuff up here | ||
28 | 6 | } | |
29 | |||
30 | 1 | static void test_normal_string(void) | |
31 | { | ||
32 | 1 | char input[] = "\"hello\""; | |
33 | 1 | char string[] = "hello"; | |
34 | char buffer[6]; | ||
35 | struct json_token tokens[1]; | ||
36 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
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 | json_string_load(input, tokens, 0, buffer); | |
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(0 == strcmp(buffer, string)); |
45 | 1 | } | |
46 | |||
47 | 1 | static void test_escape_quote(void) | |
48 | { | ||
49 | 1 | char input[] = "\"he\\\"llo\""; | |
50 | 1 | char string[] = "he\"llo"; | |
51 | char buffer[7]; | ||
52 | struct json_token tokens[1]; | ||
53 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 8); |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].length == 6); |
60 | 1 | json_string_load(input, tokens, 0, buffer); | |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(0 == strcmp(buffer, string)); |
62 | 1 | } | |
63 | |||
64 | 1 | static void test_escape_backslash(void) | |
65 | { | ||
66 | 1 | char input[] = "\"he\\\\llo\""; | |
67 | 1 | char string[] = "he\\llo"; | |
68 | char buffer[7]; | ||
69 | struct json_token tokens[1]; | ||
70 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.error == JSONERR_NO_ERROR); |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.tokenidx == 1); |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(p.textidx == sizeof(input) / sizeof(char) - 1); |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].start == 0); |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].end == 8); |
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(tokens[0].length == 6); |
77 | 1 | json_string_load(input, tokens, 0, buffer); | |
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | TEST_ASSERT(0 == strcmp(buffer, string)); |
79 | 1 | } | |
80 | |||
81 | 1 | static void test_unicode_escape(void) | |
82 | { | ||
83 | 1 | char input[] = "\"he\\u006Clo\""; | |
84 | 1 | char expected[] = "hello"; | |
85 | char buffer[6]; | ||
86 | struct json_token tokens[1]; | ||
87 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
88 | 1 | TEST_ASSERT_EQUAL_INT(JSONERR_NO_ERROR, p.error); | |
89 | 1 | TEST_ASSERT_EQUAL_INT(1, p.tokenidx); | |
90 | 1 | TEST_ASSERT_EQUAL_INT(sizeof(input) - 1, p.textidx); | |
91 | 1 | TEST_ASSERT_EQUAL_INT(0, tokens[0].start); | |
92 | 1 | TEST_ASSERT_EQUAL_INT(11, tokens[0].end); | |
93 | 1 | TEST_ASSERT_EQUAL_INT(5, tokens[0].length); | |
94 | 1 | json_string_load(input, tokens, 0, buffer); | |
95 | 1 | TEST_ASSERT_EQUAL_STRING(expected, buffer); | |
96 | 1 | } | |
97 | |||
98 | 1 | static void test_surrogate_pair(void) | |
99 | { | ||
100 | 1 | char input[] = "\"\\u00a2\\u0939\\u20ac\\uD83D\\uDCA9\""; | |
101 | 1 | char expected[] = "¢ह€💩"; | |
102 | char buffer[13]; | ||
103 | struct json_token tokens[1]; | ||
104 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
105 | 1 | TEST_ASSERT_EQUAL_INT(JSONERR_NO_ERROR, p.error); | |
106 | 1 | TEST_ASSERT_EQUAL_INT(1, p.tokenidx); | |
107 | 1 | TEST_ASSERT_EQUAL_INT(sizeof(input) - 1, p.textidx); | |
108 | 1 | TEST_ASSERT_EQUAL_INT(0, tokens[0].start); | |
109 | 1 | TEST_ASSERT_EQUAL_INT(31, tokens[0].end); | |
110 | 1 | TEST_ASSERT_EQUAL_INT(12, tokens[0].length); | |
111 | 1 | json_string_load(input, tokens, 0, buffer); | |
112 | 1 | TEST_ASSERT_EQUAL_STRING(expected, buffer); | |
113 | 1 | } | |
114 | |||
115 | 1 | static void test_unicode_undisturbed(void) | |
116 | { | ||
117 | 1 | char input[] = "\"¢ह€💩\""; | |
118 | 1 | char expected[] = "¢ह€💩"; | |
119 | char buffer[13]; | ||
120 | struct json_token tokens[1]; | ||
121 | 1 | struct json_parser p = json_parse(input, tokens, 1); | |
122 | 1 | TEST_ASSERT_EQUAL_INT(JSONERR_NO_ERROR, p.error); | |
123 | 1 | TEST_ASSERT_EQUAL_INT(1, p.tokenidx); | |
124 | 1 | TEST_ASSERT_EQUAL_INT(sizeof(input) - 1, p.textidx); | |
125 | 1 | TEST_ASSERT_EQUAL_INT(0, tokens[0].start); | |
126 | 1 | TEST_ASSERT_EQUAL_INT(13, tokens[0].end); | |
127 | 1 | TEST_ASSERT_EQUAL_INT(12, tokens[0].length); | |
128 | 1 | json_string_load(input, tokens, 0, buffer); | |
129 | 1 | TEST_ASSERT_EQUAL_STRING(expected, buffer); | |
130 | 1 | } | |
131 | |||
132 | 1 | int main(void) | |
133 | { | ||
134 | 1 | UNITY_BEGIN(); | |
135 | 1 | RUN_TEST(test_normal_string); | |
136 | 1 | RUN_TEST(test_escape_quote); | |
137 | 1 | RUN_TEST(test_escape_backslash); | |
138 | 1 | RUN_TEST(test_unicode_escape); | |
139 | 1 | RUN_TEST(test_surrogate_pair); | |
140 | 1 | RUN_TEST(test_unicode_undisturbed); | |
141 | 1 | return UNITY_END(); | |
142 | } | ||
143 |