Commit | Line | Data |
---|---|---|
5a6f7e2d ILT |
1 | // test.h -- simplistic test framework for gold unittests -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
5a6f7e2d ILT |
23 | #ifndef GOLD_TESTSUITE_TEST_H |
24 | #define GOLD_TESTSUITE_TEST_H | |
25 | ||
26 | namespace gold_testsuite | |
27 | { | |
28 | ||
29 | class Test_report; | |
30 | ||
31 | // This class handles basic test framework functionality. | |
32 | ||
33 | class Test_framework | |
34 | { | |
35 | public: | |
36 | Test_framework() | |
37 | : testname_(NULL), current_fail_(0), passes_(0), failures_(0) | |
38 | { } | |
39 | ||
40 | // Return number of failures. | |
41 | unsigned int | |
42 | failures() const | |
43 | { return this->failures_; } | |
44 | ||
45 | // Run a test. | |
46 | void | |
47 | run(const char* name, bool (*pfn)(Test_report*)); | |
48 | ||
49 | // Get the current Test_report. This is used by the test support | |
50 | // macros. | |
51 | static Test_report* | |
52 | report() | |
53 | { return Test_framework::current_report; } | |
54 | ||
55 | private: | |
56 | friend class Test_report; | |
57 | ||
58 | // Cause the current test to fail. | |
59 | void | |
60 | fail() | |
61 | { ++this->current_fail_ = true; } | |
62 | ||
63 | // Report an error from the current test. | |
64 | void | |
65 | error(const char* message); | |
66 | ||
67 | // Current Test_report. This is a static variable valid while a | |
68 | // test is being run. | |
69 | static Test_report* current_report; | |
70 | ||
71 | // Current test being run. | |
72 | const char* testname_; | |
73 | // Whether the current test is failing. | |
74 | bool current_fail_; | |
75 | // Total number of passeed tests. | |
76 | unsigned int passes_; | |
77 | // Total number of failed tests. | |
78 | unsigned int failures_; | |
79 | }; | |
80 | ||
81 | // An instance of this class is passed to each test function. | |
82 | ||
83 | class Test_report | |
84 | { | |
85 | public: | |
86 | Test_report(Test_framework* tf) | |
87 | : tf_(tf) | |
88 | { } | |
89 | ||
90 | // Mark the test as failing. | |
91 | void | |
92 | fail() | |
93 | { this->tf_->fail(); } | |
94 | ||
95 | // Report an error. | |
96 | void | |
97 | error(const char* message) | |
98 | { this->tf_->error(message); } | |
99 | ||
100 | private: | |
101 | Test_framework* tf_; | |
102 | }; | |
103 | ||
104 | // This class registers a test function so that the testsuite runs it. | |
105 | ||
106 | class Register_test | |
107 | { | |
108 | public: | |
109 | Register_test(const char* name, bool (*pfn)(Test_report*)); | |
110 | ||
111 | // Run all registered tests. | |
112 | static void | |
113 | run_tests(Test_framework*); | |
114 | ||
115 | private: | |
116 | // Linked list of all tests. | |
117 | static Register_test* all_tests; | |
118 | ||
119 | // Test name. | |
120 | const char* name_; | |
121 | // Function to call. It should return true if the test passes, | |
122 | // false if it fails. | |
123 | bool (*pfn_)(Test_report*); | |
124 | // Next test in linked list. | |
125 | Register_test* next_; | |
126 | }; | |
127 | ||
128 | } // End namespace gold_testsuite. | |
129 | ||
130 | // These macros are for convenient use in tests. | |
131 | ||
132 | // Check that a condition is true. If it is false, report a failure. | |
133 | ||
134 | #define CHECK(cond) \ | |
135 | ((cond) ? 0 : (::gold_testsuite::Test_framework::report()->fail(), 0)) | |
136 | ||
137 | // Report an error during a test. | |
138 | ||
139 | #define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg)) | |
140 | ||
141 | #endif // !defined(GOLD_TESTSUITE_TEST_H) |