]> pd.if.org Git - hexagon/blob - t/astar.c
cb7f0434711c71dd59947a3fbc8e8d94d59351ee
[hexagon] / t / astar.c
1 #include <stdio.h>
2
3 #include "hexagon.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         do {
31                 neighbor = HL_adjacent_hex(hex, dir++);
32         } while (neighbor == HL_cantor_xy(5,5));
33
34         return neighbor;
35 }
36
37 int main(void) {
38         struct HL_astar *p;
39
40         plan_tests(16);
41
42         p = HL_astar_init(0);
43         ok(p != NULL, "allocated astar struct");
44         ok(p->malloced == 1, "allocated astar sets malloced");
45         ok(p->open == NULL, "initial open list empty");
46         ok(p->closed == NULL, "initial closed list empty");
47
48         pcheck(p, 1,1, 1,3, 2);
49         pcheck(p, 1,1, 2,1, 1);
50         pcheck(p, 1,1, 2,2, 2);
51         pcheck(p, 1,1, 3,1, 2);
52         pcheck(p, 1,1, 4,7, 8);
53
54         HL_astar_clear(p);
55         p->neighbor = neighbor55;
56         pcheck(p,5,4,5,6,3);
57
58         HL_astar_free(p);
59
60         return exit_status();
61 }