summaryrefslogtreecommitdiff
path: root/test-solver.c
blob: 4f5d2ade707aa16f1c4c5cbcff5c321c8c7aa41c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>

#include "debug.h"
#include "update.h"
#include "cell.h"
#include "solve.h"
#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 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)
				return -1;

	return 0;
}

/* FIXME move to another TU, dedup with solver.c */
int solve(struct cell (*board)[9][9])
{
	int i = 0;
	int total_changes = 0;
	int change_count = -1;
	/* 9*9 is worst case */
	for (i = 0; i < 9*9 && change_count; i++) {
		change_count = 0;
		update_not_row_col(board);
		change_count += not_to_val_cell(board);
		change_count += solve_row_col(board);
		change_count += cells_fill_certainties(board);
		total_changes += change_count;
	}
	return total_changes;
}

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;

	memset(working_board, 0, sizeof(working_board));

	if (!(f = fopen("./in.sku", "r"))) {
		perror("fopen");
		return 1;
	}

	if (load(f, &working_board) < 0) {
		fclose(f);
		return 1;
	}

	fclose(f);

	memcpy(orig_board, working_board, sizeof(orig_board));

	solve(&working_board);

	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;
	}

	if (load(f, &correct_board) < 0) {
		fclose(f);
		return 1;
	}

	fclose(f);

	ret = boards_match(working_board, correct_board);
	if (ret) {
		fprintf(stderr, "Solution does not match expected\n");
	}
	return ret;
}