diff options
author | randomdan <randomdan@localhost> | 2013-03-28 17:51:07 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2013-03-28 17:51:07 +0000 |
commit | bb0cf483610bc2c6dc1ee242eac561521aebe887 (patch) | |
tree | c0beaf7e9aad6fd55bfd6a88203f874e188b126b /project2/basics/tests | |
parent | A stream processing module that reads sets of name=value pairs (diff) | |
download | project2-bb0cf483610bc2c6dc1ee242eac561521aebe887.tar.bz2 project2-bb0cf483610bc2c6dc1ee242eac561521aebe887.tar.xz project2-bb0cf483610bc2c6dc1ee242eac561521aebe887.zip |
skip 1 shuffle of code out of common
Diffstat (limited to 'project2/basics/tests')
-rw-r--r-- | project2/basics/tests/compoundTest.cpp | 79 | ||||
-rw-r--r-- | project2/basics/tests/compoundTest.h | 16 | ||||
-rw-r--r-- | project2/basics/tests/equals.cpp | 24 | ||||
-rw-r--r-- | project2/basics/tests/isdistinct.cpp | 40 | ||||
-rw-r--r-- | project2/basics/tests/isuniq.cpp | 48 | ||||
-rw-r--r-- | project2/basics/tests/validDateCheck.cpp | 66 |
6 files changed, 273 insertions, 0 deletions
diff --git a/project2/basics/tests/compoundTest.cpp b/project2/basics/tests/compoundTest.cpp new file mode 100644 index 0000000..b361db5 --- /dev/null +++ b/project2/basics/tests/compoundTest.cpp @@ -0,0 +1,79 @@ +#include <pch.hpp> +#include "compoundTest.h" +#include <scriptLoader.h> +#include <boost/foreach.hpp> +#include <boost/bind.hpp> +#include <algorithm> + +StaticMessageException(NoTestsToPerform, "No tests to perform"); + +CompoundTest::CompoundTest(ScriptNodePtr s) : + SourceObject(s), + Test(s) +{ + s->script->loader.addLoadTarget(s, Storer::into<ElementLoader>(&tests)); +} + +class All : public CompoundTest { + public: + All(ScriptNodePtr s) : + SourceObject(s), + CompoundTest(s) { + } + bool passes() const { + if (tests.empty()) { + throw NoTestsToPerform(); + } + return (std::find_if(tests.begin(), tests.end(), !boost::bind(&Test::passes, _1)) == tests.end()); + } +}; +DECLARE_LOADER("all", All); + +class Any : public CompoundTest { + public: + Any(ScriptNodePtr s) : + SourceObject(s), + CompoundTest(s) { + } + bool passes() const { + if (tests.empty()) { + throw NoTestsToPerform(); + } + return (std::find_if(tests.begin(), tests.end(), boost::bind(&Test::passes, _1)) != tests.end()); + } +}; +DECLARE_LOADER("any", Any); + +class None : public CompoundTest { + public: + None(ScriptNodePtr s) : + SourceObject(s), + CompoundTest(s) { + } + bool passes() const { + if (tests.empty()) { + throw NoTestsToPerform(); + } + return (std::find_if(tests.begin(), tests.end(), boost::bind(&Test::passes, _1)) == tests.end()); + } +}; +DECLARE_LOADER("none", None); + +class Not : public Test { + public: + Not(ScriptNodePtr s) : + SourceObject(s), + Test(s) + { + s->script->loader.addLoadTarget(s, Storer::into<ElementLoader>(&test)); + } + bool passes() const { + if (!test) { + throw NoTestsToPerform(); + } + return !test->passes(); + } + private: + TestPtr test; +}; +DECLARE_LOADER("not", Not); diff --git a/project2/basics/tests/compoundTest.h b/project2/basics/tests/compoundTest.h new file mode 100644 index 0000000..f65e954 --- /dev/null +++ b/project2/basics/tests/compoundTest.h @@ -0,0 +1,16 @@ +#ifndef IF_H +#define IF_H + +#include <test.h> + +class CompoundTest : public Test { + public: + CompoundTest(ScriptNodePtr); + + protected: + typedef ANONORDEREDSTORAGEOF(Test) Tests; + Tests tests; +}; + +#endif + diff --git a/project2/basics/tests/equals.cpp b/project2/basics/tests/equals.cpp new file mode 100644 index 0000000..ba8c695 --- /dev/null +++ b/project2/basics/tests/equals.cpp @@ -0,0 +1,24 @@ +#include <pch.hpp> +#include <test.h> +#include <scriptLoader.h> +#include <rowProcessor.h> + +class Equals : public Test { + public: + Equals(ScriptNodePtr s) : + SourceObject(s), + Test(s), + a(s, "a"), + b(s, "b") + { + } + + bool passes() const { + return (a() == b()); + } + + private: + Variable a, b; +}; +DECLARE_LOADER("equals", Equals); + diff --git a/project2/basics/tests/isdistinct.cpp b/project2/basics/tests/isdistinct.cpp new file mode 100644 index 0000000..06d887e --- /dev/null +++ b/project2/basics/tests/isdistinct.cpp @@ -0,0 +1,40 @@ +#include <pch.hpp> +#include <test.h> +#include <scriptLoader.h> +#include <iHaveParameters.h> +#include <rowProcessor.h> + +class IsDistinct : public Test, IHaveParameters { + public: + IsDistinct(ScriptNodePtr s) : + SourceObject(s), + Test(s), + IHaveParameters(s), + scope(s, "scope") + { + } + + void loadComplete(const CommonObjects *) + { + findComponent(scope())->registerFor(RowProcessor::Complete, boost::bind(&IsDistinct::reset, this)); + } + + bool passes() const { + Vars row; + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + row.push_back(p.second()); + } + return previous.insert(row).second; + } + + void reset() const { + previous.clear(); + } + + private: + Variable scope; + typedef std::vector<VariableType> Vars; + typedef std::set<Vars> Rows; + mutable Rows previous; +}; +DECLARE_LOADER("isdistinct", IsDistinct); diff --git a/project2/basics/tests/isuniq.cpp b/project2/basics/tests/isuniq.cpp new file mode 100644 index 0000000..548be14 --- /dev/null +++ b/project2/basics/tests/isuniq.cpp @@ -0,0 +1,48 @@ +#include <pch.hpp> +#include <test.h> +#include <scriptLoader.h> +#include <iHaveParameters.h> +#include <rowProcessor.h> + +class IsUniq : public Test, IHaveParameters { + public: + IsUniq(ScriptNodePtr s) : + SourceObject(s), + Test(s), + IHaveParameters(s), + scope(s, "scope") + { + } + + void loadComplete(const CommonObjects *) + { + findComponent(scope())->registerFor(RowProcessor::Complete, boost::bind(&IsUniq::reset, this)); + } + + bool passes() const { + if (previous.size() > 0) { + Vars row; + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + row.push_back(p.second()); + } + std::swap(row, previous); + return row != previous; + } + else { + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + previous.push_back(p.second()); + } + return true; + } + } + + void reset() const { + previous.clear(); + } + + private: + Variable scope; + typedef std::vector<VariableType> Vars; + mutable Vars previous; +}; +DECLARE_LOADER("isuniq", IsUniq); diff --git a/project2/basics/tests/validDateCheck.cpp b/project2/basics/tests/validDateCheck.cpp new file mode 100644 index 0000000..8dffd9d --- /dev/null +++ b/project2/basics/tests/validDateCheck.cpp @@ -0,0 +1,66 @@ +#include <pch.hpp> +#include <logger.h> +#include <scriptLoader.h> +#include <commonObjects.h> +#include <test.h> +#include <variables.h> +#include <scripts.h> + +class ValidDateTest : public Test { + public: + ValidDateTest(ScriptNodePtr p) : + SourceObject(p), + Test(p), + applyTo(p, "apply-to"), + format(p, "format"), + warnLev(p->value("warn", true).as<bool>() ? LOG_WARNING : LOG_INFO) + { + } + + ~ValidDateTest() + { + } + + bool + passes() const + { + struct tm tm, ftm; + memset(&tm, 0, sizeof(struct tm)); + mktime(&tm); + const char * at = applyTo(); + const char * f = format(); + const char * s = strptime(at, f, &tm); + if (!s || *s) { + Logger()->messagef(warnLev, "%s: check failed (parse) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + ftm = tm; + if (mktime(&ftm) == -1) { + Logger()->messagef(warnLev, "%s: check failed (normalise) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + if (tm.tm_year != ftm.tm_year || + tm.tm_mon != ftm.tm_mon || + tm.tm_mday != ftm.tm_mday || + tm.tm_hour != (ftm.tm_hour - ftm.tm_isdst) || + tm.tm_min != ftm.tm_min || + tm.tm_sec != ftm.tm_sec) { + Logger()->messagef(LOG_INFO, "tm: %d %d %d %d %d %d", + tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + Logger()->messagef(LOG_INFO, "ftm: %d %d %d %d %d %d", + ftm.tm_year, ftm.tm_mon, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec); + Logger()->messagef(warnLev, "%s: check failed (verify) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + return true; + } + Variable applyTo; + Variable format; + int warnLev; +}; + +DECLARE_LOADER("validdatetest", ValidDateTest); + |