1 // test.h -- simplistic test framework for gold unittests -*- C++ -*-
3 // Copyright (C) 2006-2017 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_TESTSUITE_TEST_H
24 #define GOLD_TESTSUITE_TEST_H
26 namespace gold_testsuite
31 // This class handles basic test framework functionality.
37 : testname_(NULL), current_fail_(0), passes_(0), failures_(0)
40 // Return number of failures.
43 { return this->failures_; }
47 run(const char* name, bool (*pfn)(Test_report*));
49 // Get the current Test_report. This is used by the test support
53 { return Test_framework::current_report; }
56 friend class Test_report;
58 // Cause the current test to fail.
60 fail(const char* filename, int lineno);
62 // Report an error from the current test.
64 error(const char* message);
66 // Current Test_report. This is a static variable valid while a
68 static Test_report* current_report;
70 // Current test being run.
71 const char* testname_;
72 // Whether the current test is failing.
74 // Total number of passeed tests.
76 // Total number of failed tests.
77 unsigned int failures_;
80 // An instance of this class is passed to each test function.
85 Test_report(Test_framework* tf)
89 // Mark the test as failing.
91 fail(const char* filename, int lineno)
92 { this->tf_->fail(filename, lineno); }
96 error(const char* message)
97 { this->tf_->error(message); }
103 // This class registers a test function so that the testsuite runs it.
108 Register_test(const char* name, bool (*pfn)(Test_report*));
110 // Run all registered tests.
112 run_tests(Test_framework*);
115 // Linked list of all tests.
116 static Register_test* all_tests;
120 // Function to call. It should return true if the test passes,
121 // false if it fails.
122 bool (*pfn_)(Test_report*);
123 // Next test in linked list.
124 Register_test* next_;
127 } // End namespace gold_testsuite.
129 // These macros are for convenient use in tests.
131 // Check that a condition is true. If it is false, report a failure.
133 #define CHECK(cond) \
137 : (::gold_testsuite::Test_framework::report()->fail(__FILE__, \
141 // Report an error during a test.
143 #define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg))
145 #endif // !defined(GOLD_TESTSUITE_TEST_H)