-
Notifications
You must be signed in to change notification settings - Fork 48
/
test.c
84 lines (64 loc) · 2.09 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/js0n.h"
#include "unit_test.h"
int main(int argc, char **argv)
{
char buff[1024], *json = NULL;
const char *ret;
size_t len, len2, jlen=0;
FILE *f;
fail_unless((f = fopen("./test/test.json","r")) != NULL);
while((len = fread(buff,1,1024,f)) > 0)
{
json = realloc(json,jlen+len);
memcpy(json+jlen,buff,len);
jlen+=len;
}
fclose(f);
fail_unless((ret = js0n("test",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("value",ret,len) == 0);
fail_unless((ret = js0n("foo",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("b\\\"a and \\\\ r",ret,len) == 0);
fail_unless((ret = js0n("array",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("[1,2,\"three\",4]",ret,len) == 0);
fail_unless((ret = js0n(0,2,ret,len,&len2)));
fail_unless(len);
fail_unless(strncmp("three",ret,len2) == 0);
fail_unless((ret = js0n("baz",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("{\"a\":\"b\"}",ret,len) == 0);
fail_unless((ret = js0n("num",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("123.45",ret,len) == 0);
fail_unless((ret = js0n("bool",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("true",ret,len) == 0);
fail_unless((ret = js0n("bare",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("yep",ret,len) == 0);
fail_unless((ret = js0n("utf8",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("$¢€𤪤",ret,len) == 0);
fail_unless((ret = js0n("key",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("value\\n\\\"newline\\\"",ret,len) == 0);
fail_unless((ret = js0n("obj",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("{\"true\":true}",ret,len) == 0);
fail_unless((ret = js0n("value",0,json,jlen,&len)));
fail_unless(len);
fail_unless(strncmp("real",ret,len) == 0);
// test parse errors
fail_unless(js0n("x",0,"{}",2,&len) == 0);
fail_unless(len == 0);
fail_unless(js0n("x",0,"{",1,&len) == 0);
fail_unless(len == 1);
fail_unless(js0n("x",0,"{\0}",3,&len) == 0);
fail_unless(len == 1);
return 0;
}