Commit c3c97227 authored by Pavel Vainerman's avatar Pavel Vainerman

(tests): добавил тесты для TriggerOR и Trigger

parent a510d139
...@@ -86,7 +86,6 @@ class TriggerOR ...@@ -86,7 +86,6 @@ class TriggerOR
inline bool state(){ return out; } inline bool state(){ return out; }
bool getState(InputType in); bool getState(InputType in);
bool commit(InputType in, bool state); bool commit(InputType in, bool state);
...@@ -115,7 +114,6 @@ class TriggerOR ...@@ -115,7 +114,6 @@ class TriggerOR
bool out; bool out;
Caller* cal; Caller* cal;
Action act; Action act;
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
template<class Caller, typename InputType> template<class Caller, typename InputType>
TriggerOR<Caller,InputType>::TriggerOR(Caller* c, Action a): TriggerOR<Caller,InputType>::TriggerOR(Caller* c, Action a):
out(false),
cal(c), cal(c),
act(a) act(a)
{ {
...@@ -84,21 +85,16 @@ template<class Caller, typename InputType> ...@@ -84,21 +85,16 @@ template<class Caller, typename InputType>
void TriggerOR<Caller,InputType>::check() void TriggerOR<Caller,InputType>::check()
{ {
bool old = out; bool old = out;
for( typename InputMap::iterator it=inputs.begin(); it!=inputs.end(); ++it ) for( auto it=inputs.begin(); it!=inputs.end(); ++it )
{ {
if( it->second ) if( it->second )
{ {
// если хоть один вход "1" на выходе "1" // если хоть один вход "1" выставляем на выходе "1"
// и прекращаем дальнейший поиск // и прекращаем дальнейший поиск
out = true; out = true;
if( old != out ) if( old != out )
{ (cal->*act)(out);
// try
// {
(cal->*act)(out);
// }
// catch(...){}
}
return; return;
} }
} }
...@@ -106,13 +102,7 @@ void TriggerOR<Caller,InputType>::check() ...@@ -106,13 +102,7 @@ void TriggerOR<Caller,InputType>::check()
out = false; out = false;
if( old != out ) if( old != out )
{ (cal->*act)(out);
// try
// {
(cal->*act)(out);
// }
// catch(...){}
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
template<class Caller, typename InputType> template<class Caller, typename InputType>
......
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
# This file is part of the UniSet library # # This file is part of the UniSet library #
############################################################################ ############################################################################
check_PROGRAMS = tests tests_with_conf #check_PROGRAMS = tests tests_with_conf
noinst_PROGRAMS = tests tests_with_conf
#umutex threadtst dlog #umutex threadtst dlog
tests_SOURCES = tests.cc passivetimer.cc hourglass.cc delaytimer.cc unixml.cc sscanf_hex.cc \ tests_SOURCES = tests.cc passivetimer.cc hourglass.cc delaytimer.cc unixml.cc sscanf_hex.cc \
callbacktimer.cc callbacktimer.cc trigger.cc triggerOR.cc triggerAND.cc
tests_LDADD = $(top_builddir)/lib/libUniSet2.la $(SIGC_LIBS) $(COMCPP_LIBS) tests_LDADD = $(top_builddir)/lib/libUniSet2.la $(SIGC_LIBS) $(COMCPP_LIBS)
tests_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch $(SIGC_CFLAGS) $(COMCPP_CFLAGS) tests_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
......
#include <catch.hpp>
// --------------------------------------------------------------------------
#include "Trigger.h"
// --------------------------------------------------------------------------
using namespace std;
// --------------------------------------------------------------------------
TEST_CASE( "Trigger", "[Trigger]" )
{
SECTION("Constructor")
{
Trigger tr;
CHECK_FALSE( tr.hi(false) );
CHECK( tr.hi(true) );
Trigger trHi;
CHECK( tr.low(false) );
CHECK( tr.hi(true) );
}
SECTION("Working")
{
Trigger tr;
CHECK_FALSE( tr.low(false) );
CHECK_FALSE( tr.change(false) );
CHECK( tr.hi(true) );
CHECK_FALSE( tr.change(true) );
CHECK( tr.low(false) );
CHECK( tr.change(true) );
CHECK( tr.low(false) );
CHECK( tr.hi(true) );
CHECK_FALSE( tr.hi(false) );
}
}
#include <catch.hpp>
#include "TriggerOR.h"
#include "UniSetTypes.h"
using namespace std;
class MyTestClass
{
public:
MyTestClass():num(0){}
~MyTestClass(){}
void setOut( bool newstate )
{
num++;
}
inline int getNum(){ return num; }
private:
int num;
};
TEST_CASE("TriggerOR", "[TriggerOR]" )
{
SECTION("Constructor")
{
MyTestClass tc;
TriggerOR<MyTestClass, int> tr(&tc, &MyTestClass::setOut);
REQUIRE( tc.getNum() == 0 );
tr.add(1, true);
tr.add(2, false);
tr.add(3, false);
REQUIRE( tc.getNum() == 1 );
CHECK( tr.state() );
CHECK( tr.getState(1) );
CHECK_FALSE( tr.getState(2) );
CHECK_FALSE( tr.getState(3) );
}
SECTION("Working")
{
MyTestClass tc;
TriggerOR<MyTestClass, bool> tr(&tc, &MyTestClass::setOut);
REQUIRE( tc.getNum() == 0 );
tr.add(1, true);
REQUIRE( tc.getNum() == 1 );
CHECK( tr.state() );
tr.add(2, false);
REQUIRE( tc.getNum() == 1 );
tr.add(3, false);
REQUIRE( tc.getNum() == 1 );
CHECK( tr.state() );
CHECK( tr.getState(1) );
CHECK_FALSE( tr.getState(2) );
CHECK_FALSE( tr.getState(3) );
tr.reset();
REQUIRE( tc.getNum() == 2 );
CHECK_FALSE( tr.state() );
CHECK( tr.getState(1) );
CHECK_FALSE( tr.getState(2) );
CHECK_FALSE( tr.getState(3) );
tr.commit(1,false);
tr.commit(2,true);
CHECK( tr.state() );
REQUIRE( tc.getNum() == 3 );
CHECK_FALSE( tr.getState(1) );
CHECK( tr.getState(2) );
CHECK_FALSE( tr.getState(3) );
tr.remove(2);
CHECK_FALSE( tr.getState(1) );
CHECK_FALSE( tr.getState(3) );
CHECK_FALSE( tr.state() );
REQUIRE( tc.getNum() == 4 );
// обращение к несуществующему входу
CHECK_FALSE( tr.getState(10) );
CHECK_FALSE( tr.getState(-10) );
CHECK_FALSE( tr.getState(0) );
tr.commit(10,true);
CHECK_FALSE( tr.state() );
tr.commit(-10,true);
CHECK_FALSE( tr.state() );
tr.commit(0,true);
CHECK_FALSE( tr.state() );
}
}
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