1230363Sdas// test.h -- simplistic test framework for gold unittests -*- C++ -*- 2230363Sdas 3230363Sdas// Copyright (C) 2006-2020 Free Software Foundation, Inc. 4230363Sdas// Written by Ian Lance Taylor <iant@google.com>. 5230363Sdas 6230363Sdas// This file is part of gold. 7230363Sdas 8230363Sdas// This program is free software; you can redistribute it and/or modify 9230363Sdas// it under the terms of the GNU General Public License as published by 10230363Sdas// the Free Software Foundation; either version 3 of the License, or 11230363Sdas// (at your option) any later version. 12230363Sdas 13230363Sdas// This program is distributed in the hope that it will be useful, 14230363Sdas// but WITHOUT ANY WARRANTY; without even the implied warranty of 15230363Sdas// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16230363Sdas// GNU General Public License for more details. 17230363Sdas 18230363Sdas// You should have received a copy of the GNU General Public License 19230363Sdas// along with this program; if not, write to the Free Software 20230363Sdas// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21230363Sdas// MA 02110-1301, USA. 22230363Sdas 23230363Sdas#ifndef GOLD_TESTSUITE_TEST_H 24230363Sdas#define GOLD_TESTSUITE_TEST_H 25230363Sdas 26230363Sdasnamespace gold_testsuite 27230363Sdas{ 28 29class Test_report; 30 31// This class handles basic test framework functionality. 32 33class 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(const char* filename, int lineno); 61 62 // Report an error from the current test. 63 void 64 error(const char* message); 65 66 // Current Test_report. This is a static variable valid while a 67 // test is being run. 68 static Test_report* current_report; 69 70 // Current test being run. 71 const char* testname_; 72 // Whether the current test is failing. 73 bool current_fail_; 74 // Total number of passeed tests. 75 unsigned int passes_; 76 // Total number of failed tests. 77 unsigned int failures_; 78}; 79 80// An instance of this class is passed to each test function. 81 82class Test_report 83{ 84public: 85 Test_report(Test_framework* tf) 86 : tf_(tf) 87 { } 88 89 // Mark the test as failing. 90 void 91 fail(const char* filename, int lineno) 92 { this->tf_->fail(filename, lineno); } 93 94 // Report an error. 95 void 96 error(const char* message) 97 { this->tf_->error(message); } 98 99private: 100 Test_framework* tf_; 101}; 102 103// This class registers a test function so that the testsuite runs it. 104 105class Register_test 106{ 107 public: 108 Register_test(const char* name, bool (*pfn)(Test_report*)); 109 110 // Run all registered tests. 111 static void 112 run_tests(Test_framework*); 113 114 private: 115 // Linked list of all tests. 116 static Register_test* all_tests; 117 118 // Test name. 119 const char* name_; 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_; 125}; 126 127} // End namespace gold_testsuite. 128 129// These macros are for convenient use in tests. 130 131// Check that a condition is true. If it is false, report a failure. 132 133#define CHECK(cond) \ 134 ((void) \ 135 ((cond) \ 136 ? 0 \ 137 : (::gold_testsuite::Test_framework::report()->fail(__FILE__, \ 138 __LINE__), \ 139 0))) 140 141// Report an error during a test. 142 143#define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg)) 144 145#endif // !defined(GOLD_TESTSUITE_TEST_H) 146