-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_plinterpreter.c
71 lines (57 loc) · 1.85 KB
/
test_plinterpreter.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
#include "test_plinterpreter.h"
#define MAX_LINE 1024
FILE *open_file(const char *filename)
{
FILE *file = fopen(filename, "r");
if (!file) {
WITH_COLOR(RED, printf("Could not open file: %s\n", filename));
exit(1);
return NULL;
}
return file;
}
void print_file(const char *filename)
{
FILE *file = fopen(filename, "r");
if (!file) {
WITH_COLOR(RED, printf("Could not open file: %s\n", filename));
exit(1);
}
char buf[MAX_LINE];
while (fgets(buf, MAX_LINE, file)) {
WITH_COLOR(YELLOW, printf("%s", buf));
}
printf("\n");
fclose(file);
}
void test_interpret(const char *database_filename, const char *query_filename,
int resolves, const char *solution)
{
WITH_COLOR(BLUE, printf("--------------------\n"));
FILE *db_file = open_file(database_filename);
FILE *query_file = open_file(query_filename);
printf("Database file:\n");
print_file(database_filename);
printf("Query file:\n");
print_file(query_filename);
PLHandleInput(db_file, query_file);
if (resolves) {
WITH_COLOR(BLUE, printf("Should resolve with resolvent:\n"));
WITH_COLOR(YELLOW, printf("%s\n", solution));
} else {
WITH_COLOR(BLUE, printf("Should not resolve\n"));
}
fclose(db_file);
fclose(query_file);
}
void test_plinterpreter()
{
WITH_COLOR(GREEN, printf("test_plinterpreter()\n"));
test_interpret("test_interpret_1_db.pl", "test_interpret_1_query.pl", 1, "add(s(0), s(0), s(s(0)))");
test_interpret("test_interpret_2_db.pl", "test_interpret_2_query.pl", 1, "owner(john, cat)");
test_interpret("test_interpret_3_db.pl", "test_interpret_3_query.pl", 1, "SEE FILE");
test_interpret("test_interpret_4_db.pl", "test_interpret_4_query.pl", 1, "sibling(sally,erica).");
test_interpret("quicksort.pl", "qs_query.pl", 1, "SEE FILE");
test_interpret("ackermann.pl", "ack.pl", 1, "SEE FILE");
test_interpret("parent.pl", "parent_q.pl", 1, "SEE FILE");
}