source: trunk/test/Suite.cc @ 3846

Last change on this file since 3846 was 3846, checked in by Peter, 2 months ago

avoid compiler warning about ignoring return value from chdir

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: Suite.cc 3846 2019-09-13 12:11:23Z peter $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010, 2011, 2012, 2014, 2015 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 <sys/stat.h>
40
41namespace theplu {
42namespace yat {
43namespace test {
44
45  Suite::Suite(int argc, char* argv[], bool require_bam)
46    : known_issues_(0), ok_(true)
47  {
48    if (require_bam) {
49#ifndef YAT_HAVE_LIBBAM
50      out() << "no bam support\n";
51      exit (EXIT_SKIP);
52#endif
53#ifndef HAVE_SAMTOOLS_EXECUTABLE
54      out() << "no samtools\n";
55      exit (EXIT_SKIP);
56#endif
57    }
58    std::string test_dir = "test/testSubDir/" + utility::basename(argv[0]);
59    utility::mkdir_p(test_dir);
60    if (chdir(test_dir.c_str())) {
61      err() << "error changing directory: " << test_dir
62            << strerror(errno) << "\n";
63      exit (EXIT_HARD_FAILURE);
64    }
65    out() << "running '" << argv[0] << "' in '" << test_dir << "'\n";
66    // synchronize cerr and cout, i.e., cout is flushed before
67    // writing anything to cerr.
68    std::cerr.tie(&std::cout);
69  }
70
71
72  Suite::~Suite(void)
73  {
74  }
75
76
77  bool Suite::add(bool ok)
78  {
79    ok_ = ok_ && ok;
80    return ok;
81  }
82
83
84  std::string filename(const std::string& path)
85  {
86    std::string result;
87    result += YAT_ABS_TOP_SRCDIR; 
88    result += "/test/"; 
89    result += path; 
90    return result;
91  }
92
93
94  std::ostream& Suite::err(void) const
95  {
96    return std::cerr;
97  }
98
99
100  bool Suite::equal(double a, double b, unsigned long int n)
101  {
102    double last_error_bound = n*std::numeric_limits<double>().epsilon()*
103      std::min(std::abs(a), std::abs(b));
104    return equal_fix(a, b, last_error_bound);
105  }
106
107
108  bool Suite::equal_fix(double a, double b, double margin)
109  {
110    // using the negated comparison to catch NaN problems
111    if (!(std::abs(a-b) <= margin)){
112      err() << "Error: Comparing " << a << " and " << b
113            << "\n  Difference: " << a - b
114            << "\n  expected difference to be at most " << margin
115            << std::endl;
116      return false;
117    }
118    return true;
119  }
120
121
122  bool Suite::equal_sqrt(double a, double b, unsigned long int n)
123  {
124    double last_error_bound = n*
125      std::sqrt(std::numeric_limits<double>().epsilon())*
126      std::min(std::abs(a), std::abs(b));
127    if (!(std::abs(a-b) <= last_error_bound)){
128      err() << "Error: Comparing " << a << " and " << b
129            << "\n  Difference: " << a - b
130            << "\n  expected difference to be at most " << last_error_bound
131            << std::endl;
132      return false;
133    }
134    return true;
135  }
136
137
138  bool Suite::ok(void) const
139  {
140    return ok_;
141  }
142
143
144  std::ostream& Suite::out(void) const
145  {
146    return std::cout;
147  }
148
149
150  int Suite::return_value(void) const
151  {
152    if (known_issues_>1)
153      out() << known_issues_ << " known issues were detected.\n";
154    else if (known_issues_==1)
155      out() << known_issues_ << " known issue was detected.\n";
156    if (ok()){
157      out() << "Test is ok.\n";
158      return EXIT_SUCCESS;
159    }
160    out() << "Test failed.\n";
161    return EXIT_FAILURE;
162  }
163
164
165  bool run_as_root(void)
166  {
167    std::string file("write_protected_file.txt");
168    std::ofstream os(file.c_str());
169    os.close();
170    chmod(file.c_str(), S_IRUSR);
171    os.open(file.c_str());
172    bool status = os.good();
173    chmod(file.c_str(), S_IWUSR);
174    unlink(file.c_str());
175    return status;
176  }
177
178
179  bool Suite::xadd(bool ok)
180  {
181    if (!ok)
182      ++known_issues_;
183    else {
184      err() << " test for a known issue returned true\n";
185      add(false);
186    }
187    return ok;
188  }
189
190
191}}}
Note: See TracBrowser for help on using the repository browser.