1215 |
07 Mar 08 |
peter |
// $Id$ |
1215 |
07 Mar 08 |
peter |
2 |
|
1215 |
07 Mar 08 |
peter |
3 |
/* |
2121 |
13 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2011, 2012, 2014, 2015, 2019, 2020, 2021, 2022, 2023 Peter Johansson |
1215 |
07 Mar 08 |
peter |
6 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1215 |
07 Mar 08 |
peter |
8 |
|
1215 |
07 Mar 08 |
peter |
The yat library is free software; you can redistribute it and/or |
1215 |
07 Mar 08 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
1215 |
07 Mar 08 |
peter |
License, or (at your option) any later version. |
1215 |
07 Mar 08 |
peter |
13 |
|
1215 |
07 Mar 08 |
peter |
The yat library is distributed in the hope that it will be useful, |
1215 |
07 Mar 08 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1215 |
07 Mar 08 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1215 |
07 Mar 08 |
peter |
General Public License for more details. |
1215 |
07 Mar 08 |
peter |
18 |
|
1215 |
07 Mar 08 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1215 |
07 Mar 08 |
peter |
21 |
*/ |
1215 |
07 Mar 08 |
peter |
22 |
|
2455 |
29 Mar 11 |
peter |
23 |
#include <config.h> |
2455 |
29 Mar 11 |
peter |
24 |
|
1215 |
07 Mar 08 |
peter |
25 |
#include "Suite.h" |
2894 |
10 Dec 12 |
peter |
26 |
#include "yat/utility/utility.h" |
1215 |
07 Mar 08 |
peter |
27 |
|
1239 |
16 Mar 08 |
peter |
28 |
#include <algorithm> |
3846 |
13 Sep 19 |
peter |
29 |
#include <cerrno> |
1215 |
07 Mar 08 |
peter |
30 |
#include <cmath> |
2550 |
12 Aug 11 |
peter |
31 |
#include <cstdlib> |
3846 |
13 Sep 19 |
peter |
32 |
#include <cstring> |
1215 |
07 Mar 08 |
peter |
33 |
#include <fstream> |
1215 |
07 Mar 08 |
peter |
34 |
#include <iostream> |
2055 |
08 Sep 09 |
peter |
35 |
#include <limits> |
1215 |
07 Mar 08 |
peter |
36 |
#include <sstream> |
1215 |
07 Mar 08 |
peter |
37 |
#include <string> |
1215 |
07 Mar 08 |
peter |
38 |
|
3961 |
05 Aug 20 |
peter |
39 |
#include <gsl/gsl_errno.h> |
3961 |
05 Aug 20 |
peter |
40 |
|
1998 |
13 Jun 09 |
peter |
41 |
#include <sys/stat.h> |
1998 |
13 Jun 09 |
peter |
42 |
|
3961 |
05 Aug 20 |
peter |
43 |
|
1215 |
07 Mar 08 |
peter |
44 |
namespace theplu { |
1215 |
07 Mar 08 |
peter |
45 |
namespace yat { |
1215 |
07 Mar 08 |
peter |
46 |
namespace test { |
1215 |
07 Mar 08 |
peter |
47 |
|
3961 |
05 Aug 20 |
peter |
// Behaves like the default gsl_error_handler, but calls exit() |
3961 |
05 Aug 20 |
peter |
// instead of abort() |
3961 |
05 Aug 20 |
peter |
50 |
void yat_gsl_error_handler(const char* reason, |
3961 |
05 Aug 20 |
peter |
51 |
const char* file, |
3961 |
05 Aug 20 |
peter |
52 |
int line, int gsl_errno) |
3961 |
05 Aug 20 |
peter |
53 |
{ |
3961 |
05 Aug 20 |
peter |
// output something like: |
3961 |
05 Aug 20 |
peter |
// gsl: /usr/include/gsl/gsl_vector_double.h:206: ERROR: index out of range |
3961 |
05 Aug 20 |
peter |
56 |
std::cerr << "gsl: " << file << ":" << line << ": ERROR: " |
3961 |
05 Aug 20 |
peter |
57 |
<< reason << "\n"; |
3961 |
05 Aug 20 |
peter |
58 |
exit(1); |
3961 |
05 Aug 20 |
peter |
59 |
} |
3961 |
05 Aug 20 |
peter |
60 |
|
3961 |
05 Aug 20 |
peter |
61 |
|
3201 |
03 May 14 |
peter |
62 |
Suite::Suite(int argc, char* argv[], bool require_bam) |
2228 |
25 Mar 10 |
peter |
63 |
: known_issues_(0), ok_(true) |
1215 |
07 Mar 08 |
peter |
64 |
{ |
3961 |
05 Aug 20 |
peter |
65 |
gsl_set_error_handler(&test::yat_gsl_error_handler); |
3961 |
05 Aug 20 |
peter |
66 |
|
3201 |
03 May 14 |
peter |
67 |
if (require_bam) { |
3883 |
24 Mar 20 |
peter |
68 |
#ifndef YAT_HAVE_HTSLIB |
3439 |
20 Nov 15 |
peter |
69 |
out() << "no bam support\n"; |
3209 |
04 May 14 |
peter |
70 |
exit (EXIT_SKIP); |
3201 |
03 May 14 |
peter |
71 |
#endif |
3413 |
27 Apr 15 |
peter |
72 |
#ifndef HAVE_SAMTOOLS_EXECUTABLE |
3201 |
03 May 14 |
peter |
73 |
out() << "no samtools\n"; |
3209 |
04 May 14 |
peter |
74 |
exit (EXIT_SKIP); |
3201 |
03 May 14 |
peter |
75 |
#endif |
3201 |
03 May 14 |
peter |
76 |
} |
2893 |
09 Dec 12 |
peter |
77 |
std::string test_dir = "test/testSubDir/" + utility::basename(argv[0]); |
2893 |
09 Dec 12 |
peter |
78 |
utility::mkdir_p(test_dir); |
3846 |
13 Sep 19 |
peter |
79 |
if (chdir(test_dir.c_str())) { |
3846 |
13 Sep 19 |
peter |
80 |
err() << "error changing directory: " << test_dir |
3846 |
13 Sep 19 |
peter |
81 |
<< strerror(errno) << "\n"; |
3846 |
13 Sep 19 |
peter |
82 |
exit (EXIT_HARD_FAILURE); |
3846 |
13 Sep 19 |
peter |
83 |
} |
2893 |
09 Dec 12 |
peter |
84 |
out() << "running '" << argv[0] << "' in '" << test_dir << "'\n"; |
2228 |
25 Mar 10 |
peter |
// synchronize cerr and cout, i.e., cout is flushed before |
2228 |
25 Mar 10 |
peter |
// writing anything to cerr. |
2228 |
25 Mar 10 |
peter |
87 |
std::cerr.tie(&std::cout); |
1215 |
07 Mar 08 |
peter |
88 |
} |
1215 |
07 Mar 08 |
peter |
89 |
|
1215 |
07 Mar 08 |
peter |
90 |
|
1215 |
07 Mar 08 |
peter |
91 |
Suite::~Suite(void) |
1215 |
07 Mar 08 |
peter |
92 |
{ |
1215 |
07 Mar 08 |
peter |
93 |
} |
1215 |
07 Mar 08 |
peter |
94 |
|
1215 |
07 Mar 08 |
peter |
95 |
|
1304 |
14 May 08 |
peter |
96 |
bool Suite::add(bool ok) |
1232 |
15 Mar 08 |
peter |
97 |
{ |
1232 |
15 Mar 08 |
peter |
98 |
ok_ = ok_ && ok; |
1304 |
14 May 08 |
peter |
99 |
return ok; |
1232 |
15 Mar 08 |
peter |
100 |
} |
1232 |
15 Mar 08 |
peter |
101 |
|
1232 |
15 Mar 08 |
peter |
102 |
|
4058 |
20 Apr 21 |
peter |
103 |
Generator::Generator(void) |
4058 |
20 Apr 21 |
peter |
104 |
: k_(0) |
4058 |
20 Apr 21 |
peter |
105 |
{} |
4058 |
20 Apr 21 |
peter |
106 |
|
4058 |
20 Apr 21 |
peter |
107 |
|
4058 |
20 Apr 21 |
peter |
108 |
double Generator::operator()(void) |
4058 |
20 Apr 21 |
peter |
109 |
{ |
4058 |
20 Apr 21 |
peter |
110 |
++k_; |
4058 |
20 Apr 21 |
peter |
111 |
return sin(k_); |
4058 |
20 Apr 21 |
peter |
112 |
} |
4058 |
20 Apr 21 |
peter |
113 |
|
4058 |
20 Apr 21 |
peter |
114 |
|
1249 |
20 Mar 08 |
peter |
115 |
std::string filename(const std::string& path) |
1249 |
20 Mar 08 |
peter |
116 |
{ |
2455 |
29 Mar 11 |
peter |
117 |
std::string result; |
4200 |
19 Aug 22 |
peter |
118 |
result += YAT_ABS_TOP_SRCDIR; |
4200 |
19 Aug 22 |
peter |
119 |
result += "/test/"; |
4200 |
19 Aug 22 |
peter |
120 |
result += path; |
2455 |
29 Mar 11 |
peter |
121 |
return result; |
1249 |
20 Mar 08 |
peter |
122 |
} |
1249 |
20 Mar 08 |
peter |
123 |
|
1249 |
20 Mar 08 |
peter |
124 |
|
1215 |
07 Mar 08 |
peter |
125 |
std::ostream& Suite::err(void) const |
1215 |
07 Mar 08 |
peter |
126 |
{ |
2228 |
25 Mar 10 |
peter |
127 |
return std::cerr; |
1215 |
07 Mar 08 |
peter |
128 |
} |
1215 |
07 Mar 08 |
peter |
129 |
|
1215 |
07 Mar 08 |
peter |
130 |
|
1244 |
17 Mar 08 |
peter |
131 |
bool Suite::equal(double a, double b, unsigned long int n) |
1215 |
07 Mar 08 |
peter |
132 |
{ |
1239 |
16 Mar 08 |
peter |
133 |
double last_error_bound = n*std::numeric_limits<double>().epsilon()* |
1234 |
15 Mar 08 |
peter |
134 |
std::min(std::abs(a), std::abs(b)); |
1658 |
17 Dec 08 |
peter |
135 |
return equal_fix(a, b, last_error_bound); |
1658 |
17 Dec 08 |
peter |
136 |
} |
1658 |
17 Dec 08 |
peter |
137 |
|
1658 |
17 Dec 08 |
peter |
138 |
|
1658 |
17 Dec 08 |
peter |
139 |
bool Suite::equal_fix(double a, double b, double margin) |
1658 |
17 Dec 08 |
peter |
140 |
{ |
1658 |
17 Dec 08 |
peter |
// using the negated comparison to catch NaN problems |
1660 |
18 Dec 08 |
peter |
142 |
if (!(std::abs(a-b) <= margin)){ |
4200 |
19 Aug 22 |
peter |
143 |
err() << "Error: Comparing " << a << " and " << b |
1239 |
16 Mar 08 |
peter |
144 |
<< "\n Difference: " << a - b |
1658 |
17 Dec 08 |
peter |
145 |
<< "\n expected difference to be at most " << margin |
1239 |
16 Mar 08 |
peter |
146 |
<< std::endl; |
1239 |
16 Mar 08 |
peter |
147 |
return false; |
1239 |
16 Mar 08 |
peter |
148 |
} |
1239 |
16 Mar 08 |
peter |
149 |
return true; |
1215 |
07 Mar 08 |
peter |
150 |
} |
1215 |
07 Mar 08 |
peter |
151 |
|
1215 |
07 Mar 08 |
peter |
152 |
|
1247 |
17 Mar 08 |
peter |
153 |
bool Suite::equal_sqrt(double a, double b, unsigned long int n) |
1247 |
17 Mar 08 |
peter |
154 |
{ |
1247 |
17 Mar 08 |
peter |
155 |
double last_error_bound = n* |
1247 |
17 Mar 08 |
peter |
156 |
std::sqrt(std::numeric_limits<double>().epsilon())* |
1247 |
17 Mar 08 |
peter |
157 |
std::min(std::abs(a), std::abs(b)); |
1247 |
17 Mar 08 |
peter |
158 |
if (!(std::abs(a-b) <= last_error_bound)){ |
4200 |
19 Aug 22 |
peter |
159 |
err() << "Error: Comparing " << a << " and " << b |
1247 |
17 Mar 08 |
peter |
160 |
<< "\n Difference: " << a - b |
1247 |
17 Mar 08 |
peter |
161 |
<< "\n expected difference to be at most " << last_error_bound |
1247 |
17 Mar 08 |
peter |
162 |
<< std::endl; |
1247 |
17 Mar 08 |
peter |
163 |
return false; |
1247 |
17 Mar 08 |
peter |
164 |
} |
1247 |
17 Mar 08 |
peter |
165 |
return true; |
1247 |
17 Mar 08 |
peter |
166 |
} |
1247 |
17 Mar 08 |
peter |
167 |
|
1247 |
17 Mar 08 |
peter |
168 |
|
1215 |
07 Mar 08 |
peter |
169 |
bool Suite::ok(void) const |
1215 |
07 Mar 08 |
peter |
170 |
{ |
1215 |
07 Mar 08 |
peter |
171 |
return ok_; |
1215 |
07 Mar 08 |
peter |
172 |
} |
1215 |
07 Mar 08 |
peter |
173 |
|
1215 |
07 Mar 08 |
peter |
174 |
|
1215 |
07 Mar 08 |
peter |
175 |
std::ostream& Suite::out(void) const |
1215 |
07 Mar 08 |
peter |
176 |
{ |
2228 |
25 Mar 10 |
peter |
177 |
return std::cout; |
1215 |
07 Mar 08 |
peter |
178 |
} |
1215 |
07 Mar 08 |
peter |
179 |
|
1215 |
07 Mar 08 |
peter |
180 |
|
4252 |
18 Nov 22 |
peter |
181 |
void Suite::require_foo_vcf_gz(void) const |
4252 |
18 Nov 22 |
peter |
182 |
{ |
4252 |
18 Nov 22 |
peter |
183 |
#ifndef HAVE_BCFTOOLS_EXECUTABLE |
4252 |
18 Nov 22 |
peter |
184 |
out() << "no bcftools executable\n"; |
4252 |
18 Nov 22 |
peter |
185 |
exit (EXIT_SKIP); |
4252 |
18 Nov 22 |
peter |
186 |
#endif |
4252 |
18 Nov 22 |
peter |
187 |
} |
4252 |
18 Nov 22 |
peter |
188 |
|
4252 |
18 Nov 22 |
peter |
189 |
|
1215 |
07 Mar 08 |
peter |
190 |
int Suite::return_value(void) const |
1215 |
07 Mar 08 |
peter |
191 |
{ |
1509 |
17 Sep 08 |
peter |
192 |
if (known_issues_>1) |
1509 |
17 Sep 08 |
peter |
193 |
out() << known_issues_ << " known issues were detected.\n"; |
1509 |
17 Sep 08 |
peter |
194 |
else if (known_issues_==1) |
1509 |
17 Sep 08 |
peter |
195 |
out() << known_issues_ << " known issue was detected.\n"; |
1215 |
07 Mar 08 |
peter |
196 |
if (ok()){ |
1215 |
07 Mar 08 |
peter |
197 |
out() << "Test is ok.\n"; |
2550 |
12 Aug 11 |
peter |
198 |
return EXIT_SUCCESS; |
1215 |
07 Mar 08 |
peter |
199 |
} |
1215 |
07 Mar 08 |
peter |
200 |
out() << "Test failed.\n"; |
2550 |
12 Aug 11 |
peter |
201 |
return EXIT_FAILURE; |
1215 |
07 Mar 08 |
peter |
202 |
} |
1215 |
07 Mar 08 |
peter |
203 |
|
1215 |
07 Mar 08 |
peter |
204 |
|
4280 |
27 Jan 23 |
peter |
205 |
utility::Matrix generate_Matrix(size_t row, size_t col, double seed) |
4280 |
27 Jan 23 |
peter |
206 |
{ |
4280 |
27 Jan 23 |
peter |
207 |
utility::Matrix m(row, col); |
4280 |
27 Jan 23 |
peter |
208 |
int count = 0; |
4280 |
27 Jan 23 |
peter |
209 |
for (size_t i=0; i<row; ++i) { |
4280 |
27 Jan 23 |
peter |
210 |
for (size_t j=0; j<col; ++j) { |
4280 |
27 Jan 23 |
peter |
211 |
m(i, j) = sin(count+seed); |
4280 |
27 Jan 23 |
peter |
212 |
++count; |
4280 |
27 Jan 23 |
peter |
213 |
} |
4280 |
27 Jan 23 |
peter |
214 |
} |
4280 |
27 Jan 23 |
peter |
215 |
return m; |
4280 |
27 Jan 23 |
peter |
216 |
} |
4280 |
27 Jan 23 |
peter |
217 |
|
4280 |
27 Jan 23 |
peter |
218 |
|
1998 |
13 Jun 09 |
peter |
219 |
bool run_as_root(void) |
1998 |
13 Jun 09 |
peter |
220 |
{ |
1998 |
13 Jun 09 |
peter |
221 |
std::string file("write_protected_file.txt"); |
1998 |
13 Jun 09 |
peter |
222 |
std::ofstream os(file.c_str()); |
1998 |
13 Jun 09 |
peter |
223 |
os.close(); |
1998 |
13 Jun 09 |
peter |
224 |
chmod(file.c_str(), S_IRUSR); |
1998 |
13 Jun 09 |
peter |
225 |
os.open(file.c_str()); |
1998 |
13 Jun 09 |
peter |
226 |
bool status = os.good(); |
1998 |
13 Jun 09 |
peter |
227 |
chmod(file.c_str(), S_IWUSR); |
1998 |
13 Jun 09 |
peter |
228 |
unlink(file.c_str()); |
1998 |
13 Jun 09 |
peter |
229 |
return status; |
1998 |
13 Jun 09 |
peter |
230 |
} |
1998 |
13 Jun 09 |
peter |
231 |
|
1998 |
13 Jun 09 |
peter |
232 |
|
1509 |
17 Sep 08 |
peter |
233 |
bool Suite::xadd(bool ok) |
1509 |
17 Sep 08 |
peter |
234 |
{ |
1509 |
17 Sep 08 |
peter |
235 |
if (!ok) |
1509 |
17 Sep 08 |
peter |
236 |
++known_issues_; |
1509 |
17 Sep 08 |
peter |
237 |
else { |
1509 |
17 Sep 08 |
peter |
238 |
err() << " test for a known issue returned true\n"; |
1509 |
17 Sep 08 |
peter |
239 |
add(false); |
1509 |
17 Sep 08 |
peter |
240 |
} |
1509 |
17 Sep 08 |
peter |
241 |
return ok; |
1509 |
17 Sep 08 |
peter |
242 |
} |
1509 |
17 Sep 08 |
peter |
243 |
|
1509 |
17 Sep 08 |
peter |
244 |
|
1215 |
07 Mar 08 |
peter |
245 |
}}} |