aboutsummaryrefslogtreecommitdiff
path: root/src/test.c
blob: c9bb3a637a987197145f5a88b43175164d399633 (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>

#ifdef TEST
#ifndef NTESTS
#define NTESTS 6
#endif

#ifndef STARTTEST
#define STARTTEST 0
#endif

#define TESTDIR "./tests/"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>

#include <gc.h>
#include <parse.h>
#include <term.h>
#include <reducer.h>

struct test {
	struct term *in;
	struct term *res;
	struct term *red;
	char *trans;
	struct {
		int alpha;
		int trans;
	} equivalency;
};

static int name_generator(void)
{
	static int current = 0xbadbad;
	return current++;
}

static void *church(int n, void(*f(void *, int)), void *x, int name)
{
	if (n == 0)
		return x;
	return church(n - 1, f, f(x, name), name);
}

static void *church_numeral_builder(void *x, int name)
{
	struct term *app = new_term(APP);
	app->u.app.lhs = new_term(VAR);
	app->u.app.lhs->u.var.name = name;
	app->u.app.lhs->u.var.type = BARENDREGT_VARIABLE;
	app->u.app.rhs = x;
	return app;
}

static struct term *church_numeral(int n)
{
	struct term *abs = new_term(ABS);
	abs->u.abs.name = name_generator();
	abs->u.abs.term = new_term(ABS);

	struct term *var = new_term(VAR);
	var->u.var.name = name_generator();
	var->u.var.type = BARENDREGT_VARIABLE;

	abs->u.abs.term->u.abs.name = var->u.var.name;
	abs->u.abs.term->u.abs.term =
		church(n, church_numeral_builder, var, abs->u.abs.name);

	return abs;
}

static struct term *identity(void)
{
	struct term *abs = new_term(ABS);
	abs->u.abs.name = name_generator();
	abs->u.abs.term = new_term(VAR);
	abs->u.abs.term->u.var.name = abs->u.abs.name;
	abs->u.abs.term->u.var.type = BARENDREGT_VARIABLE;
	return abs;
}

static struct term *omega(void)
{
	struct term *abs = new_term(ABS);
	abs->u.abs.name = name_generator();
	abs->u.abs.term = new_term(APP);
	abs->u.abs.term->u.app.lhs = new_term(VAR);
	abs->u.abs.term->u.app.lhs->u.var.name = abs->u.abs.name;
	abs->u.abs.term->u.app.lhs->u.var.type = BARENDREGT_VARIABLE;
	abs->u.abs.term->u.app.rhs = new_term(VAR);
	abs->u.abs.term->u.app.rhs->u.var.name = abs->u.abs.name;
	abs->u.abs.term->u.app.rhs->u.var.type = BARENDREGT_VARIABLE;
	return abs;
}

static void counter_callback(int i, char ch, void *data)
{
	(void)ch;
	*(int *)data = i;
}

static void test_church_transitions(void)
{
	const int limit = 18;

	int deviations = 0;
	double time = 0;

	for (int n = 1; n <= limit; n++) {
		struct term *app = new_term(APP);
		app->u.app.lhs = new_term(APP);
		app->u.app.lhs->u.app.lhs = church_numeral(n);
		app->u.app.lhs->u.app.rhs = church_numeral(2);
		app->u.app.rhs = identity();
		int counter;

		clock_t begin = clock();
		struct term *red = reduce(app, counter_callback, &counter);
		clock_t end = clock();
		time += (double)(end - begin) / CLOCKS_PER_SEC;

		free_term(red);
		free_term(app);

		int expected = 10 * (2 << (n - 1)) + n * 5 + 5;
		if (counter + 1 != expected)
			deviations++;
	}

	printf("Test church ((n 2) I) with n<=%d: %.5fs, %d transition deviations\n",
	       limit, time, deviations);
}

static void test_explode(void)
{
	const int limit = 23;

	int deviations = 0;
	double time = 0;

	for (int n = 1; n <= limit; n++) {
		struct term *abs = new_term(ABS);
		abs->u.abs.name = name_generator();
		abs->u.abs.term = new_term(APP);
		abs->u.abs.term->u.app.lhs = new_term(APP);
		abs->u.abs.term->u.app.lhs->u.app.lhs = church_numeral(n);
		abs->u.abs.term->u.app.lhs->u.app.rhs = omega();
		abs->u.abs.term->u.app.rhs = new_term(VAR);
		abs->u.abs.term->u.app.rhs->u.var.name = abs->u.abs.name;
		abs->u.abs.term->u.app.rhs->u.var.type = BARENDREGT_VARIABLE;

		int counter;

		clock_t begin = clock();
		struct term *red = reduce(abs, counter_callback, &counter);
		clock_t end = clock();
		time += (double)(end - begin) / CLOCKS_PER_SEC;

		free_term(red);
		free_term(abs);

		int expected = 9 * n + 15;
		if (counter + 1 != expected)
			deviations++;
	}

	printf("Test explode (λx.((n ω) x)) with n<=%d: %.5fs, %d transition deviations\n",
	       limit, time, deviations);
}

static char *read_file(const char *path)
{
	FILE *f = fopen(path, "rb");
	if (!f) {
		fprintf(stderr, "Can't open file %s: %s\n", path,
			strerror(errno));
		return 0;
	}
	fseek(f, 0, SEEK_END);
	long fsize = ftell(f);
	fseek(f, 0, SEEK_SET);

	char *string = malloc(fsize + 1);
	int ret = fread(string, fsize, 1, f);
	fclose(f);

	if (ret != 1) {
		fprintf(stderr, "Can't read file %s: %s\n", path,
			strerror(errno));
		return 0;
	}

	string[fsize] = 0;
	return string;
}

static void callback(int i, char ch, void *data)
{
	struct test *test = data;
	if (ch != test->trans[i]) {
		fprintf(stderr, "Transition deviation at index %d!\n", i);
		test->equivalency.trans = 0;
	}
}

int main(void)
{
	GC_INIT();
	GC_enable_incremental();

	struct test tests[NTESTS] = { 0 };

	char in_template[] = TESTDIR "x.in";
	char red_template[] = TESTDIR "x.red";
	char trans_template[] = TESTDIR "x.trans";
	int offset = strlen(TESTDIR);
	for (int i = 0; i < NTESTS; i++) {
		char ch = '0' + i + 1 + STARTTEST;
		in_template[offset] = ch;
		red_template[offset] = ch;
		trans_template[offset] = ch;
		tests[i].trans = read_file(trans_template);

		char *in = read_file(in_template);
		tests[i].in = parse_bruijn(in);
		free(in);

		char *red = read_file(red_template);
		tests[i].red = parse_bruijn(red);
		to_bruijn(tests[i].red);
		free(red);

		tests[i].equivalency.trans = 1;
	}

	clock_t begin = clock();
	for (int i = 0; i < NTESTS; i++) {
		tests[i].res = reduce(tests[i].in, callback, &tests[i]);
		printf("Test %d done\n", i + 1 + STARTTEST);
	}
	clock_t end = clock();

	for (int i = 0; i < NTESTS; i++) {
		to_bruijn(tests[i].res);
		tests[i].equivalency.alpha =
			alpha_equivalency(tests[i].res, tests[i].red);
		free_term(tests[i].res);
		free_term(tests[i].red);
		free(tests[i].trans);
	}

	printf("\n=== REDUCTION SUMMARY ===\n");
	printf("Reduced tests in %.5fs\n",
	       (double)(end - begin) / CLOCKS_PER_SEC);
	for (int i = 0; i < NTESTS; i++) {
		if (tests[i].equivalency.alpha && tests[i].equivalency.trans)
			continue;
		printf("Test %d: [failed]\n\talpha-equivalency: %d\n\ttrans-equivalency: %d\n",
		       i + 1 + STARTTEST, tests[i].equivalency.alpha,
		       tests[i].equivalency.trans);
	}

	printf("\n=== OTHER TESTS ===\n");
	test_church_transitions();
	test_explode();
}
#else
__attribute__((unused)) static int no_testing;
#endif