]> pd.if.org Git - hexagon/blob - t/astar.c
af88268d4212af1c23484843108e5aeeb4144d81
[hexagon] / t / astar.c
1 #include <stdio.h>
2
3 #include "hexmap.h"
4
5 #include "tap.h"
6
7 void pcheck(struct HL_astar *p, int x1, int y1, int x2, int y2, int expect) {
8         int from, to, dist;
9
10         from = HL_cantor_xy(x1,y1);
11         to = HL_cantor_xy(x2,y2);
12
13         HL_astar_clear(p);
14
15         p->start = from;
16         p->goal = to;
17
18         dist = HL_findpath(p,0);
19         ok(p->error == 0, "found path from (%02d, %02d) to (%02d, %02d) with no error",
20                 x1, y1, x2, y2);
21         ok(dist == expect,
22                         "found path from (%02d, %02d) to (%02d, %02d) path length = %d (expect %d)",
23                 x1, y1, x2, y2, dist, expect);
24 }
25
26 /* make hex 55 missing */
27 int neighbor55(int hex, int dir) {
28         int neighbor;
29
30         neighbor = HL_adjacent_hex(hex, dir);
31         if (neighbor == HL_cantor_xy(5,5)) {
32                 return -1;
33         }
34
35         return neighbor;
36 }
37
38 int main(void) {
39         struct HL_astar *p;
40         int length;
41
42         plan_tests(17);
43
44         p = HL_astar_init(0);
45         ok(p != NULL, "allocated astar struct");
46         ok(p->malloced == 1, "allocated astar sets malloced");
47         ok(p->open == NULL, "initial open list empty");
48         ok(p->closed == NULL, "initial closed list empty");
49
50         p->start = HL_cantor_xy(1, 1);
51         p->goal = HL_cantor_xy(1, 2);
52         
53         length = HL_findpath(p, 0);
54         ok(p->error == 0, "path finding returns no error");
55
56         pcheck(p, 1,1, 1,3, 2);
57         pcheck(p, 1,1, 2,1, 1);
58         pcheck(p, 1,1, 2,2, 2);
59         pcheck(p, 1,1, 3,1, 2);
60         pcheck(p, 1,1, 4,7, 8);
61
62         HL_astar_clear(p);
63         p->neighbor = neighbor55;
64         pcheck(p,5,4,5,6,3);
65
66         HL_astar_free(p);
67
68         return exit_status();
69 }