1 //: A simple test harness. To create new tests, define functions starting with
  2 //: 'test_'. To run all tests so defined, run:
  3 //:   $ ./mu test
  4 //:
  5 //: Every layer should include tests, and can reach into previous layers.
  6 //: However, it seems like a good idea never to reach into tests from previous
  7 //: layers. Every test should be a contract that always passes as originally
  8 //: written, regardless of any later layers. Avoid writing 'temporary' tests
  9 //: that are only meant to work until some layer.
 10 
 11 :(before "End Types")
 12 typedef void (*test_fn)(void);
 13 :(before "Globals")
 14 // move a global ahead into types that we can't generate an extern declaration for
 15 const test_fn Tests[] = {
 16   #include "test_list"  // auto-generated; see 'build' script
 17 };
 18 
 19 :(before "End Globals")
 20 bool Run_tests = false;
 21 bool Passed = true;  // set this to false inside any test to indicate failure
 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;  /* Currently we stop at the very first failure. */ \
 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';  /* BEWARE: multiple eval */ \
 36   ¦ Passed = false; \
 37   ¦ return;  /* Currently we stop at the very first failure. */ \
 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;  // shift 'test' out of commandline args
 46 }
 47 
 48 :(before "End Main")
 49 if (Run_tests) {
 50   // Test Runs
 51   // we run some tests and then exit; assume no state need be maintained afterward
 52 
 53   // End Test Run Initialization
 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 //?     cerr << "running .build/test_list line " << (i+1) << '\n';
 58   ¦ run_test(i);
 59   }
 60   cerr << '\n';
 61   // End Tests
 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   // End Test Setup
 79   (*Tests[i])();
 80   // End Test Teardown
 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  // no other characters
 88   ¦ ¦ && s.find_first_of("0123456789") != string::npos  // at least one digit
 89   ¦ ¦ && s.find('-', 1) == string::npos;  // '-' only at first position
 90 }
 91 
 92 int to_integer(string n) {
 93   char* end = NULL;
 94   // safe because string.c_str() is guaranteed to be null-terminated
 95   int result = strtoll(n.c_str(), &end, /*any base*/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"));  // not supported
109 }
110 
111 :(before "End Includes")
112 #include <stdlib.h>