source: trunk/test/Suite.cc @ 2228

Last change on this file since 2228 was 2228, checked in by Peter, 12 years ago

Using the parallel-tests driver. All tests are now by default verbose,
but 'make check' is still silent and the output is collected in log
files. Please refer to test/README or the Automake manual for more
details.

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