| #ifndef __TEST_UTIL_H__ |
| #define __TEST_UTIL_H__ |
| |
| #include "../jsmn.h" |
| |
| static int vtokeq(const char *s, jsmntok_t *t, unsigned long numtok, |
| va_list ap) { |
| if (numtok > 0) { |
| unsigned long i; |
| int start, end, size; |
| int type; |
| char *value; |
| |
| size = -1; |
| value = NULL; |
| for (i = 0; i < numtok; i++) { |
| type = va_arg(ap, int); |
| if (type == JSMN_STRING) { |
| value = va_arg(ap, char *); |
| size = va_arg(ap, int); |
| start = end = -1; |
| } else if (type == JSMN_PRIMITIVE) { |
| value = va_arg(ap, char *); |
| start = end = size = -1; |
| } else { |
| start = va_arg(ap, int); |
| end = va_arg(ap, int); |
| size = va_arg(ap, int); |
| value = NULL; |
| } |
| if (t[i].type != type) { |
| printf("token %lu type is %d, not %d\n", i, t[i].type, type); |
| return 0; |
| } |
| if (start != -1 && end != -1) { |
| if (t[i].start != start) { |
| printf("token %lu start is %d, not %d\n", i, t[i].start, start); |
| return 0; |
| } |
| if (t[i].end != end) { |
| printf("token %lu end is %d, not %d\n", i, t[i].end, end); |
| return 0; |
| } |
| } |
| if (size != -1 && t[i].size != size) { |
| printf("token %lu size is %d, not %d\n", i, t[i].size, size); |
| return 0; |
| } |
| |
| if (s != NULL && value != NULL) { |
| const char *p = s + t[i].start; |
| if (strlen(value) != (unsigned long)(t[i].end - t[i].start) || |
| strncmp(p, value, t[i].end - t[i].start) != 0) { |
| printf("token %lu value is %.*s, not %s\n", i, t[i].end - t[i].start, |
| s + t[i].start, value); |
| return 0; |
| } |
| } |
| } |
| } |
| return 1; |
| } |
| |
| static int tokeq(const char *s, jsmntok_t *tokens, int numtok, ...) { |
| int ok; |
| va_list args; |
| va_start(args, numtok); |
| ok = vtokeq(s, tokens, numtok, args); |
| va_end(args); |
| return ok; |
| } |
| |
| static int parse(const char *s, int status, int numtok, ...) { |
| int r; |
| int ok = 1; |
| va_list args; |
| jsmn_parser p; |
| jsmntok_t *t = malloc(numtok * sizeof(jsmntok_t)); |
| |
| jsmn_init(&p); |
| r = jsmn_parse(&p, s, strlen(s), t, numtok); |
| if (r != status) { |
| printf("status is %d, not %d\n", r, status); |
| return 0; |
| } |
| |
| if (status >= 0) { |
| va_start(args, numtok); |
| ok = vtokeq(s, t, numtok, args); |
| va_end(args); |
| } |
| free(t); |
| return ok; |
| } |
| |
| #endif /* __TEST_UTIL_H__ */ |