-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-tests.c
171 lines (154 loc) · 5.72 KB
/
run-tests.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
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
#include "decode.h"
#include "parse.h"
#include "sim.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#define MEASURE_TIMES 0
#if MEASURE_TIMES
#include <time.h>
#endif
struct puzzle {
struct puzzle *next;
char *filename;
struct puzzle_file *pf;
};
int main()
{
size_t n = 64;
char *buf = malloc(n);
struct puzzle *puzzles = 0;
FILE *puzzle_list = popen("find test/puzzle -type f", "r");
ssize_t line;
while ((line = getline(&buf, &n, puzzle_list)) >= 0) {
if (line > 0 && buf[line - 1] == '\n')
buf[line - 1] = '\0';
struct puzzle_file *pf = parse_puzzle_file(buf);
if (!pf) {
fprintf(stderr, "couldn't parse puzzle at '%s'\n", buf);
continue;
}
size_t last_slash = 0;
for (size_t i = 0; buf[i]; ++i) {
if (buf[i] == '/')
last_slash = i + 1;
}
size_t last_dot = 0;
for (size_t i = last_slash; buf[i]; ++i) {
if (buf[i] == '.')
last_dot = i;
}
if (last_dot > last_slash) {
struct puzzle *puzzle = calloc(sizeof(struct puzzle), 1);
puzzle->filename = malloc(last_dot - last_slash + 1);
buf[last_dot] = '\0';
memcpy(puzzle->filename, buf + last_slash, last_dot - last_slash + 1);
puzzle->pf = pf;
fprintf(stderr, "puzzle '%s' parsed\n", puzzle->filename);
puzzle->next = puzzles;
puzzles = puzzle;
}
}
pclose(puzzle_list);
int total_solutions = 0;
int validated_solutions = 0;
FILE *solution_list = popen("find test/solution -type f -name '*.solution'", "r");
while ((line = getline(&buf, &n, solution_list)) >= 0) {
#if MEASURE_TIMES
struct timespec tstart = {0};
struct timespec tend = {0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
#endif
if (line > 0 && buf[line - 1] == '\n')
buf[line - 1] = '\0';
struct solution_file *sf = parse_solution_file(buf);
if (!sf) {
fprintf(stderr, "couldn't parse solution at '%s'\n", buf);
continue;
}
struct puzzle *puzzle = puzzles;
while (puzzle && !byte_string_is(sf->puzzle, puzzle->filename))
puzzle = puzzle->next;
if (!puzzle) {
fprintf(stderr, "couldn't find puzzle named '%.*s' for '%s'\n", (int)sf->puzzle.length, sf->puzzle.bytes, buf);
free_solution_file(sf);
continue;
}
struct solution solution = { 0 };
struct board board = { 0 };
const char *error;
if (!decode_solution(&solution, puzzle->pf, sf, &error)) {
fprintf(stderr, "error in '%s': %s\n", buf, error);
free_solution_file(sf);
continue;
}
total_solutions++;
bool expect_collision = sf->cost == 0 && sf->instructions == 0;
uint64_t cost = solution_file_cost(sf);
if (sf->cost != cost && !expect_collision) {
fprintf(stderr, "cost mismatch for '%s'\n", buf);
fprintf(stderr, "solution file says cost is: %" PRIu32 "\n", sf->cost);
fprintf(stderr, "adding up its parts, the cost is: %" PRIu64 "\n", cost);
goto fail;
}
uint64_t instructions = solution_instructions(&solution);
if (sf->instructions != instructions && !expect_collision) {
fprintf(stderr, "instructions mismatch for '%s'\n", buf);
fprintf(stderr, "solution file says instruction count is: %" PRIu32 "\n", sf->instructions);
fprintf(stderr, "counting instructions says instruction count is: %" PRIu64 "\n", instructions);
goto fail;
}
// set up the board.
initial_setup(&solution, &board, sf->area);
// run the solution.
while (board.cycle < 200000 && !board.complete) {
cycle(&solution, &board);
if (board.collision) {
if (expect_collision)
goto success;
fprintf(stderr, "collision in '%s' at %" PRId32 ", %" PRId32 ": %s\n", buf,
board.collision_location.u, board.collision_location.v,
board.collision_reason);
break;
}
}
if (sf->cycles != board.cycle) {
fprintf(stderr, "cycle mismatch for '%s'\n", buf);
fprintf(stderr, "solution file says cycle count is: %" PRIu32 "\n", sf->cycles);
fprintf(stderr, "simulation says cycle count is: %" PRIu64 "\n", board.cycle);
goto fail;
}
uint32_t area = used_area(&board);
if (!puzzle->pf->production_info && sf->area != area) {
fprintf(stderr, "area mismatch for '%s'\n", buf);
fprintf(stderr, "solution file says area is: %" PRIu32 "\n", sf->area);
fprintf(stderr, "simulation says area is: %" PRIu32 "\n", area);
goto fail;
}
success:
#if MEASURE_TIMES
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("%.5f C=%" PRIu64 " A=%" PRIu32 " %s\n",
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec),
board.cycle,
used_area(&board),
buf);
#endif
validated_solutions++;
fail:
destroy(&solution, &board);
free_solution_file(sf);
}
pclose(solution_list);
fprintf(stderr, "%d / %d solutions validated!\n", validated_solutions, total_solutions);
free(buf);
struct puzzle *puzzle = puzzles;
while (puzzle) {
struct puzzle *next = puzzle->next;
free_puzzle_file(puzzle->pf);
free(puzzle->filename);
free(puzzle);
puzzle = next;
}
}