nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

test.c (1651B)


      1 #include <stdio.h>
      2 
      3 #include "alg_tests.h"
      4 #include "coord_tests.h"
      5 #include "fst_tests.h"
      6 
      7 static bool run_test(Test *);
      8 static bool run_suite(TestSuite *);
      9 
     10 static bool
     11 run_test(Test *test)
     12 {
     13 	int i;
     14 
     15 	printf("Running test %s...", test->name);
     16 	for (i = 0; test->cases[i] != NULL; i++) {
     17 		if (!test->t(test->cases[i])) {
     18 			printf("FAILED!\n");
     19 			return false;
     20 		}
     21 	}
     22 
     23 	printf("OK\n");
     24 	return true;
     25 }
     26 
     27 static bool
     28 run_suite(TestSuite *suite)
     29 {
     30 	int i;
     31 
     32 	if (suite->setup != NULL)
     33 		suite->setup();
     34 	for (i = 0; suite->tests[i] != NULL; i++)
     35 		if(!run_test(suite->tests[i]))
     36 			return false;
     37 	if (suite->teardown != NULL)
     38 		suite->teardown();
     39 
     40 	return true;
     41 }
     42 
     43 static bool
     44 module_in_args(char *module, int argc, char *argv[])
     45 {
     46 	for (int i = 0; i < argc; i++)
     47 		if (!strcmp(module, argv[i]))
     48 			return true;
     49 	return false;
     50 }
     51 
     52 int main(int argc, char *argv[]) {
     53 	/* TODO: init should be in testsuites */
     54 	init_env();
     55 	init_trans();
     56 	/**************************************/
     57 
     58 	TestModule alg   = { .name = "alg",   .suites = alg_suites };
     59 	TestModule fst   = { .name = "fst",   .suites = fst_suites };
     60 	TestModule coord = { .name = "coord", .suites = coord_suites };
     61 	TestModule *modules[999] = {
     62 		&alg,
     63 		&fst,
     64 		&coord,
     65 		NULL
     66 	};
     67 
     68 
     69 	bool all = argc == 1 || module_in_args("all", argc, argv);
     70 	int count = 0;
     71 	for (int i = 0; modules[i] != NULL; i++) {
     72 		if (all || module_in_args(modules[i]->name, argc, argv)) {
     73 			for (int j = 0; modules[i]->suites[j] != NULL; j++) {
     74 				if (!run_suite(modules[i]->suites[j])) {
     75 					return 1;
     76 				} else {
     77 					count++;
     78 				}
     79 			}
     80 		}
     81 	}
     82 
     83 	printf("All tests passed (%d test suites).\n", count);
     84 	return 0;
     85 }