merge from gcc
[binutils.git] / gold / testsuite / test.h
blobe0556e267316d4401d5cb28fa491a06628b38258
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
9 class Test_report;
11 // This class handles basic test framework functionality.
13 class Test_framework
15 public:
16 Test_framework()
17 : testname_(NULL), current_fail_(0), passes_(0), failures_(0)
18 { }
20 // Return number of failures.
21 unsigned int
22 failures() const
23 { return this->failures_; }
25 // Run a test.
26 void
27 run(const char* name, bool (*pfn)(Test_report*));
29 // Get the current Test_report. This is used by the test support
30 // macros.
31 static Test_report*
32 report()
33 { return Test_framework::current_report; }
35 private:
36 friend class Test_report;
38 // Cause the current test to fail.
39 void
40 fail()
41 { ++this->current_fail_ = true; }
43 // Report an error from the current test.
44 void
45 error(const char* message);
47 // Current Test_report. This is a static variable valid while a
48 // test is being run.
49 static Test_report* current_report;
51 // Current test being run.
52 const char* testname_;
53 // Whether the current test is failing.
54 bool current_fail_;
55 // Total number of passeed tests.
56 unsigned int passes_;
57 // Total number of failed tests.
58 unsigned int failures_;
61 // An instance of this class is passed to each test function.
63 class Test_report
65 public:
66 Test_report(Test_framework* tf)
67 : tf_(tf)
68 { }
70 // Mark the test as failing.
71 void
72 fail()
73 { this->tf_->fail(); }
75 // Report an error.
76 void
77 error(const char* message)
78 { this->tf_->error(message); }
80 private:
81 Test_framework* tf_;
84 // This class registers a test function so that the testsuite runs it.
86 class Register_test
88 public:
89 Register_test(const char* name, bool (*pfn)(Test_report*));
91 // Run all registered tests.
92 static void
93 run_tests(Test_framework*);
95 private:
96 // Linked list of all tests.
97 static Register_test* all_tests;
99 // Test name.
100 const char* name_;
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)