https://github.com/akkartik/mu/blob/master/002test.cc
1
2
3
4
5
6
7
8
9
10
11 :(before "End Types")
12 typedef void (*test_fn)(void);
13 :(before "Globals")
14
15 const test_fn Tests[] = {
16
17 };
18
19 :(before "End Globals")
20 bool Run_tests = false;
21 bool Passed = true;
22
23 :(before "End Includes")
24
25 if (Passed && !(X)) { \
26 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " <<
27 Passed = false; \
28 return; \
29 }
30
31
32 if (Passed && (X) != (Y)) { \
33 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " <<
34 cerr << " got " << (X) << '\n'; \
35 Passed = false; \
36 return; \
37 }
38
39 :(before "End Reset")
40 Passed = true;
41
42 :(before "End Commandline Parsing")
43 if (argc > 1 && is_equal(argv[1], "test")) {
44 Run_tests = true; --argc; ++argv;
45 }
46
47 :(before "End Main")
48 if (Run_tests) {
49
50
51
52 long num_failures = 0;
53
54 time_t t; time(&t);
55 cerr << "C tests: " << ctime(&t);
56 for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
57
58 run_test(i);
59 if (Passed) cerr << '.';
60 else ++num_failures;
61 }
62 cerr << '\n';
63
64 if (num_failures > 0) {
65 cerr << num_failures << " failure"
66 << (num_failures > 1 ? "s" : "")
67 << '\n';
68 return 1;
69 }
70 return 0;
71 }
72
73 :(code)
74 void run_test(size_t i) {
75 if (i >= sizeof(Tests)/sizeof(Tests[0])) {
76 cerr << "no test " << i << '\n';
77 return;
78 }
79 reset();
80
81 (*Tests[i])();
82
83 }
84
85
86 :(before "Globals")
87
88 const string Test_names[] = {
89
90 };
91 :(after "Test Runs")
92 string maybe_single_test_to_run = argv[argc-1];
93 if (!starts_with(maybe_single_test_to_run, "test_"))
94 maybe_single_test_to_run.insert(0, "test_");
95 for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
96 if (Test_names[i] == maybe_single_test_to_run) {
97 run_test(i);
98 if (Passed) cerr << ".\n";
99 return 0;
100 }
101 }
102
103 :(before "End Includes")
104