source: trunk/test/Suite.cc @ 2550

Last change on this file since 2550 was 2550, checked in by Peter, 10 years ago

use macros EXIT_FAILURE and EXIT_SUCCESS

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