aboutsummaryrefslogtreecommitdiff
path: root/tools/400_solvetest/solve_test.c
blob: 1bfe8519d88dbf57fa52e8aa5b01d6e508239dd1 (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
#include "../tool.h"
#include "scrambles.h"

#define SOL_BUFFER_LEN 100000

char *solver;
int64_t size = 0;
unsigned char *buf;

bool check_one(char *actual, char *expected) {
	unsigned i;
	size_t l_actual, l_expected;

	for (l_actual = 0; actual[l_actual] != '\n'; l_actual++) ;
	l_expected = strlen(expected);
	if (l_actual > l_expected)
		return false;
	for (i = 0; i < l_expected; i++) {
		if (!strncmp(actual, &expected[i], l_actual))
			return true;
		while(expected[i] != '\n') i++;
	}
	return false;
}

bool check_all(char *actual, char *expected) {
	unsigned i, found, n_expected;
	size_t l_actual;

	l_actual = strlen(actual);
	if (l_actual != strlen(expected))
		return false;

	for (i = 0, n_expected = 0; expected[i]; i++)
		n_expected += expected[i] == '\n';

	for (i = 0, found = 0; i < l_actual; i++)
		if (i == 0 || actual[i-1] == '\n')
			found += check_one(&actual[i], expected);

	return found == n_expected;
}

void run(void) {
	int i;
	int64_t n;
	long long stats[NISSY_SIZE_SOLVE_STATS];
	char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE];

	for (i = 0; s[i].scramble[0]; i++) {
		printf("\n%d. %s\n", i, s[i].scramble);

		/* Single solution */
		if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube)
		     == -1) {
			printf("Invalid scramble\n");
			continue;
		}
		n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
		    0, 20, 1, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats);
		if (n == 0) {
			printf("Error: no solution\n");
			return;
		}
		if (check_one(sol, s[i].solutions)) {
			printf("Single solution is correct\n");
		} else {
			printf("Error!\n");
			printf("Found solution(s):\n%s", sol);
			printf("Valid solution(s):\n%s", s[i].solutions);
			return;
		}

		/* Multiple solutions */
		if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube)
		     == -1) {
			printf("Invalid scramble\n");
			continue;
		}
		n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
		    0, 20, 100, 0, 0, size, buf, SOL_BUFFER_LEN, sol, stats);
		if (check_all(sol, s[i].solutions)) {
			printf("All solutions are correct\n");
		} else {
			printf("Error!\n");
			printf("Found solution(s):\n%s", sol);
			printf("Valid solution(s):\n%s", s[i].solutions);
			return;
		}
	}

	printf("\nAll scrambles solved correctly\n");
}

int main(int argc, char **argv) {
	char filename[255], dataid[NISSY_SIZE_DATAID];

	if (argc < 2) {
		printf("Error: not enough arguments. "
		    "A solver must be given.\n");
		return 1;
	}

	solver = argv[1];
	srand(time(NULL));
	nissy_setlogger(log_stderr, NULL);

	sprintf(filename, "tables/%s", solver);
	if (getdata(solver, &buf, filename) != 0)
		return 1;

	size = nissy_solverinfo(solver, dataid);

	timerun(run);

	free(buf);
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net