summaryrefslogtreecommitdiff
path: root/test/parse.c
blob: 07dbbfa08ecfe2ea103c2f811b6d5566754ab0c2 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "parse.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "log.h"

#define fail() fail_impl(__LINE__);

int fail_impl(int line) {
	log("%d: Test fail!\n", line);
	return 1;
}

int main(int argc, char **argv) {
	printf("Parser Tests\n");

	{
		struct Tree a;
		size_t *chunks = NULL;
		if(parse_string("<root></root>", &a, &chunks) != 0) return fail();
		if(a.adj.stride != 1) return fail();
		if(a.len != 1) return fail();
		nid expected[] = {
			0,
		};
		if(memcmp(a.adj.data, expected, sizeof(expected)) != 0) return fail();
		
		// Verify chunk offsets
		if(chunks[0] != 0) return fail();
		
		free(chunks);
		free(a.adj.data);
	}

	{
		struct Tree a;
		size_t *chunks = NULL;
		if(parse_string("<root><a></a><b></b></root>", &a, &chunks) != 0) return fail();
		if(a.adj.stride != 2) return fail();
		if(a.len != 3) return fail();
		nid expected[] = {
			2, 3,
			0, 0,
			0, 0,
		};
		if(memcmp(a.adj.data, expected, sizeof(expected)) != 0) return fail();
		
		// Verify chunk offsets
		if(chunks[0] != 0) return fail();
		if(chunks[1] != 6) return fail();
		if(chunks[2] != 13) return fail();
		
		free(chunks);
		free(a.adj.data);
	}

	{
		struct Tree a;
		size_t *chunks = NULL;
		if(parse_string("<root><a><c></c></a><b></b></root>", &a, &chunks) != 0) return fail();
		if(a.adj.stride != 2) return fail();
		if(a.len != 4) return fail();
		nid expected[] = {
			2, 4,
			3, 0,
			0, 0,
			0, 0,
		};
		if(memcmp(a.adj.data, expected, sizeof(expected)) != 0) return fail();
		
		// Verify chunk offsets
		if(chunks[0] != 0) return fail();
		if(chunks[1] != 6) return fail();
		if(chunks[2] != 9) return fail();
		if(chunks[3] != 20) return fail();
		
		free(chunks);
		free(a.adj.data);
	}

	// We forgot to close the open tag
	{
		struct Tree a;
		size_t *chunks = NULL;
		fflush(stdout);
		if(parse_string("<root</root>", &a, &chunks) != 1) return fail();
		fflush(stdout);
	}

	// Test self-closing tag
	{
		struct Tree a;
		size_t *chunks = NULL;
		fflush(stdout);
		if(parse_string("<root><a /></root>", &a, &chunks) != 0) return fail();
		if(a.adj.stride != 1) return fail();
		if(a.len != 2) return fail();
		nid expected[] = {
			2,
			0,
		};
		if(memcmp(a.adj.data, expected, sizeof(expected)) != 0) return fail();
		
		// Verify chunk offsets
		if(chunks[0] != 0) return fail();
		if(chunks[1] != 6) return fail();

		free(chunks);
		free(a.adj.data);
	}

	return 0;
}