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