source: trunk/test/Suite.cc @ 1247

Last change on this file since 1247 was 1247, checked in by Peter, 15 years ago

refs #223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Suite.cc 1247 2008-03-17 14:54:10Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Suite.h"
25
26#include <algorithm>
27#include <cmath>
28#include <fstream>
29#include <iostream>
30#include <sstream>
31#include <string>
32
33namespace theplu {
34namespace yat {
35namespace test {
36
37  Suite::Suite(int argc, char* argv[])
38    : ok_(true)
39  {
40    dev_null_ = new std::ofstream("/dev/null");
41    if (argc>1 && argv[1]==std::string("-v"))
42      verbose_ = true;
43    else {
44      verbose_ = false;
45      if (argc>1){
46        std::stringstream ss(argv[0]);
47        std::string prog;
48        while(getline(ss, prog, '/'));
49        if (prog.substr(0,3)=="lt-")
50          prog = prog.substr(3);
51        std::cout << prog << " -v : for printing extra information\n";
52      }
53    }
54  }
55
56
57  Suite::~Suite(void)
58  {
59    delete dev_null_;
60  }
61
62
63  void Suite::add(bool ok)
64  {
65    ok_ = ok_ && ok;
66  }
67
68
69  std::ostream& Suite::err(void) const
70  {
71    if (verbose_)
72      return std::cerr;
73    return *dev_null_;
74  }
75
76
77  bool Suite::equal(double a, double b, unsigned long int n)
78  {
79    double last_error_bound = n*std::numeric_limits<double>().epsilon()*
80      std::min(std::abs(a), std::abs(b));
81    if (!(std::abs(a-b) <= last_error_bound)){
82      err() << "Error: Comparing " << a << " and " << b
83            << "\n  Difference: " << a - b
84            << "\n  expected difference to be at most " << last_error_bound
85            << std::endl;
86      return false;
87    }
88    return true;
89  }
90
91
92  bool Suite::equal_sqrt(double a, double b, unsigned long int n)
93  {
94    double last_error_bound = n*
95      std::sqrt(std::numeric_limits<double>().epsilon())*
96      std::min(std::abs(a), std::abs(b));
97    if (!(std::abs(a-b) <= last_error_bound)){
98      err() << "Error: Comparing " << a << " and " << b
99            << "\n  Difference: " << a - b
100            << "\n  expected difference to be at most " << last_error_bound
101            << std::endl;
102      return false;
103    }
104    return true;
105  }
106
107
108  bool Suite::ok(void) const
109  {
110    return ok_;
111  }
112
113
114  std::ostream& Suite::out(void) const
115  {
116    if (verbose_)
117      return std::cout;
118    return *dev_null_;
119  }
120
121
122  int Suite::return_value(void) const
123  {
124    if (ok()){
125      out() << "Test is ok.\n";
126      return 0;
127    }
128    out() << "Test failed.\n";
129    return -1;
130  }
131
132
133}}}
Note: See TracBrowser for help on using the repository browser.