Add licensing text to every source file.
[external/binutils.git] / gold / testsuite / test.cc
1 // test.cc -- simplistic test framework for gold.
2
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
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 // Let a test report an error.
65
66 void
67 Test_framework::error(const char* message)
68 {
69   printf("ERROR: %s: %s\n", this->testname_, message);
70   this->fail();
71 }
72
73 // Register_test methods.
74
75 // Linked list of all registered tests.
76
77 Register_test* Register_test::all_tests;
78
79 // Register a test.
80
81 Register_test::Register_test(const char* name, bool (*pfn)(Test_report*))
82   : name_(name), pfn_(pfn), next_(Register_test::all_tests)
83 {
84   Register_test::all_tests = this;
85 }
86
87 // Run all registered tests.
88
89 void
90 Register_test::run_tests(Test_framework* tf)
91 {
92   for (Register_test* p = Register_test::all_tests;
93        p != NULL;
94        p = p->next_)
95     tf->run(p->name_, p->pfn_);
96 }
97
98 } // End namespace gold_testsuite.