1 | // $Id: Suite.cc 1249 2008-03-20 18:12:20Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "Suite.h" |
---|
25 | #include "environment.h" |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <cmath> |
---|
29 | #include <fstream> |
---|
30 | #include <iostream> |
---|
31 | #include <sstream> |
---|
32 | #include <string> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace test { |
---|
37 | |
---|
38 | Suite::Suite(int argc, char* argv[]) |
---|
39 | : ok_(true) |
---|
40 | { |
---|
41 | dev_null_ = new std::ofstream("/dev/null"); |
---|
42 | if (argc>1 && argv[1]==std::string("-v")) |
---|
43 | verbose_ = true; |
---|
44 | else { |
---|
45 | verbose_ = false; |
---|
46 | if (argc>1){ |
---|
47 | std::stringstream ss(argv[0]); |
---|
48 | std::string prog; |
---|
49 | while(getline(ss, prog, '/')); |
---|
50 | if (prog.substr(0,3)=="lt-") |
---|
51 | prog = prog.substr(3); |
---|
52 | std::cout << prog << " -v : for printing extra information\n"; |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | Suite::~Suite(void) |
---|
59 | { |
---|
60 | delete dev_null_; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void Suite::add(bool ok) |
---|
65 | { |
---|
66 | ok_ = ok_ && ok; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | std::string filename(const std::string& path) |
---|
71 | { |
---|
72 | return abs_srcdir()+"/"+path; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | std::ostream& Suite::err(void) const |
---|
77 | { |
---|
78 | if (verbose_) |
---|
79 | return std::cerr; |
---|
80 | return *dev_null_; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | bool Suite::equal(double a, double b, unsigned long int n) |
---|
85 | { |
---|
86 | double last_error_bound = n*std::numeric_limits<double>().epsilon()* |
---|
87 | std::min(std::abs(a), std::abs(b)); |
---|
88 | if (!(std::abs(a-b) <= last_error_bound)){ |
---|
89 | err() << "Error: Comparing " << a << " and " << b |
---|
90 | << "\n Difference: " << a - b |
---|
91 | << "\n expected difference to be at most " << last_error_bound |
---|
92 | << std::endl; |
---|
93 | return false; |
---|
94 | } |
---|
95 | return true; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | bool Suite::equal_sqrt(double a, double b, unsigned long int n) |
---|
100 | { |
---|
101 | double last_error_bound = n* |
---|
102 | std::sqrt(std::numeric_limits<double>().epsilon())* |
---|
103 | std::min(std::abs(a), std::abs(b)); |
---|
104 | if (!(std::abs(a-b) <= last_error_bound)){ |
---|
105 | err() << "Error: Comparing " << a << " and " << b |
---|
106 | << "\n Difference: " << a - b |
---|
107 | << "\n expected difference to be at most " << last_error_bound |
---|
108 | << std::endl; |
---|
109 | return false; |
---|
110 | } |
---|
111 | return true; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | bool Suite::ok(void) const |
---|
116 | { |
---|
117 | return ok_; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | std::ostream& Suite::out(void) const |
---|
122 | { |
---|
123 | if (verbose_) |
---|
124 | return std::cout; |
---|
125 | return *dev_null_; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | int Suite::return_value(void) const |
---|
130 | { |
---|
131 | if (ok()){ |
---|
132 | out() << "Test is ok.\n"; |
---|
133 | return 0; |
---|
134 | } |
---|
135 | out() << "Test failed.\n"; |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | }}} |
---|