diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-15 10:56:38 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-15 10:56:38 +0100 |
| commit | 7e999327af767a9f668ae51f1a102598a2ee12c0 (patch) | |
| tree | 26b4fa7696a4b17a1d239e544a32feeae4a777ad /tools/400_solvetest/solve_test.c | |
| parent | cb800102a14fb6923546b9d036dbf4e6a5b7542a (diff) | |
| download | nissy-core-7e999327af767a9f668ae51f1a102598a2ee12c0.tar.gz nissy-core-7e999327af767a9f668ae51f1a102598a2ee12c0.zip | |
Fixed constant that I rarely used until today
Diffstat (limited to '')
| -rw-r--r-- | tools/400_solvetest/solve_test.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/tools/400_solvetest/solve_test.c b/tools/400_solvetest/solve_test.c new file mode 100644 index 0000000..d19e1f3 --- /dev/null +++ b/tools/400_solvetest/solve_test.c | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | #include "../tool.h" | ||
| 2 | |||
| 3 | #define SOL_BUFFER_LEN 10000 | ||
| 4 | |||
| 5 | typedef struct { | ||
| 6 | char *scramble; | ||
| 7 | char *solutions; | ||
| 8 | } scramblesol_t; | ||
| 9 | |||
| 10 | char *solver; | ||
| 11 | int64_t size = 0; | ||
| 12 | char *buf; | ||
| 13 | |||
| 14 | scramblesol_t s[] = { | ||
| 15 | [0] = { | ||
| 16 | .scramble = "R' D R U R' D' R U'", | ||
| 17 | .solutions = "U R' D R U' R' D' R\n" | ||
| 18 | "B' D2 B U' B' D2 B U\n", | ||
| 19 | }, | ||
| 20 | { | ||
| 21 | .scramble = "", /* End-of-list signal */ | ||
| 22 | } | ||
| 23 | }; | ||
| 24 | |||
| 25 | bool check_one(char *actual, char *expected) { | ||
| 26 | unsigned i; | ||
| 27 | size_t l_actual, l_expected; | ||
| 28 | |||
| 29 | l_actual = strlen(actual); | ||
| 30 | l_expected = strlen(expected); | ||
| 31 | if (l_actual > l_expected) | ||
| 32 | return false; | ||
| 33 | for (i = 0; i < l_expected; i++) { | ||
| 34 | if (!strncmp(actual, &expected[i], l_actual)) | ||
| 35 | return true; | ||
| 36 | while(expected[i] != '\n') i++; | ||
| 37 | } | ||
| 38 | return false; | ||
| 39 | } | ||
| 40 | |||
| 41 | bool check_all(char *actual, char *expected) { | ||
| 42 | unsigned i, found, n_expected; | ||
| 43 | size_t l_actual; | ||
| 44 | |||
| 45 | if (strlen(actual) != strlen(expected)) | ||
| 46 | return false; | ||
| 47 | |||
| 48 | for (i = 0, n_expected = 0; expected[i]; i++) | ||
| 49 | n_expected += expected[i] == '\n'; | ||
| 50 | |||
| 51 | l_actual = strlen(actual); | ||
| 52 | for (i = 0, found = 0; i < l_actual; i++) | ||
| 53 | found += check_one(&actual[i], expected); | ||
| 54 | |||
| 55 | return found == n_expected; | ||
| 56 | } | ||
| 57 | |||
| 58 | void run(void) { | ||
| 59 | int i; | ||
| 60 | int64_t n; | ||
| 61 | long long stats[NISSY_SIZE_SOLVE_STATS]; | ||
| 62 | char sol[SOL_BUFFER_LEN], cube[22]; | ||
| 63 | |||
| 64 | for (i = 0; s[i].scramble[0]; i++) { | ||
| 65 | printf("\n%d. %s\n", i, s[i].scramble); | ||
| 66 | |||
| 67 | /* Single solution */ | ||
| 68 | if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube) | ||
| 69 | == -1) { | ||
| 70 | printf("Invalid scramble\n"); | ||
| 71 | continue; | ||
| 72 | } | ||
| 73 | n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, | ||
| 74 | 0, 20, 1, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats); | ||
| 75 | if (n == 0) { | ||
| 76 | printf("Error! No solution found for scramble %s\n", | ||
| 77 | s[i].scramble); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | if (check_one(sol, s[i].solutions)) { | ||
| 81 | printf("Single solution for scramble %d is correct\n", i); | ||
| 82 | } else { | ||
| 83 | printf("Error! Scramble %d: %s\n", i, s[i].scramble); | ||
| 84 | printf("Found solution(s):\n%s", sol); | ||
| 85 | printf("Valid solution(s):\n%s", s[i].solutions); | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | /* Multiple solutions */ | ||
| 90 | if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube) | ||
| 91 | == -1) { | ||
| 92 | printf("Invalid scramble\n"); | ||
| 93 | continue; | ||
| 94 | } | ||
| 95 | n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, | ||
| 96 | 0, 20, 100, 0, 0, size, buf, SOL_BUFFER_LEN, sol, stats); | ||
| 97 | if (check_all(sol, s[i].solutions)) { | ||
| 98 | printf("All solutions for scramble %d are correct\n", i); | ||
| 99 | } else { | ||
| 100 | printf("Error! Scramble %s\n", s[i].scramble); | ||
| 101 | printf("Found solution(s):\n%s", sol); | ||
| 102 | printf("Valid solution(s):\n%s", s[i].solutions); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | printf("\nAll scrambles solved correctly\n"); | ||
| 108 | } | ||
| 109 | |||
| 110 | int main(int argc, char **argv) { | ||
| 111 | char filename[255]; | ||
| 112 | |||
| 113 | if (argc < 2) { | ||
| 114 | printf("Error: not enough arguments. " | ||
| 115 | "A solver must be given.\n"); | ||
| 116 | return 1; | ||
| 117 | } | ||
| 118 | |||
| 119 | solver = argv[1]; | ||
| 120 | srand(time(NULL)); | ||
| 121 | nissy_setlogger(log_stderr); | ||
| 122 | |||
| 123 | sprintf(filename, "tables/%s", solver); | ||
| 124 | if (getdata(solver, &buf, filename) != 0) | ||
| 125 | return 1; | ||
| 126 | |||
| 127 | size = nissy_datasize(solver); | ||
| 128 | |||
| 129 | timerun(run); | ||
| 130 | |||
| 131 | free(buf); | ||
| 132 | return 0; | ||
| 133 | } | ||
