:(before "End Types")
typedef void (*test_fn)(void);
:(before "End Globals")
const test_fn Tests[] = {
#include "test_list"
};
bool Run_tests = false;
bool Passed = true;
long Num_failures = 0;
#define CHECK(X) \
if (!(X)) { \
++Num_failures; \
cerr << "\nF " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \
Passed = false; \
return; \
}
#define CHECK_EQ(X, Y) \
if ((X) != (Y)) { \
++Num_failures; \
cerr << "\nF " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \
cerr << " got " << (X) << '\n'; \
Passed = false; \
return; \
}
:(before "End Setup")
Passed = true;
:(before "End Commandline Parsing")
if (argc > 1 && is_equal(argv[1], "test")) {
Run_tests = true; --argc; ++argv;
}
:(before "End Main")
if (Run_tests) {
time_t t; time(&t);
cerr << "C tests: " << ctime(&t);
for (index_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
run_test(i);
}
cerr << '\n';
if (Num_failures > 0)
cerr << Num_failures << " failure"
<< (Num_failures > 1 ? "s" : "")
<< '\n';
return 0;
}
:(code)
void run_test(index_t i) {
if (i >= sizeof(Tests)/sizeof(Tests[0])) {
cerr << "no test " << i << '\n';
return;
}
setup();
(*Tests[i])();
teardown();
if (Passed) cerr << ".";
}
bool is_number(const string& s) {
return s.find_first_not_of("0123456789-.") == string::npos;
}
long long int to_int(string n) {
char* end = NULL;
long long int result = strtoll(n.c_str(), &end, 0);
assert(*end == '\0');
return result;
}
:(before "End Includes")
#include<cstdlib>