summaryrefslogtreecommitdiff
path: root/test-solver.c
diff options
context:
space:
mode:
Diffstat (limited to 'test-solver.c')
-rw-r--r--test-solver.c57
1 files changed, 53 insertions, 4 deletions
diff --git a/test-solver.c b/test-solver.c
index 4ea14a0..4f5d2ad 100644
--- a/test-solver.c
+++ b/test-solver.c
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <string.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
#include "debug.h"
#include "update.h"
@@ -8,15 +11,33 @@
#include "display.h"
#include "load.h"
+#define EXPECTED_SKU_FILENAME "./expected.sku"
+
+/* FIXME move to another TU */
+/* Check if all of the non-zero defined cells in *orig are still the same in
+ * *solution */
+int board_subset(struct cell orig[9][9], struct cell solution[9][9])
+{
+ int x = 0;
+ int y = 0;
+
+ for (y = 0; y < 9; y++)
+ for (x = 0; x < 9; x++)
+ if (orig[x][y].val != 0 && orig[x][y].val != solution[x][y].val)
+ return -1;
+
+ return 0;
+}
+
/* FIXME move to another TU */
-int boards_match(struct cell (*lb)[9][9], struct cell (*rb)[9][9])
+int boards_match(struct cell lb[9][9], struct cell rb[9][9])
{
int x = 0;
int y = 0;
for (y = 0; y < 9; y++)
for (x = 0; x < 9; x++)
- if ((*lb)[x][y].val != (*rb)[x][y].val)
+ if (lb[x][y].val != rb[x][y].val)
return -1;
return 0;
@@ -42,6 +63,9 @@ int solve(struct cell (*board)[9][9])
int main(int argc, char **argv)
{
+ int ret = 0;
+ struct stat sb;
+ struct cell orig_board[9][9];
struct cell working_board[9][9];
struct cell correct_board[9][9];
FILE *f = NULL;
@@ -60,9 +84,30 @@ int main(int argc, char **argv)
fclose(f);
+ memcpy(orig_board, working_board, sizeof(orig_board));
+
solve(&working_board);
- if (!(f = fopen("./expected.sku", "r"))) {
+ if (board_subset(orig_board, working_board)) {
+ fprintf(stderr, "Solution has modified one or more starting value(s)!\n");
+ return 1;
+ }
+
+ if (board_is_solved(&working_board)) {
+ fprintf(stderr, "Solution is not valid\n");
+ return 1;
+ }
+
+
+ if (stat(EXPECTED_SKU_FILENAME, &sb) < 0 && errno == ENOENT)
+ return 0;
+
+ /* Extra sanity check if expected.sku is there. Really this isn't needed,
+ * but it's nice to have the sudoku solutions in with the tests for
+ * debugging, so we might as well triple-check the solution against them */
+
+
+ if (!(f = fopen(EXPECTED_SKU_FILENAME, "r"))) {
perror("fopen");
return 1;
}
@@ -74,5 +119,9 @@ int main(int argc, char **argv)
fclose(f);
- return boards_match(&working_board, &correct_board);
+ ret = boards_match(working_board, correct_board);
+ if (ret) {
+ fprintf(stderr, "Solution does not match expected\n");
+ }
+ return ret;
}