Skip to content

Commit

Permalink
[tests] Добавление тестов для менеджера рецептов (#779)
Browse files Browse the repository at this point in the history
Co-authored-by: Dzmitry Ivaniuk <dzimitriy@gmail.com>
  • Loading branch information
Lenonim and idzm authored Oct 28, 2024
1 parent 0f8b5c2 commit 3c65a5c
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 29 deletions.
9 changes: 5 additions & 4 deletions common/PAC-driver/g_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ long device_communicator::write_devices_states_service(
printf( "cmd = %s\n", data + 1 );
#endif // DEBUG_DEV_CMCTR
int res;
if ( strstr(reinterpret_cast<char*>(data + 1), "__RECMAN") != nullptr)
auto str = reinterpret_cast<char*>( data + 1 );
if ( strstr( str, "__RECMAN" ) != nullptr )
{
res = G_PARAMS_RECIPE_MANAGER()->getInstance()->parseDriverCmd(reinterpret_cast<char*>(data) + 1);
res = G_PARAMS_RECIPE_MANAGER()->parseDriverCmd( str );
}
else
{
res = lua_manager::get_instance( )->exec_Lua_str(reinterpret_cast<char*>(data) + 1,
"CMD_EXEC_DEVICE_COMMAND " );
res = lua_manager::get_instance()->exec_Lua_str(
str, "CMD_EXEC_DEVICE_COMMAND " );
}

outdata[ 0 ] = 0;
Expand Down
128 changes: 127 additions & 1 deletion test/cip_tech_def_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,132 @@ TEST( cipline_tech_object, _DoStep )
G_LUA_MANAGER->free_Lua( );
}

TEST( cipline_tech_object, save_device )
{
auto L = lua_open();
G_LUA_MANAGER->set_Lua( L );

const int BUFF_SIZE = 5000;
char buff[ BUFF_SIZE ] = { 0 };


InitCipDevices();
cipline_tech_object cip1( "CIP1", 1, 1, "CIP1", 1, 1, 200, 200, 200, 200 );
lua_manager::get_instance()->set_Lua( lua_open() );

cip1.initline();
InitStationParams();

cip1.lineRecipes->ResetRecipeToDefaults(0);
cip1.save_device( buff );

std::string REF_STR0 = R"(t.CIP1 = t.CIP1 or {}
t.CIP1=
{
CMD=0,
STATE=0,
OPER=0,
LOADED_REC='Не выбран',
CUR_REC='',
LASTRECNMR=0,
LASTRECNAME='Не выбран',
CUR_PRG='Не выбран',
PRG_LIST='256##Наведение кислоты||512##Наведение щелочи||',
REC_LIST='',
NCAR='',
NCAR1='',
NCAR2='',
NCAR3='',
NCAR4='',
SWITCH1=0,
SWITCH2=0,
SWITCH3=0,
SWITCH4=0,
IDLE_TIME = '00:00:00',
S_PAR_F=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, )"
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " R"(
},
S_PAR_UI=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, )"
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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00, 200.00, 40.00, 15.00, 30.00, "
"300.00, 600.00, 600.00, 500.00, 500.00, 600.00, 600.00, 120.00, 0.00, 0.00, 0.00, "
"0.00, 0.00, 5.00, 600.00, 30.00, 500.00, 30.00, 8.00, 15.00, 2.00, 20.00, 20.00, "
"20.00, 95.00, 2.00, 30.00, 0.20, 500.00, 130.00, 0.00, 30.00, 0.00, 30.00, 0.00, "
"15.00, 0.50, 10.00, 0.10, 1000.00, 40.00, 0.00, 2.00, 0.00, 15.00, 0.00, 300.00, "
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, "
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, "
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, "
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, " R"(
},
OBJSTATS_LASTWASH='Не известно',
OBJSTATS_LASTPROGRAM='Не определено',
OBJSTATS_CAUSTICCOUNT=0,
OBJSTATS_LASTACIDWASH='Не известно',
CAUSTIC_REC_LIST='',
CAUSTIC_REC_NMR='0',
CAUSTICNAME='',
CAUSTIC_PAR_NAME='',
CAUSTIC_PAR =
{
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, )"
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, " R"(
},
ACID_REC_LIST='',
ACID_REC_NMR='0',
ACIDNAME='',
ACID_PAR_NAME='',
ACID_PAR =
{
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, )"
"0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, " R"(
}
}
)";
EXPECT_STREQ( REF_STR0.c_str(), buff );

G_LUA_MANAGER->free_Lua();
}
4 changes: 2 additions & 2 deletions test/device/PAC_dev_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ TEST( signal_column, blink )
EXPECT_STREQ( "test_HL1={M=0, ST=1, V=0, L_GREEN=1, L_YELLOW=0, L_RED=0, "
"L_BLUE=0, L_SIREN=0},\n", buff );

subhook_install( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_millisec(1001UL);
test_dev.slow_blink_green();
test_dev.save_device( buff, "" );
EXPECT_STREQ( "test_HL1={M=0, ST=1, V=0, L_GREEN=0, L_YELLOW=0, L_RED=0, "
Expand All @@ -546,7 +546,7 @@ TEST( signal_column, blink )
"L_BLUE=0, L_SIREN=0},\n", buff );

G_PAC_INFO()->emulation_on();
subhook_remove( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_default_time();
}

TEST( signal_column, set_rt_par )
Expand Down
44 changes: 39 additions & 5 deletions test/g_device_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
#include "g_device_tests.h"
#include "tcp_cmctr.h"
#include "lua_manager.h"
#include "device/manager.h"

using namespace ::testing;


TEST( device_communicator, write_devices_states_service )
{
auto L = lua_open();
G_LUA_MANAGER->set_Lua( L );

const int IN_BUFF_SIZE = 100;
const int OUT_BUFF_SIZE = 1000;
unsigned char data[ IN_BUFF_SIZE ] = { '\0' };
unsigned char data[ IN_BUFF_SIZE ] = { '\0' };
auto cmd_size = 1;
unsigned char out_data[ OUT_BUFF_SIZE ] = { '\0' };

tcp_communicator::init_instance( "Тест", "Test" );

device_communicator::switch_on_compression();

data[0] = device_communicator::CMD_GET_INFO_ON_CONNECT;
device_communicator::write_devices_states_service(0, data, out_data);
EXPECT_EQ('\0', out_data[0]);

data[ 0 ] = device_communicator::CMD_GET_INFO_ON_CONNECT;
G_DEVICE_CMMCTR->write_devices_states_service( cmd_size, data, out_data );
device_communicator::write_devices_states_service( cmd_size, data, out_data );
EXPECT_EQ( 'x', out_data[ 0 ] );

data[ 0 ] = device_communicator::CMD_GET_DEVICES;
G_DEVICE_CMMCTR->write_devices_states_service( cmd_size, data, out_data );
device_communicator::write_devices_states_service( cmd_size, data, out_data );
EXPECT_EQ( 'x', out_data[ 0 ] );

data[ 0 ] = device_communicator::CMD_GET_DEVICES_STATES;
G_DEVICE_CMMCTR->write_devices_states_service( cmd_size, data, out_data );
device_communicator::write_devices_states_service( cmd_size, data, out_data );
EXPECT_EQ( 'x', out_data[ 0 ] );

data[ 0 ] = device_communicator::CMD_GET_PAC_ERRORS;
G_DEVICE_CMMCTR->write_devices_states_service( cmd_size, data, out_data );
device_communicator::write_devices_states_service( cmd_size, data, out_data );
EXPECT_EQ( 'x', out_data[ 0 ] );

data[ 0 ] = device_communicator::CMD_EXEC_DEVICE_COMMAND;
device_communicator::write_devices_states_service( cmd_size, data, out_data );
EXPECT_EQ( 'x', out_data[ 0 ] );

unsigned char recman_data[IN_BUFF_SIZE] = " __RECMAN[1]:set_cmd( \"hello\", 1, 2.5 )";
recman_data[0] = device_communicator::CMD_EXEC_DEVICE_COMMAND;
device_communicator::write_devices_states_service(cmd_size, recman_data, out_data);
EXPECT_EQ('x', out_data[0]);

G_LUA_MANAGER->free_Lua();
}

TEST( device_communicator, print )
{
G_DEVICE_MANAGER()->clear_io_devices();

std::string STR_check = R"(Device communicator. Dev count = 1.
[ 0 ] Device manager
)";
testing::internal::CaptureStdout();
G_DEVICE_CMMCTR->print();
auto output = testing::internal::GetCapturedStdout();
EXPECT_EQ( output, STR_check );
EXPECT_TRUE( true );
}
37 changes: 33 additions & 4 deletions test/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,40 @@
#include <vector>
#include <limits.h>

unsigned long get_delta_millisec_as_1001( unsigned long time1 );
unsigned long subhook_for_get_delta_millisec( unsigned long );

unsigned long get_delta_millisec( unsigned long time1 );

const auto G_GET_DELTA_MILLISEC_HOOK_1001 = subhook_new(
reinterpret_cast<void*>( get_delta_millisec ),
reinterpret_cast<void*>( get_delta_millisec_as_1001 ),
const auto GTESTS_DELTA_MILLISEC_SUBHOOK = subhook_new(
reinterpret_cast<void*>( &get_delta_millisec ),
reinterpret_cast<void*>( &subhook_for_get_delta_millisec ),
SUBHOOK_64BIT_OFFSET );

class DeltaMilliSecSubHooker
{
inline static unsigned long millitime;

public:

static unsigned long get_millisec()
{
return millitime;
}

static void set_millisec( unsigned long millisec )
{
millitime = millisec;
subhook_install( GTESTS_DELTA_MILLISEC_SUBHOOK );
}

static void set_default_time()
{
millitime = 1UL;
subhook_remove( GTESTS_DELTA_MILLISEC_SUBHOOK );
}

explicit DeltaMilliSecSubHooker( unsigned long millisec )
{
DeltaMilliSecSubHooker::set_millisec( millisec );
}
};
8 changes: 4 additions & 4 deletions test/main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ int G_USE_LOG = 0; //Вывод в системный лог (syslog).
bool G_NO_IO_NODES = true; // По умолчанию обмен с модулями отключен.
bool G_READ_ONLY_IO_NODES = false;

unsigned long get_delta_millisec_as_1001( unsigned long time1 )
{
return 1001ul;
}
unsigned long subhook_for_get_delta_millisec(unsigned long)
{
return DeltaMilliSecSubHooker::get_millisec();
}

int main(int argc, char* argv[])
{
Expand Down
14 changes: 7 additions & 7 deletions test/operation_mngr_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,16 @@ TEST( operation, check_max_step_time )
test_op->start();
test_tank.evaluate();
EXPECT_EQ( operation::RUN, test_op->get_state() );
subhook_install( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_millisec(1001UL);
test_tank.evaluate();
EXPECT_EQ( operation::PAUSE, test_op->get_state() );
subhook_remove( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_default_time();

// После запуска опять в паузу из-за превышения времени.
test_op->start();
test_tank.evaluate();
EXPECT_EQ( operation::RUN, test_op->get_state() );
subhook_install( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_millisec(1001UL);
//Проверка на превышение максимльного времени шага.
const unsigned int ERR_STR_SIZE = 80;
char err_str[ ERR_STR_SIZE ] = {};
Expand All @@ -570,7 +570,7 @@ TEST( operation, check_max_step_time )

test_tank.evaluate();
EXPECT_EQ( operation::PAUSE, test_op->get_state() );
subhook_remove( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_default_time();

// После запуска опять в паузу из-за превышения времени второго шага,
// который является вспомогательным (выполняется параллельно).
Expand All @@ -581,14 +581,14 @@ TEST( operation, check_max_step_time )
test_op->on_extra_step( 2 );
test_tank.evaluate();
EXPECT_EQ( operation::RUN, test_op->get_state() );
subhook_install( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_millisec(1001UL);
test_op->check_max_step_time( err_str, ERR_STR_SIZE );
const auto RES_STR_EX = "превышено макс. t (1 с) шага 2 'Eval #1'";
EXPECT_STREQ( RES_STR_EX, err_str );
test_tank.evaluate();
EXPECT_EQ( operation::PAUSE, test_op->get_state() );
subhook_remove( G_GET_DELTA_MILLISEC_HOOK_1001 );
DeltaMilliSecSubHooker::set_default_time();

G_LUA_MANAGER->free_Lua();
}

Expand Down
Loading

0 comments on commit 3c65a5c

Please sign in to comment.