00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "Reporter.h"
00012
00013 #include <iostream>
00014 #include <cmath>
00015 #include <iomanip>
00016
00017 #ifdef OLDSTREAMS
00018 # include <strstream>
00019 # define ostringstream ostrstream
00020 #else
00021 # include <sstream>
00022 #endif
00023
00024
00025 namespace Anaphe {
00026
00027
00028
00029 const float Reporter::F_PREC = 1e-5;
00030 const double Reporter::D_PREC = 1e-9;
00031 const std::string Reporter::H_TITLE = "Test description";
00032 const std::string Reporter::H_EXP = "Expected";
00033 const std::string Reporter::H_OBS = "Observed";
00034 const std::string Reporter::H_FLAG = "Pass?";
00035
00036
00037
00038
00039 Reporter::Test::Test(void)
00040 : mname(""),
00041 mex (""),
00042 mob (""),
00043 mcomp(false)
00044 {
00045
00046 }
00047
00048
00049
00050
00051 Reporter::Test::Test(const std::string& n, const std::string& e,
00052 const std::string& o, const bool& c)
00053 : mname(n),
00054 mex (e),
00055 mob (o),
00056 mcomp(c)
00057 {
00058
00059 }
00060
00061
00062
00063
00064 Reporter::Reporter(void)
00065 : widest_title(1),
00066 widest_ob (1),
00067 widest_ex (1),
00068 nt (1)
00069 {
00070 clear();
00071 }
00072
00073
00074
00075
00076 Reporter::~Reporter(void)
00077 {
00078 clear();
00079 }
00080
00081
00082
00083
00084 void Reporter::clear(void)
00085 {
00086 testList.clear();
00087 nt = 0;
00088 widest_title = H_TITLE.length();
00089 widest_ex = H_EXP.length();
00090 widest_ob = H_OBS.length();
00091 }
00092
00093
00094
00095
00096 void Reporter::setTitle(const std::string& t)
00097 {
00098 title = t;
00099 }
00100
00101
00102
00103
00104 bool Reporter::veryClose(const float& a, const float& b) const
00105 {
00106 const float diff = std::fabs(a-b);
00107 const float tolerance = std::fabs(a*F_PREC);
00108 if (diff <= tolerance) return true;
00109 else return false;
00110 }
00111
00112
00113
00114
00115 bool Reporter::veryClose(const double& a, const double& b) const
00116 {
00117 const double diff = std::fabs(a-b);
00118 const double tolerance = std::fabs(a*D_PREC);
00119 if (diff <= tolerance) return true;
00120 else return false;
00121 }
00122
00123
00124
00125
00126 void Reporter::keep(const std::string& name, const int& ex, const int& ob)
00127 {
00128 std::string strex = asString(ex);
00129 std::string strob = asString(ob);
00130 bool comp = (ex==ob);
00131 reg(name,strex,strob,comp);
00132 }
00133
00134
00135
00136
00137 void Reporter::keep(const std::string& name, const float& ex, const float& ob)
00138 {
00139 std::string strex = asString(ex);
00140 std::string strob = asString(ob);
00141 bool comp = veryClose(ex,ob);
00142 reg(name,strex,strob,comp);
00143 }
00144
00145
00146
00147
00148 void Reporter::keep(const std::string& name, const char* ex, const char* ob)
00149 {
00150 std::string strex = ex;
00151 std::string strob = ob;
00152 bool comp = (strex==strob);
00153 reg(name,strex,strob,comp);
00154 }
00155
00156
00157
00158
00159 void Reporter::keep(const std::string& name, const double& ex, const double& ob)
00160 {
00161 std::string strex = asString(ex);
00162 std::string strob = asString(ob);
00163 bool comp = veryClose(ex,ob);
00164 reg(name,strex,strob,comp);
00165 }
00166
00167
00168
00169
00170 void Reporter::keep(const std::string& name, const std::string& ex, const std::string& ob)
00171 {
00172 bool comp = (ex==ob);
00173 reg(name, "\""+ex+"\"", "\""+ob+"\"", comp);
00174 }
00175
00176
00177
00178
00179 void Reporter::keep(const std::string& name, const bool& ex, const bool& ob)
00180 {
00181 std::string strex = asString(static_cast<int>(ex));
00182 std::string strob = asString(static_cast<int>(ob));
00183 bool comp = (ex==ob);
00184 reg(name,strex,strob,comp);
00185 }
00186
00187
00188
00189
00190 bool Reporter::report(const bool& pr) const
00191 {
00192 bool allOK = true;
00193 if (pr) printHeader();
00194 for (TestIt i = testList.begin(); i != testList.end(); ++i) {
00195 const Test& t = i->second;
00196 const int OK = t.comp();
00197 if (!OK) allOK = false;
00198 if (pr) print(t.name(), t.ex(), t.ob(), OK);
00199 }
00200 if (pr) std::cout << std::endl;
00201 return allOK;
00202 }
00203
00204
00205
00206
00207 std::string Reporter::asString(const int& a) const
00208 {
00209 std::ostringstream o;
00210 o << a;
00211 #ifndef BADENDS
00212 o << std::ends;
00213 #endif
00214 std::string ret = o.str();
00215 return ret;
00216 }
00217
00218
00219
00220
00221 std::string Reporter::asString(const float& a) const
00222 {
00223 std::ostringstream o;
00224 o << a;
00225 #ifndef BADENDS
00226 o << std::ends;
00227 #endif
00228 std::string ret = o.str();
00229 return ret;
00230 }
00231
00232
00233
00234
00235 std::string Reporter::asString(const double& a) const
00236 {
00237 std::ostringstream o;
00238 o << a;
00239 #ifndef BADENDS
00240 o << std::ends;
00241 #endif
00242 std::string ret = o.str();
00243 return ret;
00244 }
00245
00246
00247
00248
00249 void Reporter::print(const std::string& id, const std::string& ob,
00250 const std::string& ex, const int& flag) const
00251 {
00252 std::cout << std::setw(widest_title+1) << id.c_str()
00253 << std::setw(widest_ob+1) << ob.c_str()
00254 << std::setw(widest_ex+1) << ex.c_str()
00255 << std::setw(H_FLAG.length()+1);
00256 if (flag == -1) std::cout << "Pass?" << std::endl;
00257 else std::cout << (flag?"ok":"FAIL") << std::endl;
00258 }
00259
00260
00261
00262
00263 void Reporter::printHeader(void) const
00264 {
00265 std::cout << std::endl << " *** " << title << " *** "
00266 << std::endl << std::endl;
00267 print("Test description","Expected","Observed",-1);
00268 const int width = widest_title + widest_ex + widest_ob + 10;
00269 char* c = new char[width];
00270 for (int i = 0; i < width; ++i) c[i] = '=';
00271 c[width] = '\0';
00272 std::cout << c << std::endl;
00273 delete[] c;
00274 }
00275
00276
00277
00278
00279 void Reporter::reg(const std::string& name, const std::string& ex,
00280 const std::string& ob, const bool& res)
00281 {
00282 Test t(name,ex,ob,res);
00283 testList[nt++] = t;
00284 if (name.length() > widest_title) widest_title = name.length();
00285 if (ex.length() > widest_ex) widest_ex = ex.length();
00286 if (ob.length() > widest_ob) widest_ob = ob.length();
00287 }
00288
00289
00290
00291 }