source: trunk/test/Suite.cc @ 3439

Last change on this file since 3439 was 3439, checked in by Peter, 4 years ago

merge release 0.13 into trunk

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