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.
This commit is contained in:
Simon Tatham
2024-09-27 11:29:56 +01:00
parent 2ac951e70a
commit 5044e00588

View File

@ -1,3 +1,5 @@
#include <assert.h>
#include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
@ -41,6 +43,46 @@ static struct graph *graph_random(
return graph; 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) static void graph_free(struct graph *graph)
{ {
sfree(graph->adj); sfree(graph->adj);
@ -186,10 +228,20 @@ static void usage(void)
"by comparing to a simple implementation"); "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) int main(int argc, char **argv)
{ {
const char *random_seed = "12345"; const char *random_seed = "12345";
int iterations = 10000; int iterations = 10000;
size_t i;
random_state *rs; random_state *rs;
progname = argv[0]; 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("Seeding with \"%s\"\n", random_seed);
printf("Testing %d random graphs\n", iterations); printf("Testing %d random graphs\n", iterations);