1 | // $Id: Suite.cc 1516 2008-09-19 20:56:56Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library 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 GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "Suite.h" |
---|
23 | #include "environment.h" |
---|
24 | |
---|
25 | #include <algorithm> |
---|
26 | #include <cmath> |
---|
27 | #include <fstream> |
---|
28 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <string> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace test { |
---|
35 | |
---|
36 | Suite::Suite(int argc, char* argv[]) |
---|
37 | : known_issues_(0), ok_(true) |
---|
38 | { |
---|
39 | dev_null_ = new std::ofstream("/dev/null"); |
---|
40 | char* buffer=std::getenv("YAT_TEST_VERBOSE"); |
---|
41 | |
---|
42 | if ( (argc>1 && argv[1]==std::string("-v")) || |
---|
43 | (buffer && buffer == std::string("1")) ) |
---|
44 | verbose_ = true; |
---|
45 | else { |
---|
46 | verbose_ = false; |
---|
47 | if (argc>1){ |
---|
48 | std::stringstream ss(argv[0]); |
---|
49 | std::string prog; |
---|
50 | while(getline(ss, prog, '/')); |
---|
51 | if (prog.substr(0,3)=="lt-") |
---|
52 | prog = prog.substr(3); |
---|
53 | std::cout << prog << " -v : for printing extra information\n"; |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | Suite::~Suite(void) |
---|
60 | { |
---|
61 | delete dev_null_; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | bool Suite::add(bool ok) |
---|
66 | { |
---|
67 | ok_ = ok_ && ok; |
---|
68 | return ok; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | std::string filename(const std::string& path) |
---|
73 | { |
---|
74 | return abs_srcdir()+"/"+path; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | std::ostream& Suite::err(void) const |
---|
79 | { |
---|
80 | if (verbose_) |
---|
81 | return std::cerr; |
---|
82 | return *dev_null_; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | bool Suite::equal(double a, double b, unsigned long int n) |
---|
87 | { |
---|
88 | double last_error_bound = n*std::numeric_limits<double>().epsilon()* |
---|
89 | std::min(std::abs(a), std::abs(b)); |
---|
90 | if (!(std::abs(a-b) <= last_error_bound)){ |
---|
91 | err() << "Error: Comparing " << a << " and " << b |
---|
92 | << "\n Difference: " << a - b |
---|
93 | << "\n expected difference to be at most " << last_error_bound |
---|
94 | << std::endl; |
---|
95 | return false; |
---|
96 | } |
---|
97 | return true; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | bool Suite::equal_sqrt(double a, double b, unsigned long int n) |
---|
102 | { |
---|
103 | double last_error_bound = n* |
---|
104 | std::sqrt(std::numeric_limits<double>().epsilon())* |
---|
105 | std::min(std::abs(a), std::abs(b)); |
---|
106 | if (!(std::abs(a-b) <= last_error_bound)){ |
---|
107 | err() << "Error: Comparing " << a << " and " << b |
---|
108 | << "\n Difference: " << a - b |
---|
109 | << "\n expected difference to be at most " << last_error_bound |
---|
110 | << std::endl; |
---|
111 | return false; |
---|
112 | } |
---|
113 | return true; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | bool Suite::ok(void) const |
---|
118 | { |
---|
119 | return ok_; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | std::ostream& Suite::out(void) const |
---|
124 | { |
---|
125 | if (verbose_) |
---|
126 | return std::cout; |
---|
127 | return *dev_null_; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | int Suite::return_value(void) const |
---|
132 | { |
---|
133 | if (known_issues_>1) |
---|
134 | out() << known_issues_ << " known issues were detected.\n"; |
---|
135 | else if (known_issues_==1) |
---|
136 | out() << known_issues_ << " known issue was detected.\n"; |
---|
137 | if (ok()){ |
---|
138 | out() << "Test is ok.\n"; |
---|
139 | return 0; |
---|
140 | } |
---|
141 | out() << "Test failed.\n"; |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | bool Suite::xadd(bool ok) |
---|
147 | { |
---|
148 | if (!ok) |
---|
149 | ++known_issues_; |
---|
150 | else { |
---|
151 | err() << " test for a known issue returned true\n"; |
---|
152 | add(false); |
---|
153 | } |
---|
154 | return ok; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | }}} |
---|