nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

solvetest.h (3157B)


      1 #include "tool.h"
      2 
      3 #define SOL_BUFFER_LEN   100000
      4 #define MAX_SOLUTIONS    1000
      5 #define MAX_SOLUTION_LEN 1000
      6 
      7 char *solver;
      8 int64_t size = 0;
      9 unsigned char *buf;
     10 
     11 bool check_one(
     12 	const char *astr,
     13 	const char *aname,
     14 	const char *bstr,
     15 	const char *bname,
     16 	bool exp[static MAXSOLUTIONS]
     17 ) {
     18 	size_t i, j, nn, lb;
     19 	char b[MAX_SOLUTION_LEN];
     20 
     21 	lb = strlen(bstr);
     22 	for (i = 0, j = 0, nn = 0; i < lb; j++, i = ++nn) {
     23 		while (bstr[nn] != '\n') nn++;
     24 		strncpy(b, &bstr[i], nn-i);
     25 		b[nn-i] = '\0';
     26 		if (nissy_comparemoves(astr, b) == NISSY_COMPARE_MOVES_EQUAL) {
     27 			if (exp[j]) {
     28 				printf("%s solution %s found twice\n",
     29 				    aname, b);
     30 				return false;
     31 			}
     32 			exp[j] = true;
     33 			return true;
     34 		}
     35 	}
     36 
     37 	printf("%s solution %s not found in %s\n", aname, astr, bname);
     38 	return false;
     39 }
     40 
     41 size_t count_lines(const char *str) {
     42 	size_t i, ret;
     43 
     44 	for (i = 0, ret = 0; str[i] != '\0'; i++)
     45 		ret += str[i] == '\n';
     46 
     47 	return ret;
     48 }
     49 
     50 bool find_strlist(
     51 	const char *astr,
     52 	const char *aname,
     53 	const char *bstr,
     54 	const char *bname
     55 )
     56 {
     57 	size_t i, nn, la;
     58 	char a[MAX_SOLUTION_LEN];
     59 	bool exp[MAX_SOLUTIONS];
     60 
     61 	memset(exp, false, MAX_SOLUTIONS * sizeof(bool));
     62 
     63 	la = strlen(astr);
     64 	for (i = 0, nn = 0; i < la; i = ++nn) {
     65 		while (astr[nn] != '\n') nn++;
     66 		strncpy(a, &astr[i], nn-i);
     67 		a[nn-i] = '\0';
     68 		if (!check_one(a, aname, bstr, bname, exp))
     69 			return false;
     70 	}
     71 
     72 	return true;
     73 }
     74 
     75 bool check_all(const char *actual, const char *expected) {
     76 	size_t n_actual, n_expected;
     77 
     78 	n_actual = count_lines(actual);
     79 	n_expected = count_lines(expected);
     80 	if (n_actual < n_expected)
     81 		printf("Found less solutions than expected\n");
     82 	if (n_actual > n_expected)
     83 		printf("Found more solutions than expected\n");
     84 
     85 	return n_actual > n_expected ?
     86 	    find_strlist(actual, "actual", expected, "expected") :
     87 	    find_strlist(expected, "expected", actual, "actual");
     88 }
     89 
     90 void run(void) {
     91 	int i;
     92 	long long stats[NISSY_SIZE_SOLVE_STATS];
     93 	char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE];
     94 
     95 	for (i = 0; s[i].scramble[0]; i++) {
     96 		printf("\n%d. %s\n", i, s[i].scramble);
     97 
     98 		if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube)
     99 		     == NISSY_ERROR_INVALID_MOVES) {
    100 			printf("Invalid scramble\n");
    101 			exit(1);
    102 		}
    103 
    104 		nissy_solve(cube, solver,
    105 		    NISSFLAG, MINMOVES, MAXMOVES, MAXSOLUTIONS, OPTIMAL,
    106 		    0, size, buf, SOL_BUFFER_LEN, sol, stats,
    107 		    NULL, NULL);
    108 
    109 		if (check_all(sol, s[i].solutions)) {
    110 			printf("All solutions are correct\n");
    111 		} else {
    112 			printf("Error!\n");
    113 			printf("Found solution(s):\n%s", sol);
    114 			printf("Valid solution(s):\n%s", s[i].solutions);
    115 			exit(1);
    116 		}
    117 	}
    118 
    119 	printf("\nAll scrambles solved correctly\n");
    120 }
    121 
    122 int main(int argc, char **argv) {
    123 	char filename[7+NISSY_SIZE_DATAID], dataid[NISSY_SIZE_DATAID];
    124 
    125 	if (argc < 2) {
    126 		solver = SOLVER;
    127 		printf("No solver given, using default %s\n", solver);
    128 	} else {
    129 		solver = argv[1];
    130 		printf("Using user-specified solver %s\n", solver);
    131 	}
    132 
    133 	srand(time(NULL));
    134 	nissy_setlogger(log_stderr, NULL);
    135 
    136 	size = nissy_solverinfo(solver, dataid);
    137 	sprintf(filename, "tables/%s", dataid);
    138 	if (getdata(solver, &buf, filename) != 0)
    139 		return 1;
    140 
    141 	timerun(run);
    142 
    143 	free(buf);
    144 	return 0;
    145 }