]> git.draconx.ca Git - rrace.git/blob - t/boardrect.c
Improve exposure mask calculation.
[rrace.git] / t / boardrect.c
1 /*
2  * Helper to test board_rect function.
3  * Copyright © 2022 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include "game.h"
21 #include "tap.h"
22
23 #define mask_char(x) ((x) ? '@' : '.')
24
25 static void print_mask(const char *prefix, unsigned long mask)
26 {
27         int i;
28
29         for (i = 0; i < 5; i++) {
30                 tap_diag("%s%c%c%c%c%c", prefix, mask_char(mask & 1),
31                                                  mask_char(mask & 2),
32                                                  mask_char(mask & 4),
33                                                  mask_char(mask & 8),
34                                                  mask_char(mask & 16));
35                 mask >>= 5;
36         }
37 }
38
39 static void do_check(int x1, int y1, int x2, int y2)
40 {
41         unsigned long expect = board_right(x1)
42                              & board_below(y1)
43                              & board_left(x2)
44                              & board_above(y2);
45
46         unsigned long actual = board_rect(x1, y1, x2, y2);
47
48         if (!tap_result(actual == expect, "board_rect(%d, %d, %d, %d)",
49                                           x1, y1, x2, y2))
50         {
51                 tap_diag("Failed, unexpected result");
52                 tap_diag("Received 0x%.7lx:", actual);
53                 print_mask("   ", actual);
54                 tap_diag("Expected 0x%.7lx:", expect);
55                 print_mask("   ", expect);
56         }
57 }
58
59 int main(void)
60 {
61         int x1, y1, x2, y2;
62
63         tap_plan(225);
64
65         /* Exhaustive search of all valid inputs */
66         for (x1 = 0; x1 < 5; x1++) {
67                 for (y1 = 0; y1 < 5; y1++) {
68                         for (x2 = x1; x2 < 5; x2++) {
69                                 for (y2 = y1; y2 < 5; y2++) {
70                                         do_check(x1, y1, x2, y2);
71                                 }
72                         }
73                 }
74         }
75         tap_done();
76 }