summaryrefslogtreecommitdiff
path: root/tests/interpretation.c
blob: bdae290b8cd889a84c2552bf0f57bcd378b39fea (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "test.h"

#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

void test_interpretation(void) {
	char **listing = list_dir("interpretation", ".pom");
	if (!listing)
		return;
	for (size_t l = 0; listing[l]; l++) {
		const char *conf_path = listing[l];
		printf("Testing %s...\n",conf_path);
		pom_error *error;
		pom_conf *conf = pom_load_path(NULL, conf_path, &error);
		if (error) {
			test_fail("Error parsing %s: %s.", conf_path,
				pom_error_to_string(error));
			free(error);
			continue;
		}
		const pom_conf *bad = pom_conf_section(conf, "bad");
		const pom_conf *good = pom_conf_section(conf, "good");
		const char *key = NULL;
		pom_key_iter *iter = NULL;
		if (strstr(conf_path, "uint.pom")) {
			uint64_t val, val2;
			while ((key = pom_conf_next_key(bad, &iter))) {
				if (!pom_conf_get_uint(bad, key, &val)) {
					test_fail("Key %s should be rejected as a uint", key);
				}
			}
			while ((key = pom_conf_next_key(good, &iter))) {
				const pom_conf *section = pom_conf_section(good, key);
				if (pom_conf_get_uint(section, "a", &val)) {
					test_fail("Key %s.a should be parsed as a uint", key);
					continue;
				}
				if (pom_conf_get_uint(section, "b", &val2)) {
					test_fail("Key %s.a should be parsed as a uint", key);
					continue;
				}
				if (val != val2) {
					test_fail("Keys %s.a and %s.b disagree:\n"
						"a: %" PRIu64 "\n"
						"b: %" PRIu64, key, key, val, val2);
				}
			}
		} else if (strstr(conf_path, "int.pom")) {
			int64_t val, val2;
			while ((key = pom_conf_next_key(bad, &iter))) {
				if (!pom_conf_get_int(bad, key, &val)) {
					test_fail("Key %s should be rejected as an int", key);
				}
			}
			while ((key = pom_conf_next_key(good, &iter))) {
				const pom_conf *section = pom_conf_section(good, key);
				if (pom_conf_get_int(section, "a", &val)) {
					test_fail("Key %s.a should be parsed as an int", key);
					continue;
				}
				if (pom_conf_get_int(section, "b", &val2)) {
					test_fail("Key %s.a should be parsed as an int", key);
					continue;
				}
				if (val != val2) {
					test_fail("Keys %s.a and %s.b disagree:\n"
						"a: %" PRId64 "\n"
						"b: %" PRId64, key, key, val, val2);
				}
			}
		} else if (strstr(conf_path, "float.pom")) {
			double val, val2;
			while ((key = pom_conf_next_key(bad, &iter))) {
				if (!pom_conf_get_float(bad, key, &val)) {
					test_fail("Key %s should be rejected as a float", key);
				}
			}
			while ((key = pom_conf_next_key(good, &iter))) {
				const pom_conf *section = pom_conf_section(good, key);
				if (pom_conf_get_float(section, "a", &val)) {
					test_fail("Key %s.a should be parsed as a float", key);
					continue;
				}
				if (pom_conf_get_float(section, "b", &val2)) {
					test_fail("Key %s.a should be parsed as a float", key);
					continue;
				}
				if (val != val2) {
					test_fail("Keys %s.a and %s.b disagree:\n"
						"a: %f\n"
						"b: %f", key, key, val, val2);
				}
			}
		} else if (strstr(conf_path, "bool.pom")) {
			bool val, val2;
			while ((key = pom_conf_next_key(bad, &iter))) {
				if (!pom_conf_get_bool(bad, key, &val)) {
					test_fail("Key %s should be rejected as a bool", key);
				}
			}
			while ((key = pom_conf_next_key(good, &iter))) {
				const pom_conf *section = pom_conf_section(good, key);
				if (pom_conf_get_bool(section, "a", &val)) {
					test_fail("Key %s.a should be parsed as a bool", key);
					continue;
				}
				if (pom_conf_get_bool(section, "b", &val2)) {
					test_fail("Key %s.a should be parsed as a bool", key);
					continue;
				}
				if (val != val2) {
					test_fail("Keys %s.a and %s.b disagree:\n"
						"a: %d\n"
						"b: %d", key, key, val, val2);
				}
			}
		} else if (strstr(conf_path, "list.pom")) {
			// TODO
			while ((key = pom_conf_next_key(conf, &iter))) {
				const pom_conf *section = pom_conf_section(conf, key);
				char **list = pom_conf_get_list(section, "list");
				if (!list) {
					test_fail("%s should have a subkey 'list'", key);
					continue;
				}
				const char *sep = pom_conf_get(section, "sep");
				if (!sep) {
					test_fail("%s should have a subkey 'sep'", key);
					free(list);
					continue;
				}
				size_t list_count, sep_count = 0;
				for (list_count = 0; list[list_count]; list_count++);
				for (size_t i = 0; sep[i]; i++)
					sep_count += sep[i] == ';';
				if (list_count != sep_count) {
					test_fail("List %s should have %zu elements (got %zu)", key, sep_count, list_count);
					free(list);
					continue;
				}
				for (size_t i = 0; list[i]; i++) {
					const char *item = list[i];
					size_t sep_len = strcspn(sep, ";");
					if (strlen(item) != sep_len
						|| memcmp(item, sep, sep_len) != 0) {
						test_fail("List %s element %zu is wrong:\n"
							"expected: %.*s\n"
							"     got: %s\n",
							key, i, (int)sep_len, sep, item);
					}
					sep += sep_len + 1;
				}
				free(list);
			}
		} else {
			test_fail("Unrecognized test: %s",conf_path);
		}
		pom_conf_free(conf);
	}
	free_listing(listing);
//	pom_conf *conf = pom_load_path("../tests");
}