Moggy 1lib prior: Add support for stone weighing, turned off by default
[pachi/pachi-r6144.git] / t-unit / test.c
blobe4473a9e3af9030357e7285ec7ec70a5cf934508
1 #define DEBUG
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "tactics/selfatari.h"
10 #include "random.h"
12 static bool board_printed;
14 void
15 board_load(struct board *b, FILE *f, unsigned int size)
17 board_printed = false;
18 board_resize(b, size);
19 board_clear(b);
20 for (int y = size - 1; y >= 0; y--) {
21 char line[256];
22 if (!fgets(line, sizeof(line), f)) {
23 fprintf(stderr, "Premature EOF.\n");
24 exit(EXIT_FAILURE);
26 line[strlen(line) - 1] = 0; // chomp
27 if (strlen(line) != size) {
28 fprintf(stderr, "Line not %d char long: %s\n", size, line);
29 exit(EXIT_FAILURE);
31 for (unsigned int x = 0; x < size; x++) {
32 enum stone s;
33 switch (line[x]) {
34 case '.': s = S_NONE; break;
35 case 'X': s = S_BLACK; break;
36 case 'O': s = S_WHITE; break;
37 default: fprintf(stderr, "Invalid stone %c\n", line[x]);
38 exit(EXIT_FAILURE);
40 if (s == S_NONE) continue;
41 struct move m = { .color = s, .coord = coord_xy(b, x + 1, y + 1) };
42 if (board_play(b, &m) < 0) {
43 fprintf(stderr, "Failed to play %s %s\n",
44 stone2str(s), coord2sstr(m.coord, b));
45 board_print(b, stderr);
46 exit(EXIT_FAILURE);
50 if (DEBUGL(2))
51 board_print(b, stderr);
54 void
55 test_sar(struct board *b, char *arg)
57 enum stone color = str2stone(arg);
58 arg += 2;
59 coord_t *cc = str2coord(arg, board_size(b));
60 coord_t c = *cc; coord_done(cc);
61 arg += strcspn(arg, " ") + 1;
62 int eres = atoi(arg);
63 if (DEBUGL(1))
64 printf("sar %s %s %d...\t", stone2str(color), coord2sstr(c, b), eres);
66 int rres = is_bad_selfatari(b, color, c);
68 if (rres == eres) {
69 if (DEBUGL(1))
70 printf("OK\n");
71 } else {
72 if (debug_level <= 2) {
73 if (DEBUGL(0) && !board_printed) {
74 board_print(b, stderr);
75 board_printed = true;
77 printf("sar %s %s %d...\t", stone2str(color), coord2sstr(c, b), eres);
79 printf("FAILED (%d)\n", rres);
84 void
85 unittest(char *filename)
87 FILE *f = fopen(filename, "r");
88 if (!f) {
89 perror(filename);
90 exit(EXIT_FAILURE);
93 struct board *b = board_init(NULL);
94 char line[256];
95 while (fgets(line, sizeof(line), f)) {
96 line[strlen(line) - 1] = 0; // chomp
97 switch (line[0]) {
98 case '%': printf("\n%s\n", line); continue;
99 case 0: continue;
101 if (!strncmp(line, "boardsize ", 10)) {
102 board_load(b, f, atoi(line + 10));
103 } else if (!strncmp(line, "sar ", 4)) {
104 test_sar(b, line + 4);
105 } else {
106 fprintf(stderr, "Syntax error: %s\n", line);
107 exit(EXIT_FAILURE);
111 fclose(f);