source: trunk/test/Suite.cc

Last change on this file was 4089, checked in by Peter, 3 weeks ago

updating copyright years

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