1 // test.h -- simplistic test framework for gold unittests -*- C++ -*-
3 #ifndef GOLD_TESTSUITE_TEST_H
4 #define GOLD_TESTSUITE_TEST_H
6 namespace gold_testsuite
11 // This class handles basic test framework functionality.
17 : testname_(NULL), current_fail_(0), passes_(0), failures_(0)
20 // Return number of failures.
23 { return this->failures_; }
27 run(const char* name, bool (*pfn)(Test_report*));
29 // Get the current Test_report. This is used by the test support
33 { return Test_framework::current_report; }
36 friend class Test_report;
38 // Cause the current test to fail.
41 { ++this->current_fail_ = true; }
43 // Report an error from the current test.
45 error(const char* message);
47 // Current Test_report. This is a static variable valid while a
49 static Test_report* current_report;
51 // Current test being run.
52 const char* testname_;
53 // Whether the current test is failing.
55 // Total number of passeed tests.
57 // Total number of failed tests.
58 unsigned int failures_;
61 // An instance of this class is passed to each test function.
66 Test_report(Test_framework* tf)
70 // Mark the test as failing.
73 { this->tf_->fail(); }
77 error(const char* message)
78 { this->tf_->error(message); }
84 // This class registers a test function so that the testsuite runs it.
89 Register_test(const char* name, bool (*pfn)(Test_report*));
91 // Run all registered tests.
93 run_tests(Test_framework*);
96 // Linked list of all tests.
97 static Register_test* all_tests;
101 // Function to call. It should return true if the test passes,
102 // false if it fails.
103 bool (*pfn_)(Test_report*);
104 // Next test in linked list.
105 Register_test* next_;
108 } // End namespace gold_testsuite.
110 // These macros are for convenient use in tests.
112 // Check that a condition is true. If it is false, report a failure.
114 #define CHECK(cond) \
115 ((cond) ? 0 : (::gold_testsuite::Test_framework::report()->fail(), 0))
117 // Report an error during a test.
119 #define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg))
121 #endif // !defined(GOLD_TESTSUITE_TEST_H)