:(before "End Types")
typedef void (*test_fn)(void);
:(before "Globals")
const test_fn Tests[] = {
#include "test_list"
};
:(before "End Globals")
bool Run_tests = false;
bool Passed = true;
long Num_failures = 0;
:(before "End Includes")
#define CHECK(X) \
if (Passed && !(X)) { \
cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \
Passed = false; \
return; \
}
#define CHECK_EQ(X, Y) \
if (Passed && (X) != (Y)) { \
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 (size_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 1;
}
return 0;
}
:(code)
void run_test(size_t i) {
if (i >= sizeof(Tests)/sizeof(Tests[0])) {
cerr << "no test " << i << '\n';
return;
}
setup();
(*Tests[i])();
teardown();
if (Passed) cerr << '.';
else ++Num_failures;
}
bool is_integer(const string& s) {
return s.find_first_not_of("0123456789-") == string::npos
&& s.find_first_of("0123456789") != string::npos
&& s.find('-', 1) == string::npos;
}
int to_integer(string n) {
char* end = NULL;
int result = strtoll(n.c_str(), &end, 0);
if (*end != '\0') cerr << "tried to convert " << n << " to number\n";
assert(*end == '\0');
return result;
}
void test_is_integer() {
CHECK(is_integer("1234"));
CHECK(is_integer("-1"));
CHECK(!is_integer("234.0"));
CHECK(is_integer("-567"));
CHECK(!is_integer("89-0"));
CHECK(!is_integer("-"));
CHECK(!is_integer("1e3"));
}
:(before "End Includes")
#include <stdlib.h>