Skip to content

Commit 5f42c4c

Browse files
committed
More coverage
1 parent deaed8d commit 5f42c4c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

str.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
463463
if (toklen) *toklen = 0;
464464
if (path[0] != '$') return -1;
465465

466-
#define MG_CHECKRET(x) \
466+
#define MG_CHECKRET() \
467467
do { \
468468
if (depth == ed && path[pos] == '\0' && ci == ei) { \
469469
if (toklen) *toklen = i - j + 1; \
@@ -473,12 +473,12 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
473473

474474
// In the ascii table, the distance between `[` and `]` is 2.
475475
// Ditto for `{` and `}`. Hence +2 in the code below.
476-
#define MG_EOO(x) \
476+
#define MG_EOO() \
477477
do { \
478478
if (depth == ed && ci != ei) return -2; \
479479
if (c != nesting[depth - 1] + 2) return -1; \
480480
depth--; \
481-
MG_CHECKRET(x); \
481+
MG_CHECKRET(); \
482482
} while (0)
483483

484484
for (i = 0; i < len; i++) {
@@ -510,7 +510,7 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
510510
nesting[depth++] = c;
511511
break;
512512
} else if (c == ']' && depth > 0) { // Empty array
513-
MG_EOO(']');
513+
MG_EOO();
514514
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
515515
i += 3;
516516
} else if (c == 'n' && i + 3 < len && memcmp(&s[i], "null", 4) == 0) {
@@ -528,7 +528,7 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
528528
} else {
529529
return -1;
530530
}
531-
MG_CHECKRET('V');
531+
MG_CHECKRET();
532532
if (depth == ed && ei >= 0) ci++;
533533
expecting = S_COMMA_OR_EOO;
534534
break;
@@ -554,8 +554,9 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
554554
i += n + 1;
555555
expecting = S_COLON;
556556
} else if (c == '}') { // Empty object
557-
MG_EOO('}');
557+
MG_EOO();
558558
expecting = S_COMMA_OR_EOO;
559+
if (depth == ed && ei >= 0) ci++;
559560
} else {
560561
return -1;
561562
}
@@ -575,7 +576,7 @@ XAPI int json_get(const char *s, int len, const char *path, int *toklen) {
575576
} else if (c == ',') {
576577
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
577578
} else if (c == ']' || c == '}') {
578-
MG_EOO('O');
579+
MG_EOO();
579580
if (depth == ed && ei >= 0) ci++;
580581
} else {
581582
return -1;

test/main.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,19 @@ static void test_m(void) {
181181

182182
static void test_json(void) {
183183
char buf[100];
184-
const char *json = "{ \"a\": -42, \"b\": [ \"hi\\t\\u0020\", true, { } ] }";
185-
int ofs, n, b = 0, len = (int) strlen(json);
186-
assert(json_get_long(json, len, "$.a", 0) == -42);
187-
assert(json_get_str(json, len, "$.b[0]", buf, sizeof(buf)) == 4);
184+
const char *s = "{\"a\": -42, \"b\": [\"hi\\t\\u0020\", true, { }, -1.7e-2]}";
185+
int ofs, n, b = 0, len = (int) strlen(s);
186+
double d = 0.0;
187+
assert(json_get_long(s, len, "$.a", 0) == -42);
188+
assert(json_get_str(s, len, "$.b[0]", buf, sizeof(buf)) == 4);
188189
assert(strcmp(buf, "hi\t ") == 0);
189-
assert(json_get_bool(json, len, "$.b[1]", &b) == 1);
190-
assert(b == 1);
191-
assert(json_get(json, len, "$.c", &n) < 0);
192-
assert((ofs = json_get(json, len, "$.b[2]", &n)) > 0 && n == 3 &&
193-
json[ofs] == '{' && json[ofs + 2] == '}');
190+
assert(json_get_str(s, len, "$.b[0]", buf, 4) < 0);
191+
assert(json_get_bool(s, len, "$.b[1]", &b) == 1 && b == 1);
192+
assert(json_get(s, len, "$.c", &n) < 0);
193+
assert(json_get(s, len, "$.b[4]", &n) < 0);
194+
assert((ofs = json_get(s, len, "$.b[2]", &n)) > 0);
195+
assert(n == 3 && s[ofs] == '{' && s[ofs + 2] == '}');
196+
assert(json_get_num(s, len, "$.b[3]", &d) == 1 && d == -0.017);
194197
}
195198

196199
int main(void) {

0 commit comments

Comments
 (0)