Commit eb49d3c1 authored by Pavel Vainerman's avatar Pavel Vainerman

(tests): добавил предварительный тест класса 'Configuration'

parent 4491159f
......@@ -243,7 +243,8 @@ namespace UniSetTypes
// Инициализация UniSetTypes::conf.
// ( учитываются параметры командной строки --confile и --id-from-config )
void uniset_init( int argc, const char* const* argv, const std::string& xmlfile="configure.xml" );
// force - инициализировать принудительно, даже если это повторная инициализация
void uniset_init( int argc, const char* const* argv, const std::string& xmlfile="configure.xml", bool force = false );
} // end of UniSetTypes namespace
......
......@@ -229,7 +229,7 @@ void Configuration::initConfiguration( int argc, const char* const* argv )
}
}
}
// Настраиваем отладочные логи
initDebug(ulog,"UniSetDebug");
......@@ -978,10 +978,18 @@ UniversalIO::IOType Configuration::getIOType( const std::string& name )
return UniversalIO::UnknownIOType;
}
// -------------------------------------------------------------------------
void uniset_init( int argc, const char* const* argv, const std::string& xmlfile )
void uniset_init( int argc, const char* const* argv, const std::string& xmlfile, bool force )
{
string confile = UniSetTypes::getArgParam( "--confile", argc, argv, xmlfile );
ulog.setLogName("ulog");
if( UniSetTypes::conf )
{
if( !force && confile == UniSetTypes::conf->getConfFileName() )
return;
delete UniSetTypes::conf;
}
UniSetTypes::conf = new Configuration(argc, argv, confile);
}
// -------------------------------------------------------------------------
......
......@@ -2,25 +2,17 @@
# This file is part of the UniSet library #
############################################################################
noinst_PROGRAMS = tests
#hourglass delaytimer unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog
noinst_PROGRAMS = tests tests_with_conf
#unixml ui umutex conftest iterator_test sscanf_hex calibration threadtst dlog
tests_SOURCES = tests.cc passivetimer.cc hourglass.cc delaytimer.cc
tests_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_CPPFLAGS = -I$(top_builddir)/include -I$(includeir)/Catch
#passivetimer_SOURCES = passivetimer.cc
#passivetimer_LDADD = $(top_builddir)/lib/libUniSet2.la
#passivetimer_CPPFLAGS = -I$(top_builddir)/include -ICatch
tests_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch
# hourglass_SOURCES = hourglass.cc
# hourglass_LDADD = $(top_builddir)/lib/libUniSet2.la
# hourglass_CPPFLAGS = -I$(top_builddir)/include
#
# delaytimer_SOURCES = delaytimer.cc
# delaytimer_LDADD = $(top_builddir)/lib/libUniSet2.la
# delaytimer_CPPFLAGS = -I$(top_builddir)/include
#
tests_with_conf_SOURCES = tests_with_conf.cc conftest.cc
tests_with_conf_LDADD = $(top_builddir)/lib/libUniSet2.la
tests_with_conf_CPPFLAGS = -I$(top_builddir)/include -I$(includedir)/Catch
# unixml_SOURCES = unixml.cc
# unixml_LDADD = $(top_builddir)/lib/libUniSet2.la ${SIGC_LIBS}
# unixml_CPPFLAGS = -I$(top_builddir)/include ${SIGC_CFLAGS}
......
// --------------------------------------------------------------------------
#include <string>
#include "Debug.h"
#include "Configuration.h"
#include "ORepHelpers.h"
// --------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// --------------------------------------------------------------------------
int main(int argc, const char **argv)
{
// if( argc>1 && strcmp(argv[1],"--help")==0 )
// {
// cout << "--confile - Configuration file. Default: test.xml" << endl;
// return 0;
// }
try
{
// string confile = UniSetTypes::getArgParam( "--confile", argc, argv, "test.xml" );
// conf = new Configuration(argc, argv, confile);
for( int i=-5; i<4095; i+=100 )
{
cout << "raw=" << (817+i)
<< " cal=" << lcalibrate(817+i,817,4095,0,400)
<< endl;
}
return 0;
}
catch(SystemError& err)
{
cerr << "(calibration): " << err << endl;
}
catch(Exception& ex)
{
cerr << "(calibration): " << ex << endl;
}
catch(...)
{
cerr << "(calibration): catch(...)" << endl;
}
return 1;
}
#include <catch.hpp>
// --------------------------------------------------------------------------
#include <string>
#include "Debug.h"
#include "Configuration.h"
#include "ORepHelpers.h"
// --------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// --------------------------------------------------------------------------
int main(int argc, const char **argv)
{
if( argc>1 && strcmp(argv[1],"--help")==0 )
{
cout << "--confile - Configuration file. Default: test.xml" << endl;
return 0;
}
cout << "**** uni_atoi('')=" << uni_atoi("") << endl;
TEST_CASE( "Configuration", "[Configuration]" )
{
assert( conf != 0 );
assert( conf->oind != 0 );
// смотри tests_with_conf.xml
ObjectId testID = 1;
const std::string testSensor("Input1_S");
const std::string testObject("TestProc");
const std::string testController("SharedMemory");
const std::string testService("TimeService");
// CHECK( conf != 0 );
// CHECK( conf->oind != 0 );
SECTION( "ObjectIndex tests.." )
{
CHECK( conf->getLocalNode()!=DefaultObjectId );
CHECK( conf->oind->getTextName(testID) != "" );
string ln("/Projects/Sensors/VeryVeryLongNameSensor_ForTest_AS");
string ln_short("VeryVeryLongNameSensor_ForTest_AS");
REQUIRE( ORepHelpers::getShortName(ln) == ln_short );
CHECK( conf->oind->getNameById(testID) != "" );
string mn(conf->oind->getMapName(testID));
CHECK( mn != "" );
CHECK( conf->oind->getIdByName(mn) != DefaultObjectId );
CHECK( conf->getIOType(testID) != UniversalIO::UnknownIOType );
CHECK( conf->getIOType(mn) != UniversalIO::UnknownIOType );
CHECK( conf->getIOType(testSensor) != UniversalIO::UnknownIOType );
}
SECTION( "Arguments" )
{
xmlNode* cnode = conf->getNode("testnode");
CHECK( cnode != NULL );
UniXML_iterator it(cnode);
CHECK( conf->getArgInt("--prop-id2",it.getProp("id2")) != 0 );
CHECK( conf->getArgInt("--prop-dummy",it.getProp("id2")) == -100 );
CHECK( conf->getArgPInt("--prop-id2",it.getProp("id2"),0) != 0 );
CHECK( conf->getArgPInt("--prop-dummy",it.getProp("dummy"),20) == 20 );
CHECK( conf->getArgPInt("--prop-dummy",it.getProp("dummy"),0) == 0 );
}
SECTION( "XML sections" )
{
CHECK( conf->getXMLSensorsSection() != 0 );
CHECK( conf->getXMLObjectsSection() != 0 );
CHECK( conf->getXMLControllersSection() != 0 );
CHECK( conf->getXMLServicesSection() != 0 );
CHECK( conf->getXMLNodesSection() != 0 );
CHECK( conf->getSensorID(testSensor) != DefaultObjectId );
CHECK( conf->getObjectID(testObject) != DefaultObjectId );
CHECK( conf->getControllerID(testController) != DefaultObjectId );
CHECK( conf->getServiceID(testService) != DefaultObjectId );
}
SECTION( "Empty Constructor" )
{
int t_argc = 0;
char t_argv[]={""};
REQUIRE_THROWS_AS( Configuration(t_argc,(const char* const*)(t_argv),""), UniSetTypes::SystemError );
}
// SECTION( "ObjectIndex Constructor" )
// SECTION( "ObjectsMap Constructor" )
SECTION( "Bad conf: no <ObjectsMap>" )
{
int t_argc = 0;
char t_argv[]={""};
ulog.level(Debug::NONE);
REQUIRE_THROWS_AS( Configuration(t_argc,(const char* const*)(t_argv),"tests_no_objectsmap.xml"), UniSetTypes::SystemError );
}
SECTION( "Bad conf: no <UniSet>" )
{
int t_argc = 0;
char t_argv[]={""};
ulog.level(Debug::NONE);
REQUIRE_THROWS_AS( Configuration(t_argc,(const char* const*)(t_argv),"tests_no_uniset_section.xml"), UniSetTypes::SystemError );
}
}
#if 0
try
{
string confile = UniSetTypes::getArgParam( "--confile", argc, argv, "test.xml" );
conf = new Configuration(argc, argv, confile);
cout << "getLocalNode()=" << conf->getLocalNode() << endl;
string t(conf->oind->getTextName(1));
cout << "**** check getTextName: " << ( t.empty() ? "FAILED" : "OK" ) << endl;
string ln("/Projects/Sensors/VeryVeryLongNameSensor_ForTest_AS");
string ln_t("VeryVeryLongNameSensor_ForTest_AS");
cout << "**** check getShortName: " << ( ln_t == ORepHelpers::getShortName(ln) ? "OK" : "FAILED" ) << endl;
string mn(conf->oind->getMapName(1));
cout << "**** check getMapName: " << ( mn.empty() ? "FAILED" : "OK" ) << endl;
cout << "getSensorID(Input1_S): " << conf->getSensorID("Input1_S") << endl;
std::string iname = conf->oind->getNameById(1);
cout << "getNameById(1): " << iname << endl;
ObjectId i_id = conf->oind->getIdByName(mn);
cout << "getIdByName(" << iname << "): " << (i_id == DefaultObjectId ? "FAIL" : "OK" ) << endl;
UniversalIO::IOType t1=conf->getIOType(1);
cout << "**** getIOType for " << mn << endl;
cout << "**** check getIOType(id): (" << t1 << ") " << ( t1 == UniversalIO::UnknownIOType ? "FAILED" : "OK" ) << endl;
UniversalIO::IOType t2=conf->getIOType(mn);
cout << "**** check getIOType(name): (" << t2 << ") " << ( t2 == UniversalIO::UnknownIOType ? "FAILED" : "OK" ) << endl;
UniversalIO::IOType t3=conf->getIOType("Input1_S");
cout << "**** check getIOType(name): for short name 'Input1_S': (" << t3 << ") " << ( t3 == UniversalIO::UnknownIOType ? "FAILED" : "OK" ) << endl;
int i1 = uni_atoi("-100");
cout << "**** check uni_atoi: '-100' " << ( ( i1 != -100 ) ? "FAILED" : "OK" ) << endl;
......@@ -104,3 +154,4 @@ int main(int argc, const char **argv)
return 1;
}
#endif
\ No newline at end of file
#include <Catch/catch.hpp>
#include <catch.hpp>
#include "DelayTimer.h"
#include "UniSetTypes.h"
......@@ -107,146 +107,3 @@ TEST_CASE("DelayTimer", "[DelayTimer]" )
CHECK_FALSE( dt2.get() );
}
}
#if 0
#include <iostream>
using namespace std;
#include "HourGlass.h"
#include "DelayTimer.h"
#include "UniSetTypes.h"
int main()
{
// ----------------------
// test DelayTimer
DelayTimer dtm(1000,500);
if( dtm.check(true) )
{
cerr << "DelayTimer: TEST1 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST1 OK!" << endl;
msleep(1100);
if( !dtm.check(true) )
{
cerr << "DelayTimer: TEST2 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST2 OK!" << endl;
if( !dtm.check(false) )
{
cerr << "DelayTimer: TEST3 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST3 OK!" << endl;
msleep(200);
if( !dtm.check(false) )
{
cerr << "DelayTimer: TEST4 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST4 OK!" << endl;
msleep(500);
if( dtm.check(false) )
{
cerr << "DelayTimer: TEST5 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST5 OK!" << endl;
dtm.check(true);
msleep(800);
if( dtm.check(true) )
{
cerr << "DelayTimer: TEST6 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST6 OK!" << endl;
dtm.check(false);
msleep(600);
if( dtm.check(false) )
{
cerr << "DelayTimer: TEST7 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST7 OK!" << endl;
dtm.check(true);
msleep(1100);
if( !dtm.check(true) )
{
cerr << "DelayTimer: TEST8 FAILED! " << endl;
return 1;
}
cout << "DelayTimer: TEST8 OK!" << endl;
DelayTimer dtm2(200,0);
dtm2.check(true);
msleep(250);
if( !dtm2.check(true) )
{
cerr << "DelayTimer: TEST9 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST9 OK! " << endl;
if( dtm2.check(false) )
{
cerr << "DelayTimer: TEST10 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST10 OK! " << endl;
DelayTimer dtm3(200,100);
dtm3.check(true);
msleep(190);
dtm3.check(false);
dtm3.check(true);
msleep(50);
if( dtm3.check(true) )
{
cerr << "DelayTimer: TEST11 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST11 OK! " << endl;
msleep(200);
if( !dtm3.check(true) )
{
cerr << "DelayTimer: TEST12 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST12 OK! " << endl;
dtm3.check(false);
msleep(90);
dtm3.check(true);
msleep(50);
if( !dtm3.check(false) )
{
cerr << "DelayTimer: TEST13 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST13 OK! " << endl;
msleep(150);
if( dtm3.check(false) )
{
cerr << "DelayTimer: TEST14 FAILED! " << endl;
return 1;
}
cerr << "DelayTimer: TEST14 OK! " << endl;
return 0;
}
#endif
\ No newline at end of file
#include <Catch/catch.hpp>
#include <catch.hpp>
#include <cmath>
......
#include <Catch/catch.hpp>
#include <catch.hpp>
#include "PassiveTimer.h"
#include "UniSetTypes.h"
......
#define CATCH_CONFIG_MAIN
#include <Catch/catch.hpp>
// не удивляйтесь, что тут нет main().. она спрятана в catch.cpp :)
<?xml version = '1.0' encoding = 'UTF-8' ?>
<UNISETPLC>
<UserData></UserData>
<!-- Общие(стартовые) параметры по UniSet -->
<UniSet>
<NameService host="localhost" port="2809"/>
<LocalNode name="LocalhostNode"/>
<RootSection name="UNISET_PLC"/>
<CountOfNet name="1"/>
<RepeatCount name="3"/>
<RepeatTimeoutMS name="50"/>
<WatchDogTime name="0"/> <!-- [мин] -->
<PingNodeTime name="0"/> <!-- [сек] -->
<AutoStartUpTime name="1"/> <!-- [сек] -->
<DumpStateTime name="10"/><!-- [сек] -->
<SleepTickMS name="500"/><!-- [мсек] -->
<!-- Debug, logs -->
<UniSetDebug name="unideb" levels="crit,warn" file=""/>
<!-- Если не указано, то считается текущий каталог -->
<ConfDir name=""/>
<DataDir name=""/>
<BinDir name=""/>
<LogDir name=""/>
<DocDir name=""/>
<LockDir name=""/>
<testnode name="testnode" id2="-100"/>
<Services>
<LocalTimeService name="TimeService" MaxCountTimers="100" AskLifeTimeSEC="10"/>
<LocalInfoServer name="InfoServer" dbrepeat="1">
<RouteList>
</RouteList>
</LocalInfoServer>
<LocalDBServer name="DBServer" dbnode="" dbname="UNISET_PLC" dbuser="dbadmin" dbpass="dbadmin" pingTime="60000" reconnectTime="30000"/>
<LocalPrintServer device="/dev/lp0" checkTime="5000" />
</Services>
</UniSet>
<dlog name="dlog" levels="" file=""/>
<messages name="messages" idfromfile="1" >
</messages>
</UNISETPLC>
<?xml version = '1.0' encoding = 'UTF-8' ?>
<UNISETPLC>
<UserData></UserData>
<dlog name="dlog" levels="" file=""/>
<ObjectsMap idfromfile="1">
<!--
Краткие пояснения к полям секции 'sensors'
==========================================
node - узел на котором физически находится данный датчик
iotype - тип датчика
priority - приоритет сообщения об изменении данного датчика
textname - текстовое имя датчика
-->
<nodes port="2809">
<item id="1000" name="LocalhostNode" alias="" textname="Локальный узел" ip="127.0.0.1"/>
<item id="1001" name="Node2" alias="" textname="Локальный узел" ip="127.0.0.1"/>
</nodes>
<!-- ************************ Датчики ********************** -->
<sensors name="Sensors">
<item id="1" name="Input1_S" textname="Команда 1" node="" iotype="DI" priority="Medium" default="1" />
<item id="4" name="Input2_S" textname="Команда 2" node="" iotype="DI" priority="Medium" />
<item id="140" name="Input3_S" textname="Команда 3" node="" iotype="DI" priority="Medium"/>
</sensors>
<thresholds name="thresholds">
</thresholds>
<controllers name="Controllers">
<item id="90" name="SharedMemory"/>
</controllers>
<!-- ******************* Идентификаторы сервисов ***************** -->
<services name="Services">
<item id="99" name="TimeService"/>
</services>
<!-- ******************* Идентификаторы объектов ***************** -->
<objects name="UniObjects">
<item id="100" name="TestProc"/>
</objects>
</ObjectsMap>
<messages name="messages" idfromfile="1" >
</messages>
</UNISETPLC>
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
#include "Configuration.h"
int main( int argc, char* argv[] )
{
Catch::Session session;
UniSetTypes::uniset_init(argc,argv);
int returnCode = session.applyCommandLine( argc, argv, Catch::Session::OnUnusedOptions::Ignore );
if( returnCode != 0 ) // Indicates a command line error
return returnCode;
return session.run();
}
#!/bin/sh
# '--' - нужен для отделения аоргументов catch, от наших..
uniset-start.sh -f ./tests_with_conf -- --confile tests_with_conf.xml --prop-id2 -10 $*
<?xml version = '1.0' encoding = 'UTF-8' ?>
<UNISETPLC>
<UserData></UserData>
<!-- Общие(стартовые) параметры по UniSet -->
<UniSet>
<NameService host="localhost" port="2809"/>
<LocalNode name="LocalhostNode"/>
<RootSection name="UNISET_PLC"/>
<CountOfNet name="1"/>
<RepeatCount name="3"/>
<RepeatTimeoutMS name="50"/>
<WatchDogTime name="0"/> <!-- [мин] -->
<PingNodeTime name="0"/> <!-- [сек] -->
<AutoStartUpTime name="1"/> <!-- [сек] -->
<DumpStateTime name="10"/><!-- [сек] -->
<SleepTickMS name="500"/><!-- [мсек] -->
<!-- Debug, logs -->
<UniSetDebug name="unideb" levels="crit,warn" file=""/>
<!-- Если не указано, то считается текущий каталог -->
<ConfDir name=""/>
<DataDir name=""/>
<BinDir name=""/>
<LogDir name=""/>
<DocDir name=""/>
<LockDir name=""/>
<testnode name="testnode" id2="-100"/>
<Services>
<LocalTimeService name="TimeService" MaxCountTimers="100" AskLifeTimeSEC="10"/>
<LocalInfoServer name="InfoServer" dbrepeat="1">
<RouteList>
</RouteList>
</LocalInfoServer>
<LocalDBServer name="DBServer" dbnode="" dbname="UNISET_PLC" dbuser="dbadmin" dbpass="dbadmin" pingTime="60000" reconnectTime="30000"/>
<LocalPrintServer device="/dev/lp0" checkTime="5000" />
</Services>
</UniSet>
<dlog name="dlog" levels="" file=""/>
<ObjectsMap idfromfile="1">
<!--
Краткие пояснения к полям секции 'sensors'
==========================================
node - узел на котором физически находится данный датчик
iotype - тип датчика
priority - приоритет сообщения об изменении данного датчика
textname - текстовое имя датчика
-->
<nodes port="2809">
<item id="1000" name="LocalhostNode" alias="" textname="Локальный узел" ip="127.0.0.1"/>
<item id="1001" name="Node2" alias="" textname="Локальный узел" ip="127.0.0.1"/>
</nodes>
<!-- ************************ Датчики ********************** -->
<sensors name="Sensors">
<item id="1" name="Input1_S" textname="Команда 1" node="" iotype="DI" priority="Medium" default="1" />
<item id="4" name="Input2_S" textname="Команда 2" node="" iotype="DI" priority="Medium" />
<item id="140" name="Input3_S" textname="Команда 3" node="" iotype="DI" priority="Medium"/>
</sensors>
<thresholds name="thresholds">
</thresholds>
<controllers name="Controllers">
<item id="90" name="SharedMemory"/>
</controllers>
<!-- ******************* Идентификаторы сервисов ***************** -->
<services name="Services">
<item id="99" name="TimeService"/>
</services>
<!-- ******************* Идентификаторы объектов ***************** -->
<objects name="UniObjects">
<item id="100" name="TestProc"/>
</objects>
</ObjectsMap>
<messages name="messages" idfromfile="1" >
</messages>
</UNISETPLC>
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