source: trunk/test/Suite.cc @ 2055

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

fixed some warnings from gcc 4.3.1 and added some missing includes. There are still some warnings for test/commandline_test.cc, but not obvious how to fix them.

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