]> pd.if.org Git - hexagon/blob - t/range.c
move astar to lib
[hexagon] / t / range.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <limits.h>
5 #include <math.h>
6
7 #include "hexagon.h"
8
9 #include "ctap.h"
10
11 int icmp(const void *ap, const void *bp) {
12         struct HL_hex const *a;
13         struct HL_hex const *b;
14         
15         a = ap;
16         b = bp;
17
18         if (a->x < b->x) return -1;
19         if (a->x > b->x) return 1;
20         if (a->y < b->y) return -1;
21         if (a->y > b->y) return 1;
22
23         return 0;
24 }
25
26 void sort(struct HL_hex *r, int ct) {
27         qsort(r, ct, sizeof *r, icmp);
28 }
29
30 int main(void) {
31         struct HL_hex start;
32         int i, count;
33         struct HL_hex range[32];
34         struct HL_hex testrange[32];
35
36         plan(7+4);
37
38         /* printf("range = 1 from 3,3\n"); */
39         start = HL_hex_xy(3,3);
40         count = HL_hexes_at_range(start, 1, range, 0);
41         ok(count == 6, "6 hexes at range 1");
42         count = HL_hexes_at_range(start, 1, range, count);
43         sort(range, 6);
44         testrange[0] = HL_hex_xy(3,2);
45         testrange[1] = HL_hex_xy(3,4);
46         testrange[2] = HL_hex_xy(4,3);
47         testrange[3] = HL_hex_xy(4,2);
48         testrange[4] = HL_hex_xy(2,3);
49         testrange[5] = HL_hex_xy(2,2);
50         sort(testrange, 6);
51         for (i = 0; i < count; i++) {
52                 ok(HL_hex_eq(&range[i], &testrange[i]), "3, 3 range 1 hex %d", testrange[i]);
53         }
54
55         start = HL_hex_xy(0,3);
56         count = HL_hexes_at_range(start, 1, range, 0);
57         ok(count == 6, "6 hexes at range1 from 0,3");
58
59         start = HL_hex_xy(-1,5);
60         count = HL_hexes_at_range(start, 1, range, 0);
61         ok(count == 6, "6 hexes at range1 from -1,5");
62
63         start = HL_hex_xy(-2,3);
64         count = HL_hexes_at_range(start, 1, range, 0);
65         ok(count == 6, "6 hexes at range1 from -2,3");
66
67         start = HL_hex_xy(3,3);
68         count = HL_hexes_at_range(start, 2, range, 0);
69         ok(count == 12, "6 hexes at range1 from 3,3");
70
71         return 0;
72 }