source: trunk/test/Suite.cc @ 2455

Last change on this file since 2455 was 2455, checked in by Peter, 11 years ago

closes #662

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