MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_json_tok_size

Function test_json_tok_size

common/test/run-json.c:116–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

114}
115
116static void test_json_tok_size(void)
117{
118 jsmntok_t *toks;
119 char *buf;
120 bool ok, complete;
121 jsmn_parser parser;
122
123 buf = "[\"e1\", [\"e2\", \"e3\"]]";
124 toks = toks_alloc(tmpctx);
125 jsmn_init(&parser);
126 ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
127 assert(ok);
128 assert(complete);
129 /* size only counts *direct* children */
130 assert(toks[0].size == 2);
131 assert(toks[2].size == 2);
132
133 buf = "[[\"e1\", \"e2\"], \"e3\"]";
134 toks_reset(toks);
135 jsmn_init(&parser);
136 ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
137 assert(ok);
138 assert(complete);
139 /* size only counts *direct* children */
140 assert(toks[0].size == 2);
141 assert(toks[1].size == 2);
142
143 buf = "{\"e1\" : {\"e2\": 2, \"e3\": 3}}";
144 toks_reset(toks);
145 jsmn_init(&parser);
146 ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
147 assert(ok);
148 assert(complete);
149 /* size only counts *direct* children */
150 assert(toks[0].size == 1);
151 assert(toks[2].size == 2);
152
153 buf = "{\"e1\" : {\"e2\": 2, \"e3\": 3}, \"e4\" : {\"e5\": 5, \"e6\": 6}}";
154 toks_reset(toks);
155 jsmn_init(&parser);
156 ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
157 assert(ok);
158 assert(complete);
159 /* size only counts *direct* children */
160 assert(toks[0].size == 2);
161 assert(toks[2].size == 2);
162 assert(toks[8].size == 2);
163
164 /* This should *not* parse! (used to give toks[0]->size == 3!) */
165 buf = "{ \"\" \"\" \"\" }";
166 toks_reset(toks);
167 jsmn_init(&parser);
168 ok = json_parse_input(&parser, &toks, buf, strlen(buf), &complete);
169 assert(!ok);
170
171 /* This should *not* parse! (used to give toks[0]->size == 2!) */
172 buf = "{ 'satoshi', '546' }";
173 toks = json_parse_simple(tmpctx, buf, strlen(buf));

Callers 1

mainFunction · 0.85

Calls 4

toks_allocFunction · 0.50
json_parse_inputFunction · 0.50
toks_resetFunction · 0.50
json_parse_simpleFunction · 0.50

Tested by

no test coverage detected