2015-04-22 20:00:01 +00:00
|
|
|
//: A simple test harness. To create new tests define functions starting with
|
|
|
|
//: 'test_'. To run all tests so defined, run:
|
2016-08-10 16:12:11 +00:00
|
|
|
//: $ mu test
|
2015-04-22 20:00:01 +00:00
|
|
|
//:
|
|
|
|
//: So far it seems tasteful for layers to never ever reach back to modify
|
|
|
|
//: previously-defined tests. Every test is a contract once written, and should
|
|
|
|
//: pass as-is if it is included, regardless of how much later layers change
|
|
|
|
//: the program. Avoid writing 'temporary' tests that only work with some
|
|
|
|
//: subsets of the program.
|
2015-02-18 00:57:37 +00:00
|
|
|
|
|
|
|
:(before "End Types")
|
|
|
|
typedef void (*test_fn)(void);
|
2016-08-29 00:08:01 +00:00
|
|
|
:(before "End Constants")
|
2015-02-18 00:57:37 +00:00
|
|
|
const test_fn Tests[] = {
|
|
|
|
#include "test_list" // auto-generated; see makefile
|
|
|
|
};
|
|
|
|
|
2016-08-28 22:21:12 +00:00
|
|
|
:(before "End Globals")
|
2015-04-22 20:29:02 +00:00
|
|
|
bool Run_tests = false;
|
2015-04-22 19:43:38 +00:00
|
|
|
bool Passed = true; // set this to false inside any test to indicate failure
|
2015-02-18 00:57:37 +00:00
|
|
|
long Num_failures = 0;
|
|
|
|
|
2016-08-28 22:21:12 +00:00
|
|
|
:(before "End Includes")
|
2015-02-18 00:57:37 +00:00
|
|
|
#define CHECK(X) \
|
|
|
|
if (!(X)) { \
|
|
|
|
++Num_failures; \
|
2015-05-12 15:47:06 +00:00
|
|
|
cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \
|
2015-02-18 00:57:37 +00:00
|
|
|
Passed = false; \
|
2015-04-22 19:43:38 +00:00
|
|
|
return; /* Currently we stop at the very first failure. */ \
|
2015-02-18 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_EQ(X, Y) \
|
|
|
|
if ((X) != (Y)) { \
|
|
|
|
++Num_failures; \
|
2015-05-12 15:47:06 +00:00
|
|
|
cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \
|
2015-02-18 00:57:37 +00:00
|
|
|
cerr << " got " << (X) << '\n'; /* BEWARE: multiple eval */ \
|
|
|
|
Passed = false; \
|
2015-04-22 19:43:38 +00:00
|
|
|
return; /* Currently we stop at the very first failure. */ \
|
2015-02-18 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:31:52 +00:00
|
|
|
:(before "End Setup")
|
|
|
|
Passed = true;
|
|
|
|
|
2015-04-22 20:29:02 +00:00
|
|
|
:(before "End Commandline Parsing")
|
2015-04-22 19:17:16 +00:00
|
|
|
if (argc > 1 && is_equal(argv[1], "test")) {
|
2015-04-22 20:29:02 +00:00
|
|
|
Run_tests = true; --argc; ++argv; // shift 'test' out of commandline args
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Main")
|
|
|
|
if (Run_tests) {
|
|
|
|
// Test Runs
|
|
|
|
// we run some tests and then exit; assume no state need be maintained afterward
|
2015-02-18 00:57:37 +00:00
|
|
|
|
2015-04-22 20:29:02 +00:00
|
|
|
// End Test Run Initialization
|
2015-04-22 20:35:47 +00:00
|
|
|
time_t t; time(&t);
|
|
|
|
cerr << "C tests: " << ctime(&t);
|
2015-05-17 09:22:41 +00:00
|
|
|
for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
|
2015-08-29 06:21:48 +00:00
|
|
|
//? cerr << i << '\n';
|
2015-04-22 20:35:47 +00:00
|
|
|
run_test(i);
|
|
|
|
}
|
2015-04-22 20:29:02 +00:00
|
|
|
cerr << '\n';
|
2016-08-14 00:17:24 +00:00
|
|
|
// End Tests
|
2015-05-13 00:40:04 +00:00
|
|
|
if (Num_failures > 0) {
|
2015-04-22 20:29:02 +00:00
|
|
|
cerr << Num_failures << " failure"
|
|
|
|
<< (Num_failures > 1 ? "s" : "")
|
|
|
|
<< '\n';
|
2015-05-13 00:40:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-04-22 20:29:02 +00:00
|
|
|
return 0;
|
2015-04-22 19:17:16 +00:00
|
|
|
}
|
2015-04-22 18:49:24 +00:00
|
|
|
|
2015-02-18 00:57:37 +00:00
|
|
|
:(code)
|
2015-05-17 09:22:41 +00:00
|
|
|
void run_test(size_t i) {
|
2015-04-22 17:09:09 +00:00
|
|
|
if (i >= sizeof(Tests)/sizeof(Tests[0])) {
|
|
|
|
cerr << "no test " << i << '\n';
|
|
|
|
return;
|
|
|
|
}
|
2015-03-17 16:08:09 +00:00
|
|
|
setup();
|
|
|
|
// End Test Setup
|
|
|
|
(*Tests[i])();
|
2016-02-25 15:31:20 +00:00
|
|
|
// End Test Teardown
|
2015-04-25 04:37:36 +00:00
|
|
|
teardown();
|
2016-02-25 15:31:20 +00:00
|
|
|
if (Passed) cerr << '.';
|
2015-02-18 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 04:24:21 +00:00
|
|
|
bool is_integer(const string& s) {
|
2016-02-25 02:07:54 +00:00
|
|
|
return s.find_first_not_of("0123456789-") == string::npos // no other characters
|
|
|
|
&& s.find_first_of("0123456789") != string::npos // at least one digit
|
|
|
|
&& s.find('-', 1) == string::npos; // '-' only at first position
|
2015-04-22 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 03:26:47 +00:00
|
|
|
int to_integer(string n) {
|
2015-03-17 16:08:09 +00:00
|
|
|
char* end = NULL;
|
2015-05-17 04:24:47 +00:00
|
|
|
// safe because string.c_str() is guaranteed to be null-terminated
|
2016-03-14 03:26:47 +00:00
|
|
|
int result = strtoll(n.c_str(), &end, /*any base*/0);
|
2015-05-23 19:54:06 +00:00
|
|
|
if (*end != '\0') cerr << "tried to convert " << n << " to number\n";
|
2015-03-17 16:08:09 +00:00
|
|
|
assert(*end == '\0');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-10 18:15:28 +00:00
|
|
|
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")); // not supported
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:57:37 +00:00
|
|
|
:(before "End Includes")
|
2016-06-02 17:40:06 +00:00
|
|
|
#include <cstdlib>
|