aboutsummaryrefslogtreecommitdiff
path: root/divvy.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2023-04-02 10:42:42 +0100
committerSimon Tatham <anakin@pobox.com>2023-04-02 14:35:12 +0100
commit83244294f56cbf4c6ac03564489d1775054bbcfc (patch)
tree731721d49f303640449affc8cc2057fa3c9117d9 /divvy.c
parent71e1776094aa9240e9772b7fbc99dd5e2f4e1acb (diff)
downloadpuzzles-83244294f56cbf4c6ac03564489d1775054bbcfc.zip
puzzles-83244294f56cbf4c6ac03564489d1775054bbcfc.tar.gz
puzzles-83244294f56cbf4c6ac03564489d1775054bbcfc.tar.bz2
puzzles-83244294f56cbf4c6ac03564489d1775054bbcfc.tar.xz
Move other test main()s out of library source files.
Having stated the principle in the previous commit, I should apply it consistently. A source file linked into the Puzzles library of common support code should not also define a main() under ifdef. This commit only goes as far as the _library_ support modules. It would be a much bigger job to do the same for all the actual _puzzles_ that have test main()s or standalone-solver main()s. And it's not necessary, because modifying one of those source files only triggers a rebuild of _one_ puzzle, not absolutely everything. (Not to mention that it's quite likely the puzzle and the test main() will need to be modified in conjunction anyway.) As in the previous commit, this has required exposing a few internal API functions as global, and maybe editing them a bit. In particular, the one-shot internal function that divvy_rectangle() loops on until it succeeds is now exposed as divvy_rectangle_attempt(), which means the test program doesn't have to condition a failure counter into the real function. I've thrown away penrose-vector-test completely, because that didn't look like a test program with any ongoing use at all - it was surely vestigial, while James was getting the vector representation up and running in the first place.
Diffstat (limited to 'divvy.c')
-rw-r--r--divvy.c122
1 files changed, 2 insertions, 120 deletions
diff --git a/divvy.c b/divvy.c
index 5b8ee27..021956f 100644
--- a/divvy.c
+++ b/divvy.c
@@ -260,7 +260,7 @@ static bool addremcommon(int w, int h, int x, int y, int *own, int val)
* In both of the above suggested use cases, the user would
* probably want w==h==k, but that isn't a requirement.
*/
-static int *divvy_internal(int w, int h, int k, random_state *rs)
+int *divvy_rectangle_attempt(int w, int h, int k, random_state *rs)
{
int *order, *queue, *tmp, *own, *sizes, *addable, *retdsf;
bool *removable;
@@ -652,131 +652,13 @@ static int *divvy_internal(int w, int h, int k, random_state *rs)
return retdsf;
}
-#ifdef TESTMODE
-static int fail_counter = 0;
-#endif
-
int *divvy_rectangle(int w, int h, int k, random_state *rs)
{
int *ret;
do {
- ret = divvy_internal(w, h, k, rs);
-
-#ifdef TESTMODE
- if (!ret)
- fail_counter++;
-#endif
-
+ ret = divvy_rectangle_attempt(w, h, k, rs);
} while (!ret);
return ret;
}
-
-#ifdef TESTMODE
-
-/*
- * gcc -g -O0 -DTESTMODE -I.. -o divvy divvy.c ../random.c ../malloc.c ../dsf.c ../misc.c ../nullfe.c
- *
- * or to debug
- *
- * gcc -g -O0 -DDIVVY_DIAGNOSTICS -DTESTMODE -I.. -o divvy divvy.c ../random.c ../malloc.c ../dsf.c ../misc.c ../nullfe.c
- */
-
-int main(int argc, char **argv)
-{
- int *dsf;
- int i;
- int w = 9, h = 4, k = 6, tries = 100;
- random_state *rs;
-
- rs = random_new("123456", 6);
-
- if (argc > 1)
- w = atoi(argv[1]);
- if (argc > 2)
- h = atoi(argv[2]);
- if (argc > 3)
- k = atoi(argv[3]);
- if (argc > 4)
- tries = atoi(argv[4]);
-
- for (i = 0; i < tries; i++) {
- int x, y;
-
- dsf = divvy_rectangle(w, h, k, rs);
- assert(dsf);
-
- for (y = 0; y <= 2*h; y++) {
- for (x = 0; x <= 2*w; x++) {
- int miny = y/2 - 1 /*, maxy = y/2 */;
- int minx = x/2 - 1 /*, maxx = x/2 */;
- int classes[4], tx, ty;
- for (ty = 0; ty < 2; ty++)
- for (tx = 0; tx < 2; tx++) {
- int cx = minx+tx, cy = miny+ty;
- if (cx < 0 || cx >= w || cy < 0 || cy >= h)
- classes[ty*2+tx] = -1;
- else
- classes[ty*2+tx] = dsf_canonify(dsf, cy*w+cx);
- }
- switch (y%2 * 2 + x%2) {
- case 0: /* corner */
- /*
- * Cases for the corner:
- *
- * - if all four surrounding squares belong
- * to the same omino, we print a space.
- *
- * - if the top two are the same and the
- * bottom two are the same, we print a
- * horizontal line.
- *
- * - if the left two are the same and the
- * right two are the same, we print a
- * vertical line.
- *
- * - otherwise, we print a cross.
- */
- if (classes[0] == classes[1] &&
- classes[1] == classes[2] &&
- classes[2] == classes[3])
- printf(" ");
- else if (classes[0] == classes[1] &&
- classes[2] == classes[3])
- printf("-");
- else if (classes[0] == classes[2] &&
- classes[1] == classes[3])
- printf("|");
- else
- printf("+");
- break;
- case 1: /* horiz edge */
- if (classes[1] == classes[3])
- printf(" ");
- else
- printf("--");
- break;
- case 2: /* vert edge */
- if (classes[2] == classes[3])
- printf(" ");
- else
- printf("|");
- break;
- case 3: /* square centre */
- printf(" ");
- break;
- }
- }
- printf("\n");
- }
- printf("\n");
- sfree(dsf);
- }
-
- printf("%d retries needed for %d successes\n", fail_counter, tries);
-
- return 0;
-}
-
-#endif