2017-03-02 12:41:24 +00:00
|
|
|
//: A simple test harness. To create new tests, define functions starting with
|
2015-04-22 20:00:01 +00:00
|
|
|
//: 'test_'. To run all tests so defined, run:
|
2020-01-02 09:41:55 +00:00
|
|
|
//: $ ./bootstrap test
|
2015-04-22 20:00:01 +00:00
|
|
|
//:
|
2016-10-22 23:27:36 +00:00
|
|
|
//: Every layer should include tests, and can reach into previous layers.
|
|
|
|
//: However, it seems like a good idea never to reach into tests from previous
|
|
|
|
//: layers. Every test should be a contract that always passes as originally
|
|
|
|
//: written, regardless of any later layers. Avoid writing 'temporary' tests
|
|
|
|
//: that are only meant to work until some layer.
|
2015-02-18 00:57:37 +00:00
|
|
|
|
|
|
|
:(before "End Types")
|
|
|
|
typedef void (*test_fn)(void);
|
2016-08-29 01:12:03 +00:00
|
|
|
:(before "Globals")
|
|
|
|
// move a global ahead into types that we can't generate an extern declaration for
|
2015-02-18 00:57:37 +00:00
|
|
|
const test_fn Tests[] = {
|
2018-06-06 15:56:50 +00:00
|
|
|
#include "test_list" // auto-generated; see 'build*' scripts
|
2015-02-18 00:57:37 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-08-28 22:21:12 +00:00
|
|
|
:(before "End Includes")
|
2015-02-18 00:57:37 +00:00
|
|
|
#define CHECK(X) \
|
2016-09-02 07:42:43 +00:00
|
|
|
if (Passed && !(X)) { \
|
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) \
|
2016-09-02 07:42:43 +00:00
|
|
|
if (Passed && (X) != (Y)) { \
|
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
|
|
|
}
|
|
|
|
|
2017-07-09 21:34:17 +00:00
|
|
|
:(before "End Reset")
|
2015-05-04 17:31:52 +00:00
|
|
|
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
|
|
|
|
2017-07-09 20:55:32 +00:00
|
|
|
long num_failures = 0;
|
2015-04-22 20:29:02 +00:00
|
|
|
// End Test Run Initialization
|
2016-10-20 05:10:35 +00:00
|
|
|
time_t t; time(&t);
|
2015-04-22 20:35:47 +00:00
|
|
|
cerr << "C tests: " << ctime(&t);
|
2016-10-20 05:10:35 +00:00
|
|
|
for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
|
2018-06-08 06:29:31 +00:00
|
|
|
//? cerr << "running " << Test_names[i] << '\n';
|
2015-04-22 20:35:47 +00:00
|
|
|
run_test(i);
|
2017-07-09 20:55:32 +00:00
|
|
|
if (Passed) cerr << '.';
|
|
|
|
else ++num_failures;
|
2015-04-22 20:35:47 +00:00
|
|
|
}
|
2015-04-22 20:29:02 +00:00
|
|
|
cerr << '\n';
|
2016-08-14 00:17:24 +00:00
|
|
|
// End Tests
|
2017-07-09 20:55:32 +00:00
|
|
|
if (num_failures > 0) {
|
|
|
|
cerr << num_failures << " failure"
|
|
|
|
<< (num_failures > 1 ? "s" : "")
|
2015-04-22 20:29:02 +00:00
|
|
|
<< '\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
|
|
|
|
2019-07-27 23:01:55 +00:00
|
|
|
:(after "End Main")
|
|
|
|
//: Raise other unrecognized sub-commands as errors.
|
2020-01-02 09:41:55 +00:00
|
|
|
//: We couldn't do this until now because we want `./bootstrap test` to always
|
2019-07-27 23:01:55 +00:00
|
|
|
//: succeed, no matter how many layers are included in the build.
|
|
|
|
cerr << "nothing to do\n";
|
|
|
|
return 1;
|
|
|
|
|
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;
|
|
|
|
}
|
2017-07-09 21:25:48 +00:00
|
|
|
reset();
|
2015-03-17 16:08:09 +00:00
|
|
|
// End Test Setup
|
|
|
|
(*Tests[i])();
|
2016-02-25 15:31:20 +00:00
|
|
|
// End Test Teardown
|
2015-02-18 00:57:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 16:02:40 +00:00
|
|
|
//: Convenience: run a single test
|
2018-06-06 17:09:34 +00:00
|
|
|
:(before "Globals")
|
|
|
|
// Names for each element of the 'Tests' global, respectively.
|
|
|
|
const string Test_names[] = {
|
|
|
|
#include "test_name_list" // auto-generated; see 'build*' scripts
|
|
|
|
};
|
2018-06-06 16:02:40 +00:00
|
|
|
:(after "Test Runs")
|
|
|
|
string maybe_single_test_to_run = argv[argc-1];
|
|
|
|
for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
|
|
|
|
if (Test_names[i] == maybe_single_test_to_run) {
|
|
|
|
run_test(i);
|
|
|
|
if (Passed) cerr << ".\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 04:45:24 +00:00
|
|
|
//: A pending test that also serves to put our test harness through its paces.
|
|
|
|
|
|
|
|
:(code)
|
|
|
|
void test_is_equal() {
|
|
|
|
CHECK(is_equal("", ""));
|
|
|
|
CHECK(!is_equal("", "foo"));
|
|
|
|
CHECK(!is_equal("foo", ""));
|
|
|
|
CHECK(!is_equal("f", "bar"));
|
|
|
|
CHECK(!is_equal("bar", "f"));
|
|
|
|
CHECK(!is_equal("bar", "ba"));
|
|
|
|
CHECK(!is_equal("ba", "bar"));
|
|
|
|
CHECK(is_equal("bar", "bar"));
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:57:37 +00:00
|
|
|
:(before "End Includes")
|
2016-10-02 06:14:17 +00:00
|
|
|
#include <stdlib.h>
|