source: trunk/test/Suite.cc @ 2035

Last change on this file since 2035 was 2035, checked in by Peter, 13 years ago
  • deprecate YAT_TEST_VERBOSE and use VERBOSE instead (fixes #557).
  • tie cerr with cout, so cout is flushed before anything is written to cerr. (We should be more careful in selecting writing to cout or cerr).
  • updated README appropriately. modified make examples to less gnu make specific syntax.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1// $Id: Suite.cc 2035 2009-08-22 14:32:57Z 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 <sstream>
31#include <string>
32
33#include <sys/stat.h>
34
35namespace theplu {
36namespace yat {
37namespace test {
38
39  Suite::Suite(int argc, char* argv[])
40    : dev_null_(NULL), known_issues_(0), ok_(true)
41  {
42    char* buffer=std::getenv("YAT_TEST_VERBOSE");
43    if (buffer)
44      std::cerr << "WARNING: environment variable `YAT_TEST_VERBOSE' "
45                << "is deprecated\n  use `VERBOSE' instead.\n";
46    char* env_verbose=std::getenv("VERBOSE");
47
48    if ( (argc>1 && argv[1]==std::string("-v")) || 
49         (buffer && buffer == std::string("1")) ||
50         env_verbose) {
51      verbose_ = true;
52      // synchronize cerr and cout, i.e., cout is flushed before
53      // writing anything to cerr.
54      std::cerr.tie(&std::cout);
55    }
56    else {
57      verbose_ = false;
58      dev_null_ = new std::ofstream("/dev/null");
59      if (argc>1){
60        std::stringstream ss(argv[0]);
61        std::string prog;
62        while(getline(ss, prog, '/'));
63        if (prog.substr(0,3)=="lt-")
64          prog = prog.substr(3);
65        std::cout << prog << " -v : for printing extra information\n";
66      }
67    }
68  }
69
70
71  Suite::~Suite(void)
72  {
73    if (dev_null_)
74      delete dev_null_;
75  }
76
77
78  bool Suite::add(bool ok)
79  {
80    ok_ = ok_ && ok;
81    return ok;
82  }
83
84
85  std::string filename(const std::string& path)
86  {
87    return abs_srcdir()+"/"+path;
88  }
89
90
91  std::ostream& Suite::err(void) const
92  {
93    if (verbose_)
94      return std::cerr;
95    return *dev_null_;
96  }
97
98
99  bool Suite::equal(double a, double b, unsigned long int n)
100  {
101    double last_error_bound = n*std::numeric_limits<double>().epsilon()*
102      std::min(std::abs(a), std::abs(b));
103    return equal_fix(a, b, last_error_bound);
104  }
105
106
107  bool Suite::equal_fix(double a, double b, double margin)
108  {
109    // using the negated comparison to catch NaN problems
110    if (!(std::abs(a-b) <= margin)){
111      err() << "Error: Comparing " << a << " and " << b
112            << "\n  Difference: " << a - b
113            << "\n  expected difference to be at most " << margin
114            << std::endl;
115      return false;
116    }
117    return true;
118  }
119
120
121  bool Suite::equal_sqrt(double a, double b, unsigned long int n)
122  {
123    double last_error_bound = n*
124      std::sqrt(std::numeric_limits<double>().epsilon())*
125      std::min(std::abs(a), std::abs(b));
126    if (!(std::abs(a-b) <= last_error_bound)){
127      err() << "Error: Comparing " << a << " and " << b
128            << "\n  Difference: " << a - b
129            << "\n  expected difference to be at most " << last_error_bound
130            << std::endl;
131      return false;
132    }
133    return true;
134  }
135
136
137  bool Suite::ok(void) const
138  {
139    return ok_;
140  }
141
142
143  std::ostream& Suite::out(void) const
144  {
145    if (verbose_)
146      return std::cout;
147    return *dev_null_;
148  }
149
150
151  int Suite::return_value(void) const
152  {
153    if (known_issues_>1)
154      out() << known_issues_ << " known issues were detected.\n";
155    else if (known_issues_==1)
156      out() << known_issues_ << " known issue was detected.\n";
157    if (ok()){
158      out() << "Test is ok.\n";
159      return 0;
160    }
161    out() << "Test failed.\n";
162    return -1;
163  }
164
165
166  bool run_as_root(void)
167  {
168    std::string file("write_protected_file.txt");
169    std::ofstream os(file.c_str());
170    os.close();
171    chmod(file.c_str(), S_IRUSR);
172    os.open(file.c_str());
173    bool status = os.good();
174    chmod(file.c_str(), S_IWUSR);
175    unlink(file.c_str());
176    return status;
177  }
178
179
180  bool Suite::xadd(bool ok)
181  {
182    if (!ok)
183      ++known_issues_;
184    else {
185      err() << " test for a known issue returned true\n";
186      add(false);
187    }
188    return ok;
189  }
190
191
192}}}
Note: See TracBrowser for help on using the repository browser.