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 #include "test_list"
17 };
18
19 :(before "End Globals")
20 bool Run_tests = false;
21 bool Passed = true;
22 long Num_failures = 0;
23
24 :(before "End Includes")
25 #define CHECK(X) \
26 if (Passed && !(X)) { \
27 ¦ cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \
28 ¦ Passed = false; \
29 ¦ return; \
30 }
31
32 #define CHECK_EQ(X, Y) \
33 if (Passed && (X) != (Y)) { \
34 ¦ cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \
35 ¦ cerr << " got " << (X) << '\n'; \
36 ¦ Passed = false; \
37 ¦ return; \
38 }
39
40 :(before "End Setup")
41 Passed = true;
42
43 :(before "End Commandline Parsing")
44 if (argc > 1 && is_equal(argv[1], "test")) {
45 Run_tests = true; --argc; ++argv;
46 }
47
48 :(before "End Main")
49 if (Run_tests) {
50
51
52
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 }
60 cerr << '\n';
61
62 if (Num_failures > 0) {
63 ¦ cerr << Num_failures << " failure"
64 ¦ ¦ ¦ ¦<< (Num_failures > 1 ? "s" : "")
65 ¦ ¦ ¦ ¦<< '\n';
66 ¦ return 1;
67 }
68 return 0;
69 }
70
71 :(code)
72 void run_test(size_t i) {
73 if (i >= sizeof(Tests)/sizeof(Tests[0])) {
74 ¦ cerr << "no test " << i << '\n';
75 ¦ return;
76 }
77 setup();
78
79 (*Tests[i])();
80
81 teardown();
82 if (Passed) cerr << '.';
83 else ++Num_failures;
84 }
85
86 bool is_integer(const string& s) {
87 return s.find_first_not_of("0123456789-") == string::npos
88 ¦ ¦ && s.find_first_of("0123456789") != string::npos
89 ¦ ¦ && s.find('-', 1) == string::npos;
90 }
91
92 int to_integer(string n) {
93 char* end = NULL;
94
95 int result = strtoll(n.c_str(), &end, 0);
96 if (*end != '\0') cerr << "tried to convert " << n << " to number\n";
97 assert(*end == '\0');
98 return result;
99 }
100
101 void test_is_integer() {
102 CHECK(is_integer("1234"));
103 CHECK(is_integer("-1"));
104 CHECK(!is_integer("234.0"));
105 CHECK(is_integer("-567"));
106 CHECK(!is_integer("89-0"));
107 CHECK(!is_integer("-"));
108 CHECK(!is_integer("1e3"));
109 }
110
111 :(before "End Includes")
112 #include <stdlib.h>