Added a testsuite. More support for COPY relocations.
[external/binutils.git] / gold / testsuite / test.h
1 // test.h -- simplistic test framework for gold unittests -*- C++ -*-
2
3 #ifndef GOLD_TESTSUITE_TEST_H
4 #define GOLD_TESTSUITE_TEST_H
5
6 namespace gold_testsuite
7 {
8
9 class Test_report;
10
11 // This class handles basic test framework functionality.
12
13 class Test_framework
14 {
15  public:
16   Test_framework()
17     : testname_(NULL), current_fail_(0), passes_(0), failures_(0)
18   { }
19
20   // Return number of failures.
21   unsigned int
22   failures() const
23   { return this->failures_; }
24
25   // Run a test.
26   void
27   run(const char* name, bool (*pfn)(Test_report*));
28
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; }
34
35  private:
36   friend class Test_report;
37
38   // Cause the current test to fail.
39   void
40   fail()
41   { ++this->current_fail_ = true; }
42
43   // Report an error from the current test.
44   void
45   error(const char* message);
46
47   // Current Test_report.  This is a static variable valid while a
48   // test is being run.
49   static Test_report* current_report;
50
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_;
59 };
60
61 // An instance of this class is passed to each test function.
62
63 class Test_report
64 {
65 public:
66   Test_report(Test_framework* tf)
67     : tf_(tf)
68   { }
69
70   // Mark the test as failing.
71   void
72   fail()
73   { this->tf_->fail(); }
74
75   // Report an error.
76   void
77   error(const char* message)
78   { this->tf_->error(message); }
79
80 private:
81   Test_framework* tf_;
82 };
83
84 // This class registers a test function so that the testsuite runs it.
85
86 class Register_test
87 {
88  public: 
89   Register_test(const char* name, bool (*pfn)(Test_report*));
90
91   // Run all registered tests.
92   static void
93   run_tests(Test_framework*);
94
95  private:
96   // Linked list of all tests.
97   static Register_test* all_tests;
98
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_;
106 };
107
108 } // End namespace gold_testsuite.
109
110 // These macros are for convenient use in tests.
111
112 // Check that a condition is true.  If it is false, report a failure.
113
114 #define CHECK(cond) \
115   ((cond) ? 0 : (::gold_testsuite::Test_framework::report()->fail(), 0))
116
117 // Report an error during a test.
118
119 #define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg))
120
121 #endif // !defined(GOLD_TESTSUITE_TEST_H)