int main(int argc, char *argv[])
{
    Graph *g;
    int test_num;
    char continue_run;
    char rebuild_graph;
    char *path = (char *)malloc(256 * sizeof(char));
    
    int opt;
    while((opt = getopt(argc, argv, "p:")) > 0) {
        switch(opt) {
            case 'p':
                g = init_graph_from_file(optarg);
                break;
            default:
                break;
        }
    }

    printf("Enter test unit? y or n: ");
    scanf("%c", &continue_run);
    clean_stdin();
    while(continue_run == 'y' || continue_run == 'Y') {
        printf("Need rebuild graph? y or n: ");
        scanf("%c", &rebuild_graph);
        clean_stdin();
        if(rebuild_graph == 'y' || rebuild_graph == 'Y') {
            size_t n = 255;
            int byte_read;
            printf("Input source file path: ");
            byte_read = getline(&path, &n, stdin);
            if(byte_read == -1) {
                printf("ERROR");
            }
            if(path[byte_read-1] == '\n')
                path[byte_read-1] = '\0';
            if(g != NULL)
                destroy(g);
            g = init_graph_from_file(path);
        }

        if(g == NULL) {
            printf("The test graph is NULL\n");
            exit(EXIT_FAILURE);
        }

        printf("Input a number to choose test functions: ");
        scanf("%d", &test_num);
        clean_stdin();
        switch(test_num) {
            case 0:
                print_graph(g);
                break;
            case 1:
                test_dfs(g);
                break;
            case 2:
                test_bfs(g);
                break;
            case 3:
                test_top_sort(g);
                break;
            case 4:
                test_strong_component(g);
                break;
            case 5:
                print_reverse_graph(g);
                break;
            default:
                break;
        }

        printf("Continue test? y or n: ");
        scanf("%c", &continue_run);
        clean_stdin();
    }
    if(path != NULL)
        free(path);
    destroy(g);
    return 0;
}
Example #2
0
int main(void)
{
	struct trivial_graphr tgr;
	struct parallel_graphr pgr;
	struct full_graphr fgr;
	struct chain_graphr cgr;
	struct grid_graphr ggr1, ggr2;
	struct error_graphr egr;
	struct traversal1_graphr t1gr;
	struct agar_state *sr;
	const void *nr;
	
	plan_tests(2 * 13 + 12 + 10);

	trivial_graphr_init(&tgr);
	test_dfs(&tgr.gr, 1, 1);

	parallel_graphr_init(&pgr, 3);
	test_dfs(&pgr.gr, 1, 1, 2);

	full_graphr_init(&fgr, 5);
	test_dfs(&fgr.gr, 1, 1, 2, 3, 4, 5);
	test_dfs(&fgr.gr, 3, 3, 1, 2, 4, 5);

	chain_graphr_init(&cgr, 8);
	test_dfs(&cgr.fgr.gr, 1, 1, 2, 3, 4, 5, 6, 7, 8);
	test_dfs(&cgr.fgr.gr, 8, 8, 7, 6, 5, 4, 3, 2, 1);
	test_dfs(&cgr.fgr.gr, 5, 5, 4, 3, 2, 1, 6, 7, 8);

	grid_graphr_init(&ggr1, 3, 3, true, true, false, false);
	test_dfs(&ggr1.gr, 1, 1, 2, 3, 6, 9, 5, 8, 4, 7);
	test_dfs(&ggr1.gr, 5, 5, 6, 9, 8);
	test_dfs(&ggr1.gr, 9, 9);

	grid_graphr_init(&ggr2, 3, 3, true, true, true, true);
	test_dfs(&ggr2.gr, 1, 1, 2, 3, 6, 9, 8, 7, 4, 5);
	test_dfs(&ggr2.gr, 5, 5, 6, 9, 8, 7, 4, 1, 2, 3);
	test_dfs(&ggr2.gr, 9, 9, 8, 7, 4, 5, 6, 3, 2, 1);

	error_graphr_init(&egr);
	test_dfs(&egr.gr, 1, 1, 2);
	ok((sr = agar_dfs_new(NULL, &egr.gr)), "started error traversal");
	ok1(agar_dfs_explore(sr, int2ptr(3), &nr));
	ok(ptr2int(nr) == 3, "Expected node #3, actually #%ld", ptr2int(nr));
	ok1(agar_dfs_explore(sr, nr, &nr));
	ok(ptr2int(nr) == 4, "Expected node #4, actually #%ld", ptr2int(nr));
	ok1(!agar_dfs_explore(sr, nr, &nr));
	ok(agar_error(sr) == -1, "Error is %d (expected -1)", agar_error(sr));
	ok1(!agar_dfs_explore(sr, nr, &nr));
	tal_free(sr);
	test_dfs(&egr.gr, 1, 1, 2);

	traversal1_graphr_init(&t1gr);
	test_dfs(&t1gr.gr, 1, 1, 2, 4, 5, 3, 6);
	test_dfs(&t1gr.gr, 9, 9, 8, 6, 5, 7, 4);

	ok1((sr = agar_dfs_new(NULL, &t1gr.gr)));
	test_dfs_partial(sr, 1, 1, 2, 4, 5, 3, 6);
	test_dfs_partial(sr, 9, 9, 8, 7);
	tal_free(sr);

	ok1((sr = agar_dfs_new(NULL, &t1gr.gr)));
	test_dfs_partial(sr, 9, 9, 8, 6, 5, 7, 4);
	test_dfs_partial(sr, 1, 1, 2, 3);
	tal_free(sr);

	return exit_status();
}