diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-31 08:38:37 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-31 08:38:37 +0200 |
| commit | e2b154c40acaac4e7a7b3e379ada0404519c3750 (patch) | |
| tree | 2ddca6533b03180eee1cd199d1c5a56586211a65 /tools/solvetest.h | |
| parent | 36c317ebd1cf1dba8a8cbec494f4d3971e6cefd3 (diff) | |
| download | nissy-core-e2b154c40acaac4e7a7b3e379ada0404519c3750.tar.gz nissy-core-e2b154c40acaac4e7a7b3e379ada0404519c3750.zip | |
Added solvetest for EO (broken for now)
Diffstat (limited to 'tools/solvetest.h')
| -rw-r--r-- | tools/solvetest.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tools/solvetest.h b/tools/solvetest.h new file mode 100644 index 0000000..4a36736 --- /dev/null +++ b/tools/solvetest.h | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | #include "tool.h" | ||
| 2 | |||
| 3 | #define SOL_BUFFER_LEN 100000 | ||
| 4 | |||
| 5 | char *solver; | ||
| 6 | int64_t size = 0; | ||
| 7 | unsigned char *buf; | ||
| 8 | |||
| 9 | bool check_one(char *actual, char *expected) { | ||
| 10 | unsigned i; | ||
| 11 | size_t l_actual, l_expected; | ||
| 12 | |||
| 13 | for (l_actual = 0; actual[l_actual] != '\n'; l_actual++) ; | ||
| 14 | l_expected = strlen(expected); | ||
| 15 | if (l_actual > l_expected) | ||
| 16 | return false; | ||
| 17 | for (i = 0; i < l_expected; i++) { | ||
| 18 | if (!strncmp(actual, &expected[i], l_actual)) | ||
| 19 | return true; | ||
| 20 | while(expected[i] != '\n') i++; | ||
| 21 | } | ||
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | bool check_all(char *actual, char *expected) { | ||
| 26 | unsigned i, found, n_expected; | ||
| 27 | size_t l_actual; | ||
| 28 | |||
| 29 | l_actual = strlen(actual); | ||
| 30 | if (l_actual != strlen(expected)) | ||
| 31 | return false; | ||
| 32 | |||
| 33 | for (i = 0, n_expected = 0; expected[i]; i++) | ||
| 34 | n_expected += expected[i] == '\n'; | ||
| 35 | |||
| 36 | for (i = 0, found = 0; i < l_actual; i++) | ||
| 37 | if (i == 0 || actual[i-1] == '\n') | ||
| 38 | found += check_one(&actual[i], expected); | ||
| 39 | |||
| 40 | return found == n_expected; | ||
| 41 | } | ||
| 42 | |||
| 43 | void run(void) { | ||
| 44 | int i; | ||
| 45 | int64_t n; | ||
| 46 | long long stats[NISSY_SIZE_SOLVE_STATS]; | ||
| 47 | char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE]; | ||
| 48 | |||
| 49 | for (i = 0; s[i].scramble[0]; i++) { | ||
| 50 | printf("\n%d. %s\n", i, s[i].scramble); | ||
| 51 | |||
| 52 | /* Multiple solutions */ | ||
| 53 | if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube) | ||
| 54 | == -1) { | ||
| 55 | printf("Invalid scramble\n"); | ||
| 56 | continue; | ||
| 57 | } | ||
| 58 | n = nissy_solve(cube, solver, | ||
| 59 | NISSFLAG, MINMOVES, MAXMOVES, MAXSOLUTIONS, OPTIMAL, | ||
| 60 | 0, size, buf, SOL_BUFFER_LEN, sol, stats, | ||
| 61 | NULL, NULL); | ||
| 62 | if (check_all(sol, s[i].solutions)) { | ||
| 63 | printf("All solutions are correct\n"); | ||
| 64 | } else { | ||
| 65 | printf("Error!\n"); | ||
| 66 | printf("Found solution(s):\n%s", sol); | ||
| 67 | printf("Valid solution(s):\n%s", s[i].solutions); | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | printf("\nAll scrambles solved correctly\n"); | ||
| 73 | } | ||
| 74 | |||
| 75 | int main(int argc, char **argv) { | ||
| 76 | char filename[255], dataid[NISSY_SIZE_DATAID]; | ||
| 77 | |||
| 78 | if (argc < 2) { | ||
| 79 | printf("Error: not enough arguments. " | ||
| 80 | "A solver must be given.\n"); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | solver = argv[1]; | ||
| 85 | srand(time(NULL)); | ||
| 86 | nissy_setlogger(log_stderr, NULL); | ||
| 87 | |||
| 88 | sprintf(filename, "tables/%s", solver); | ||
| 89 | if (getdata(solver, &buf, filename) != 0) | ||
| 90 | return 1; | ||
| 91 | |||
| 92 | size = nissy_solverinfo(solver, dataid); | ||
| 93 | |||
| 94 | timerun(run); | ||
| 95 | |||
| 96 | free(buf); | ||
| 97 | return 0; | ||
| 98 | } | ||
