• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __TEST_UTIL_H__
2 #define __TEST_UTIL_H__
3 
4 #include "../jsmn.h"
5 
vtokeq(const char * s,jsmntok_t * t,unsigned long numtok,va_list ap)6 static int vtokeq(const char *s, jsmntok_t *t, unsigned long numtok,
7                   va_list ap) {
8   if (numtok > 0) {
9     unsigned long i;
10     int start, end, size;
11     int type;
12     char *value;
13 
14     size = -1;
15     value = NULL;
16     for (i = 0; i < numtok; i++) {
17       type = va_arg(ap, int);
18       if (type == JSMN_STRING) {
19         value = va_arg(ap, char *);
20         size = va_arg(ap, int);
21         start = end = -1;
22       } else if (type == JSMN_PRIMITIVE) {
23         value = va_arg(ap, char *);
24         start = end = size = -1;
25       } else {
26         start = va_arg(ap, int);
27         end = va_arg(ap, int);
28         size = va_arg(ap, int);
29         value = NULL;
30       }
31       if (t[i].type != type) {
32         printf("token %lu type is %d, not %d\n", i, t[i].type, type);
33         return 0;
34       }
35       if (start != -1 && end != -1) {
36         if (t[i].start != start) {
37           printf("token %lu start is %d, not %d\n", i, t[i].start, start);
38           return 0;
39         }
40         if (t[i].end != end) {
41           printf("token %lu end is %d, not %d\n", i, t[i].end, end);
42           return 0;
43         }
44       }
45       if (size != -1 && t[i].size != size) {
46         printf("token %lu size is %d, not %d\n", i, t[i].size, size);
47         return 0;
48       }
49 
50       if (s != NULL && value != NULL) {
51         const char *p = s + t[i].start;
52         if (strlen(value) != (unsigned long)(t[i].end - t[i].start) ||
53             strncmp(p, value, t[i].end - t[i].start) != 0) {
54           printf("token %lu value is %.*s, not %s\n", i, t[i].end - t[i].start,
55                  s + t[i].start, value);
56           return 0;
57         }
58       }
59     }
60   }
61   return 1;
62 }
63 
tokeq(const char * s,jsmntok_t * tokens,int numtok,...)64 static int tokeq(const char *s, jsmntok_t *tokens, int numtok, ...) {
65   int ok;
66   va_list args;
67   va_start(args, numtok);
68   ok = vtokeq(s, tokens, numtok, args);
69   va_end(args);
70   return ok;
71 }
72 
parse(const char * s,int status,int numtok,...)73 static int parse(const char *s, int status, int numtok, ...) {
74   int r;
75   int ok = 1;
76   va_list args;
77   jsmn_parser p;
78   jsmntok_t *t = malloc(numtok * sizeof(jsmntok_t));
79 
80   jsmn_init(&p);
81   r = jsmn_parse(&p, s, strlen(s), t, numtok);
82   if (r != status) {
83     printf("status is %d, not %d\n", r, status);
84     return 0;
85   }
86 
87   if (status >= 0) {
88     va_start(args, numtok);
89     ok = vtokeq(s, t, numtok, args);
90     va_end(args);
91   }
92   free(t);
93   return ok;
94 }
95 
96 #endif /* __TEST_UTIL_H__ */
97