source: trunk/test/Suite.cc @ 1509

Last change on this file since 1509 was 1509, checked in by Peter, 13 years ago

fixes #437 and added support for having known issues in test::Suite

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: Suite.cc 1509 2008-09-17 20:56:30Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
23#include "environment.h"
24
25#include <algorithm>
26#include <cmath>
27#include <fstream>
28#include <iostream>
29#include <sstream>
30#include <string>
31
32namespace theplu {
33namespace yat {
34namespace test {
35
36  Suite::Suite(int argc, char* argv[])
37    : known_issues_(0), ok_(true)
38  {
39    dev_null_ = new std::ofstream("/dev/null");
40    if (argc>1 && argv[1]==std::string("-v"))
41      verbose_ = true;
42    else {
43      verbose_ = false;
44      if (argc>1){
45        std::stringstream ss(argv[0]);
46        std::string prog;
47        while(getline(ss, prog, '/'));
48        if (prog.substr(0,3)=="lt-")
49          prog = prog.substr(3);
50        std::cout << prog << " -v : for printing extra information\n";
51      }
52    }
53  }
54
55
56  Suite::~Suite(void)
57  {
58    delete dev_null_;
59  }
60
61
62  bool Suite::add(bool ok)
63  {
64    ok_ = ok_ && ok;
65    return ok;
66  }
67
68
69  std::string filename(const std::string& path)
70  {
71    return abs_srcdir()+"/"+path;
72  }
73
74
75  std::ostream& Suite::err(void) const
76  {
77    if (verbose_)
78      return std::cerr;
79    return *dev_null_;
80  }
81
82
83  bool Suite::equal(double a, double b, unsigned long int n)
84  {
85    double last_error_bound = n*std::numeric_limits<double>().epsilon()*
86      std::min(std::abs(a), std::abs(b));
87    if (!(std::abs(a-b) <= last_error_bound)){
88      err() << "Error: Comparing " << a << " and " << b
89            << "\n  Difference: " << a - b
90            << "\n  expected difference to be at most " << last_error_bound
91            << std::endl;
92      return false;
93    }
94    return true;
95  }
96
97
98  bool Suite::equal_sqrt(double a, double b, unsigned long int n)
99  {
100    double last_error_bound = n*
101      std::sqrt(std::numeric_limits<double>().epsilon())*
102      std::min(std::abs(a), std::abs(b));
103    if (!(std::abs(a-b) <= last_error_bound)){
104      err() << "Error: Comparing " << a << " and " << b
105            << "\n  Difference: " << a - b
106            << "\n  expected difference to be at most " << last_error_bound
107            << std::endl;
108      return false;
109    }
110    return true;
111  }
112
113
114  bool Suite::ok(void) const
115  {
116    return ok_;
117  }
118
119
120  std::ostream& Suite::out(void) const
121  {
122    if (verbose_)
123      return std::cout;
124    return *dev_null_;
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 Suite::xadd(bool ok)
144  {
145    if (!ok)
146      ++known_issues_;
147    else {
148      err() << " test for a known issue returned true\n";
149      add(false);
150    }
151    return ok;
152  }
153
154
155}}}
Note: See TracBrowser for help on using the repository browser.