From 5044e00588e70e82fe72bc2a255eeb4adacb3fa4 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 27 Sep 2024 11:29:56 +0100 Subject: [PATCH] findloop-test: add some fixed test cases. This increases the chance of detecting a bug in a simple fixed case before a complicated random one that's harder to debug. It also allows adding regression tests for bugs found later, if any. --- auxiliary/findloop-test.c | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/auxiliary/findloop-test.c b/auxiliary/findloop-test.c index 530567a..5b630aa 100644 --- a/auxiliary/findloop-test.c +++ b/auxiliary/findloop-test.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -41,6 +43,46 @@ static struct graph *graph_random( return graph; } +static struct graph *graph_from_untangle(const char *p) +{ + struct graph *graph = snew(struct graph); + + graph->nvertices = atoi(p); + while (p && isdigit((unsigned char)*p)) + p++; + + assert(*p == ':' && "Expected ':' separating vertex count from edges"); + p++; + + graph->adj = snewn(graph->nvertices * graph->nvertices, bool); + memset(graph->adj, 0, graph->nvertices * graph->nvertices * sizeof(bool)); + + while (*p) { + int u, v; + + u = atoi(p); + while (p && isdigit((unsigned char)*p)) + p++; + + assert(*p == '-' && "Expected '-' separating two vertex numbers"); + p++; + + v = atoi(p); + while (p && isdigit((unsigned char)*p)) + p++; + + if (*p) { + assert(*p == ',' && "Expected ',' separating two edges"); + p++; + } + + graph->adj[u * graph->nvertices + v] = 1; + graph->adj[v * graph->nvertices + u] = 1; + } + + return graph; +} + static void graph_free(struct graph *graph) { sfree(graph->adj); @@ -186,10 +228,20 @@ static void usage(void) "by comparing to a simple implementation"); } +static const char *const testgraphs[] = { + "3:", /* empty graph: no edges at all */ + "3:0-1,1-2", /* line graph: everything is a bridge */ + "3:0-1,0-2,1-2", /* cycle graph: nothing is a bridge */ + "6:0-1,0-2,1-2,2-3,3-4,3-5,4-5", /* simplest dumb-bell graph */ + "5:0-1,0-2,0-3,0-4,1-2,1-4,2-3,2-4,3-4", /* complete graph */ + "20:0-1,0-2,1-2,3-4,4-5,6-7,7-8,7-9,8-9,8-10,10-11,11-12,11-13,11-18,12-14,13-14,13-17,14-15,15-16,17-18,17-19,18-19", /* complicated-ish example */ +}; + int main(int argc, char **argv) { const char *random_seed = "12345"; int iterations = 10000; + size_t i; random_state *rs; progname = argv[0]; @@ -211,6 +263,14 @@ int main(int argc, char **argv) } } + printf("Testing %d fixed test cases\n", (int)lenof(testgraphs)); + + for (i = 0; i < lenof(testgraphs); i++) { + struct graph *graph = graph_from_untangle(testgraphs[i]); + test_findloop(graph); + graph_free(graph); + } + printf("Seeding with \"%s\"\n", random_seed); printf("Testing %d random graphs\n", iterations);