/* * This is not part of your eventual program, but it allows you to test the * functioning of your graph.c, as well as to test that it works without * symbol.c and token.c (which is required). Compile together your graph.c, * this file, and the original emalloc.c, using the original graph.h and * emalloc.h. Then simply run this program. * * It doesn't test your colouring algorithm, just the basic vertex and edge * management. */ #include #include #include "graph.h" int main() { extern int test1(); extern void test2(); if (test1() == 0) { test2(); printf("No errors detected\n"); } return 0; } int test1() { GRAPH g[2]; char name[4]; int i; g[0] = new_graph(6); g[1] = new_graph(6); name[1] = name[3] = '\0'; for (i = 0; i < 6; i++) { name[0] = 'a' + i; name[2] = 'q' + i; if (add_vertex(g[0], name) != i || add_vertex(g[1], name+2) != i) { printf("Unexpected index number from add_vertex().\n"); printf("This might not be an error with your program, but it might prevent this\n"); printf("test program from testing it properly.\n\n"); } } if (is_adjacent(g[0], 3, 4) || is_adjacent(g[1], 3, 4)) { printf("It looks like a new graph already contains some edges. Can't proceed.\n"); return 1; } add_edge(g[0], 3, 4); if (!is_adjacent(g[0], 3, 4)) { printf("add_edge() doesn't seem to work. Can't proceed.\n"); return 1; } if (is_adjacent(g[1], 3, 4)) printf("BAD ERROR! add_edge() on one graph adds it to another!\n\n"); free_graph(g[0]); free_graph(g[1]); return 0; } void test2() { GRAPH g; char name[2]; int actual[6][6]; long perm; int i, j, k; for (perm = 0; perm < 2000000000L; perm += 12345) { g = new_graph(6); name[1] = '\0'; for (i = 0; i < 6; i++) { name[0] = 'a' + i; add_vertex(g, name); } for (j = 0; j < 6; j++) { for (k = 0; k < 6; k++) { if (perm & (1L << (j * 6 + k))) { add_edge(g, j, k); actual[j][k] = 1; } else { actual[j][k] = 0; } } } for (j = 0; j < 6; j++) { for (k = 0; k < 6; k++) { if (is_adjacent(g, j, k) != actual[j][k]) { printf("add_edge(%d, %d) %s called, " "but is_adjacent(%d, %d) returns %d\n", j, k, actual[j][k] ? "was" : "wasn't", j, k, is_adjacent(g, j, k)); printf("There may be other errors too\n"); exit(1); } } } free_graph(g); } }