| 1 | // test.cc -- simplistic test framework for gold. |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008 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 | |
| 23 | #include "gold.h" |
| 24 | |
| 25 | #include <cstdio> |
| 26 | |
| 27 | #include "test.h" |
| 28 | |
| 29 | namespace gold_testsuite |
| 30 | { |
| 31 | |
| 32 | // Test_framework methods. |
| 33 | |
| 34 | // The current test being run. |
| 35 | |
| 36 | Test_report* Test_framework::current_report; |
| 37 | |
| 38 | // Run a test. |
| 39 | |
| 40 | void |
| 41 | Test_framework::run(const char *name, bool (*pfn)(Test_report*)) |
| 42 | { |
| 43 | this->testname_ = name; |
| 44 | this->current_fail_ = false; |
| 45 | |
| 46 | Test_report tr(this); |
| 47 | Test_framework::current_report = &tr; |
| 48 | |
| 49 | if ((*pfn)(&tr) && !this->current_fail_) |
| 50 | { |
| 51 | printf("PASS: %s\n", name); |
| 52 | ++this->passes_; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | printf("FAIL: %s\n", name); |
| 57 | ++this->failures_; |
| 58 | } |
| 59 | |
| 60 | Test_framework::current_report = NULL; |
| 61 | this->testname_ = NULL; |
| 62 | } |
| 63 | |
| 64 | // Report a failure. |
| 65 | |
| 66 | void |
| 67 | Test_framework::fail(const char* filename, int lineno) |
| 68 | { |
| 69 | printf("FAIL: %s: %s: %d\n", this->testname_, filename, lineno); |
| 70 | this->current_fail_ = true; |
| 71 | } |
| 72 | |
| 73 | // Let a test report an error. |
| 74 | |
| 75 | void |
| 76 | Test_framework::error(const char* message) |
| 77 | { |
| 78 | printf("ERROR: %s: %s\n", this->testname_, message); |
| 79 | this->current_fail_ = true; |
| 80 | } |
| 81 | |
| 82 | // Register_test methods. |
| 83 | |
| 84 | // Linked list of all registered tests. |
| 85 | |
| 86 | Register_test* Register_test::all_tests; |
| 87 | |
| 88 | // Register a test. |
| 89 | |
| 90 | Register_test::Register_test(const char* name, bool (*pfn)(Test_report*)) |
| 91 | : name_(name), pfn_(pfn), next_(Register_test::all_tests) |
| 92 | { |
| 93 | Register_test::all_tests = this; |
| 94 | } |
| 95 | |
| 96 | // Run all registered tests. |
| 97 | |
| 98 | void |
| 99 | Register_test::run_tests(Test_framework* tf) |
| 100 | { |
| 101 | for (Register_test* p = Register_test::all_tests; |
| 102 | p != NULL; |
| 103 | p = p->next_) |
| 104 | tf->run(p->name_, p->pfn_); |
| 105 | } |
| 106 | |
| 107 | } // End namespace gold_testsuite. |