]> git.ozlabs.org Git - ccan/blob - ccan/json/test/run-validate.c
json: new module for parsing and generating JSON
[ccan] / ccan / json / test / run-validate.c
1 #include "common.h"
2
3 int main(void)
4 {
5         const char *strings_file = "test/test-strings";
6         FILE *f;
7         char buffer[1024];
8         
9         plan_tests(224);
10         
11         f = fopen(strings_file, "rb");
12         if (f == NULL) {
13                 diag("Could not open %s: %s", strings_file, strerror(errno));
14                 return 1;
15         }
16         
17         while (fgets(buffer, sizeof(buffer), f)) {
18                 const char *s = chomp(buffer);
19                 bool valid;
20                 
21                 if (expect_literal(&s, "valid ")) {
22                         valid = true;
23                 } else if (expect_literal(&s, "invalid ")) {
24                         valid = false;
25                 } else {
26                         fail("Invalid line in test-strings: %s", buffer);
27                         continue;
28                 }
29                 
30                 if (strcmp(s, "\"1\\u2\"") == 0)
31                         puts("here");
32                 
33                 if (json_validate(s) == valid) {
34                         pass("%s %s", valid ? "valid" : "invalid", s);
35                 } else {
36                         fail("%s is %s, but json_validate returned %s",
37                                  s,
38                                  valid ? "valid" : "invalid",
39                                  valid ? "false" : "true");
40                 }
41         }
42         
43         if (ferror(f) || fclose(f) != 0) {
44                 diag("I/O error reading test strings.");
45                 return 1;
46         }
47         
48         return exit_status();
49 }