Commit e539ad9b authored by Martin Marinov's avatar Martin Marinov

Enforce containment of test checksums in namespace Test::Checksum

parent 4f8bd49e
// (C) Copyright 2019 by Autodesk, Inc. // (C) Copyright 2020 by Autodesk, Inc.
#ifdef DEB_ON #ifdef DEB_ON
...@@ -9,28 +9,29 @@ ...@@ -9,28 +9,29 @@
#include "Base/Code/CodeLink.hh" #include "Base/Code/CodeLink.hh"
#include "Base/Test/ChecksumDebugEvent.hh" #include "Base/Test/ChecksumDebugEvent.hh"
namespace Debug { namespace Debug
{
void warning(const std::string& _wrng, const Base::CodeLink& _lnk) void warning(const std::string& _wrng, const Base::CodeLink& _lnk)
{ {
TEST(Test::Checksum::Debug::warning, record(_wrng, _lnk)); TEST(Debug::warning, record(_wrng, _lnk));
Stream strm(File::modify()); Stream strm(File::modify());
strm << WARNING << ": " << _wrng << ' ' << _lnk << Base::ENDL; strm << WARNING << ": " << _wrng << ' ' << _lnk << Base::ENDL;
} }
void error(const std::string& _err, const Base::CodeLink& _lnk) void error(const std::string& _err, const Base::CodeLink& _lnk)
{ {
TEST(Test::Checksum::Debug::error, record(_err, _lnk)); TEST(Debug::error, record(_err, _lnk));
Stream strm(File::modify()); Stream strm(File::modify());
strm << ERROR << ": " << _err << ' ' << _lnk << Base::ENDL; strm << ERROR << ": " << _err << ' ' << _lnk << Base::ENDL;
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
Enter::Enter(const char* const _flnm, const char* const _fnct, Enter::Enter(
int& _nmbr, int& _lvl) const char* const _flnm, const char* const _fnct, int& _nmbr, int& _lvl)
: flnm_(_flnm), fnct_(_fnct), outs_(0), strm_(File::modify()) : flnm_(_flnm), fnct_(_fnct), outs_(0), strm_(File::modify())
{// TODO: for thread-safety we will need to make the constructor body atomic! { // TODO: for thread-safety we will need to make the constructor body atomic!
strm_.file_.enter(this); strm_.file_.enter(this);
nmbr_ = _nmbr++; nmbr_ = _nmbr++;
...@@ -42,10 +43,7 @@ Enter::Enter(const char* const _flnm, const char* const _fnct, ...@@ -42,10 +43,7 @@ Enter::Enter(const char* const _flnm, const char* const _fnct,
id_ = ++id_cnt; id_ = ++id_cnt;
} }
Enter::~Enter() Enter::~Enter() { strm_.file_.exit(); }
{
strm_.file_.exit();
}
Stream& Enter::stream() Stream& Enter::stream()
{ {
...@@ -59,8 +57,7 @@ Stream& Enter::stream() ...@@ -59,8 +57,7 @@ Stream& Enter::stream()
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
Stream::Stream(const Stream& _othr) : file_(_othr.file_) {} Stream::Stream(const Stream& _othr) : file_(_othr.file_) {}
Stream& Stream::operator = (const Stream&) { return *this; } Stream& Stream::operator=(const Stream&) { return *this; }
Base::IOutputStream& Stream::print(const int _i) Base::IOutputStream& Stream::print(const int _i)
{ {
...@@ -98,6 +95,6 @@ Base::IOutputStream& Stream::print(const char _c) ...@@ -98,6 +95,6 @@ Base::IOutputStream& Stream::print(const char _c)
return *this; return *this;
}; };
}//namespace Debug } // namespace Debug
#endif // DEB_ON #endif // DEB_ON
...@@ -15,13 +15,21 @@ ...@@ -15,13 +15,21 @@
#include <map> #include <map>
#include <sstream> #include <sstream>
namespace Test { namespace Test
namespace Checksum { {
namespace Checksum
{
//! Enumerate the checksum levels //! Enumerate the checksum levels
enum Level { L_NONE, L_STABLE, L_PRIME, L_ALL }; enum Level
{
L_NONE,
L_STABLE,
L_PRIME,
L_ALL
};
extern Level run_lvl; //<! The checksum run level extern Level run_lvl; //<! The checksum run level
const char* const LEVEL_TEXT[4] = { "NONE", "STABLE", "PRIME", "ALL" }; const char* const LEVEL_TEXT[4] = {"NONE", "STABLE", "PRIME", "ALL"};
//! typedef String, this is used a lot in this namespace //! typedef String, this is used a lot in this namespace
typedef std::string String; typedef std::string String;
...@@ -37,40 +45,32 @@ struct Record ...@@ -37,40 +45,32 @@ struct Record
}; };
//! The difference found by the IChecksum::compare() operation //! The difference found by the IChecksum::compare() operation
class Difference class Difference
{ {
public: public:
enum Type enum Type
{ {
EQUAL, // result is bitwise identical EQUAL, // result is bitwise identical
UNKNOWN, // non-negligible difference, but of unknown quality UNKNOWN, // non-negligible difference, but of unknown quality
IMPROVED, // result is better IMPROVED, // result is better
NEGLEGIBLE, // result is negligibly different NEGLEGIBLE, // result is negligibly different
SUSPICIOUS, // result is different, and the new result might be worse SUSPICIOUS, // result is different, and the new result might be worse
REGRESSED, // result is worse REGRESSED, // result is worse
WORKED, // result works now, but used to fail WORKED, // result works now, but used to fail
FAILED // result fails now, but used to work FAILED // result fails now, but used to work
}; };
static const char* const type_text(const Type _type) static const char* const type_text(const Type _type)
{ {
static const char dscr[][32] = static const char dscr[][32] = {"EQUAL", "UNKNOWN", "IMPROVED",
{ "NEGLEGIBLE", "SUSPICIOUS", "REGRESSED", "WORKED", "FAILED"};
"EQUAL",
"UNKNOWN",
"IMPROVED",
"NEGLEGIBLE",
"SUSPICIOUS",
"REGRESSED",
"WORKED",
"FAILED"
};
return dscr[_type]; return dscr[_type];
} }
Difference(const Type _type = EQUAL, const String& _dscr = String()) Difference(const Type _type = EQUAL, const String& _dscr = String())
: type_(_type), dscr_(_dscr) : type_(_type), dscr_(_dscr)
{} {
}
const Type type() const { return type_; } const Type type() const { return type_; }
bool equal() const { return type() == EQUAL; } bool equal() const { return type() == EQUAL; }
...@@ -98,15 +98,12 @@ public: ...@@ -98,15 +98,12 @@ public:
return *this; return *this;
} }
const String& description() const const String& description() const { return dscr_; }
{
return dscr_;
}
const char* const type_text() const { return type_text(type_); } const char* const type_text() const { return type_text(type_); }
friend Base::IOutputStream& operator<<(Base::IOutputStream& _os, friend Base::IOutputStream& operator<<(
Difference& _diff) Base::IOutputStream& _os, Difference& _diff)
{ {
// TODO: use string description array // TODO: use string description array
return _os << _diff.type_text() << " " << _diff.dscr_; return _os << _diff.type_text() << " " << _diff.dscr_;
...@@ -119,7 +116,6 @@ private: ...@@ -119,7 +116,6 @@ private:
String dscr_; String dscr_;
}; };
/*! /*!
Base class for test checksums. Whatever check we want to add in the test system, Base class for test checksums. Whatever check we want to add in the test system,
it must be an instance of a class derived from Checksum. All derived classes it must be an instance of a class derived from Checksum. All derived classes
...@@ -128,12 +124,11 @@ must be instantiated as global variables. ...@@ -128,12 +124,11 @@ must be instantiated as global variables.
class Object class Object
{ {
public: public:
//! Checksum name. //! Checksum name.
const char* const name() const { return name_; } const char* const name() const { return name_; }
//! Add a record the checksum (generic version) //! Add a record the checksum (generic version)
template <typename T> template <typename T> void record(const Result& _rslt, const T& _data)
void record(const Result& _rslt, const T& _data)
{ {
Base::OStringStream strm; Base::OStringStream strm;
strm << _data; strm << _data;
...@@ -146,15 +141,15 @@ public: ...@@ -146,15 +141,15 @@ public:
/*! /*!
Compare two existing records (old and new). Compare two existing records (old and new).
Returns a qualification and a description of the difference. Returns a qualification and a description of the difference.
The default implementation has some intelligence in comparing the record The default implementation has some intelligence in comparing the record
results, but compares the the data simply as strings (no parsing). results, but compares the the data simply as strings (no parsing).
*/ */
virtual Difference compare( virtual Difference compare(
const Path& _old_path, //!<[in] Path to the left record const Path& _old_path, //!<[in] Path to the left record
const Record& _old_rcrd, //!<[in] "Left" record const Record& _old_rcrd, //!<[in] "Left" record
const Path& _new_path, //!<[in] Path to the right record const Path& _new_path, //!<[in] Path to the right record
const Record& _new_rcrd //!<[in] "Right" record const Record& _new_rcrd //!<[in] "Right" record
) const; ) const;
//! Get if the checksum should be run //! Get if the checksum should be run
bool allow() const { return lvl_ <= run_lvl; } bool allow() const { return lvl_ <= run_lvl; }
...@@ -184,23 +179,33 @@ private: ...@@ -184,23 +179,33 @@ private:
}; };
/*! /*!
Definition of the checksums registry. It is a map from a string (that is the Definition of the checksums registry. It is a map from a string (that is the
checksum name to an IChecksum. checksum name to an IChecksum.
*/ */
typedef std::map<String, Object*> Registry; typedef std::map<String, Object*> Registry;
/*! /*!
Function to get a static map with all the registered checksums. Function to get a static map with all the registered checksums.
*/ */
const Registry& registry(); const Registry& registry();
}//namespace Checksum } // namespace Checksum
}//namespace Test } // namespace Test
#define TEST(CHKSM, RCRD) { if (CHKSM.allow()) { CHKSM.RCRD; } } #define TEST(CHKSM, RCRD) \
#define TEST_if(CNDT, CHKSM, RCRD) { if (CNDT) TEST(CHKSM, RCRD) } { \
if (Test::Checksum::CHKSM.allow()) \
{ \
Test::Checksum::CHKSM.RCRD; \
} \
}
#endif//TEST_ON #define TEST_if(CNDT, CHKSM, RCRD) \
{ \
if (CNDT) \
TEST(CHKSM, RCRD) \
}
#endif // TEST_ON
#endif//BASE_ICHECKSUM_HH_INCLUDE #endif // BASE_ICHECKSUM_HH_INCLUDE
...@@ -30,8 +30,7 @@ public: ...@@ -30,8 +30,7 @@ public:
}//namespace Checksum }//namespace Checksum
}//namespace Test }//namespace Test
#define TEST_CHECKSUM_FILE(VRBL, NAME) \ #define TEST_CHECKSUM_FILE(VRBL, NAME) File VRBL(NAME"-file", File::TAG_USED)
Test::Checksum::File VRBL(NAME"-file", Test::Checksum::File::TAG_USED)
#else//TEST_ON #else//TEST_ON
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment