mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 07:06:32 +08:00
cid ut
This commit is contained in:
@@ -62,7 +62,7 @@
|
||||
#define NEU_LOG_LEVEL_ERROR "error"
|
||||
#define NEU_LOG_LEVEL_FATAL "fatal"
|
||||
|
||||
#define NEU_CID_LNCLASS_LEN 8
|
||||
#define NEU_CID_LNCLASS_LEN 12
|
||||
#define NEU_CID_LNTYPE_LEN 32
|
||||
#define NEU_CID_IED_NAME_LEN 32
|
||||
#define NEU_CID_LDEVICE_LEN 32
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
#ifndef _NEU_UTILS_CID_H_
|
||||
#define _NEU_UTILS_CID_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "define.h"
|
||||
#include "msg.h"
|
||||
|
||||
@@ -118,8 +122,8 @@ typedef struct {
|
||||
} cid_template_t;
|
||||
|
||||
typedef struct {
|
||||
cid_ied_t ied;
|
||||
cid_template_t template;
|
||||
cid_ied_t ied;
|
||||
cid_template_t cid_template;
|
||||
} cid_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -141,4 +145,8 @@ void neu_cid_to_msg(char *driver, cid_t *cid, neu_req_add_gtag_t *cmd);
|
||||
char * neu_cid_info_to_string(cid_dataset_info_t *info);
|
||||
cid_dataset_info_t *neu_cid_info_from_string(const char *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+12
-12
@@ -59,7 +59,7 @@ int neu_cid_parse(const char *path, cid_t *cid)
|
||||
while (find != NULL) {
|
||||
if (find->type == XML_ELEMENT_NODE &&
|
||||
strcmp((const char *) find->name, "DataTypeTemplates") == 0) {
|
||||
int ret = parse_template(find, &cid->template);
|
||||
int ret = parse_template(find, &cid->cid_template);
|
||||
if (ret == 0) {
|
||||
templates_parsed = true;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ int neu_cid_parse(const char *path, cid_t *cid)
|
||||
while (find_ied != NULL) {
|
||||
if (find_ied->type == XML_ELEMENT_NODE &&
|
||||
strcmp((const char *) find_ied->name, "IED") == 0) {
|
||||
int ret = parse_ied(find_ied, &cid->ied, &cid->template);
|
||||
int ret = parse_ied(find_ied, &cid->ied, &cid->cid_template);
|
||||
if (ret == 0) {
|
||||
ied_parsed = true;
|
||||
}
|
||||
@@ -94,22 +94,22 @@ int neu_cid_parse(const char *path, cid_t *cid)
|
||||
|
||||
void neu_cid_free(cid_t *cid)
|
||||
{
|
||||
for (int i = 0; i < cid->template.n_lnos; i++) {
|
||||
if (cid->template.lnos[i].n_dos > 0) {
|
||||
free(cid->template.lnos[i].dos);
|
||||
for (int i = 0; i < cid->cid_template.n_lnos; i++) {
|
||||
if (cid->cid_template.lnos[i].n_dos > 0) {
|
||||
free(cid->cid_template.lnos[i].dos);
|
||||
}
|
||||
}
|
||||
if (cid->template.n_lnos > 0) {
|
||||
free(cid->template.lnos);
|
||||
if (cid->cid_template.n_lnos > 0) {
|
||||
free(cid->cid_template.lnos);
|
||||
}
|
||||
|
||||
for (int i = 0; i < cid->template.n_dos; i++) {
|
||||
if (cid->template.dos[i].n_das > 0) {
|
||||
free(cid->template.dos[i].das);
|
||||
for (int i = 0; i < cid->cid_template.n_dos; i++) {
|
||||
if (cid->cid_template.dos[i].n_das > 0) {
|
||||
free(cid->cid_template.dos[i].das);
|
||||
}
|
||||
}
|
||||
if (cid->template.n_dos > 0) {
|
||||
free(cid->template.dos);
|
||||
if (cid->cid_template.n_dos > 0) {
|
||||
free(cid->cid_template.dos);
|
||||
}
|
||||
|
||||
for (int i = 0; i < cid->ied.n_ldevices; i++) {
|
||||
|
||||
@@ -79,6 +79,14 @@ target_include_directories(common_test PRIVATE
|
||||
)
|
||||
target_link_libraries(common_test neuron-base gtest_main gtest)
|
||||
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/tests/ut/serverBMS_3_test.cid DESTINATION ${UT_DIRECTORY}/config)
|
||||
add_executable(cid_test cid_test.cc)
|
||||
target_include_directories(cid_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
target_link_libraries(cid_test neuron-base gtest_main gtest)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(json_test)
|
||||
gtest_discover_tests(http_test)
|
||||
@@ -90,3 +98,4 @@ gtest_discover_tests(async_queue_test)
|
||||
gtest_discover_tests(rolling_counter_test)
|
||||
gtest_discover_tests(mqtt_client_test)
|
||||
gtest_discover_tests(common_test)
|
||||
gtest_discover_tests(cid_test)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "neuron.h"
|
||||
#include "utils/cid.h"
|
||||
|
||||
zlog_category_t *neuron = NULL;
|
||||
TEST(cid_parse, cid)
|
||||
{
|
||||
cid_t cid = { 0 };
|
||||
int ret = neu_cid_parse("./config/serverBMS_3_test.cid", &cid);
|
||||
EXPECT_EQ(ret, 0);
|
||||
neu_cid_free(&cid);
|
||||
}
|
||||
|
||||
TEST(cid_to_msg, cid)
|
||||
{
|
||||
cid_t cid = { 0 };
|
||||
int ret = neu_cid_parse("./config/serverBMS_3_test.cid", &cid);
|
||||
EXPECT_EQ(ret, 0);
|
||||
|
||||
const char *driver = "driver";
|
||||
|
||||
neu_req_add_gtag_t cmd = { 0 };
|
||||
neu_cid_to_msg((char *) driver, &cid, &cmd);
|
||||
neu_cid_free(&cid);
|
||||
|
||||
EXPECT_STREQ(cmd.driver, "driver");
|
||||
EXPECT_EQ(cmd.n_group, 12);
|
||||
|
||||
for (int i = 0; i < cmd.n_group; i++) {
|
||||
for (int j = 0; j < cmd.groups[i].n_tag; j++) {
|
||||
if (cmd.groups[i].tags[j].name != NULL) {
|
||||
free(cmd.groups[i].tags[j].name);
|
||||
}
|
||||
if (cmd.groups[i].tags[j].address != NULL) {
|
||||
free(cmd.groups[i].tags[j].address);
|
||||
}
|
||||
if (cmd.groups[i].tags[j].description != NULL) {
|
||||
free(cmd.groups[i].tags[j].description);
|
||||
}
|
||||
}
|
||||
free(cmd.groups[i].tags);
|
||||
free(cmd.groups[i].context);
|
||||
}
|
||||
free(cmd.groups);
|
||||
}
|
||||
|
||||
TEST(cid_string, cid)
|
||||
{
|
||||
cid_dataset_info_t info = { 0 };
|
||||
info.control = true;
|
||||
strcpy(info.ied_name, "ied_name");
|
||||
strcpy(info.ldevice_inst, "ldevice_inst");
|
||||
strcpy(info.ln_class, "ln_class");
|
||||
info.buffered = true;
|
||||
strcpy(info.report_name, "report_name");
|
||||
strcpy(info.report_id, "report_id");
|
||||
strcpy(info.dataset_name, "dataset_name");
|
||||
|
||||
char *str = neu_cid_info_to_string(&info);
|
||||
EXPECT_STREQ(
|
||||
str,
|
||||
"{\"control\": true, \"buffered\": true, \"ied_name\": \"ied_name\", "
|
||||
"\"ldevice_inst\": \"ldevice_inst\", \"ln_class\": "
|
||||
"\"ln_class\", \"report_name\": \"report_name\", "
|
||||
"\"report_id\": \"report_id\", \"dataset_name\": \"dataset_name\"}");
|
||||
free(str);
|
||||
}
|
||||
|
||||
TEST(cid_from_string, cid)
|
||||
{
|
||||
cid_dataset_info_t *info = NULL;
|
||||
const char * str =
|
||||
"{\"control\": true, \"buffered\": true, \"ied_name\": \"ied_name\", "
|
||||
"\"ldevice_inst\": \"ldevice_inst\", \"ln_class\": "
|
||||
"\"ln_class\", \"report_name\": \"report_name\", "
|
||||
"\"report_id\": \"report_id\", \"dataset_name\": \"dataset_name\"}";
|
||||
|
||||
info = neu_cid_info_from_string(str);
|
||||
EXPECT_EQ(info->control, true);
|
||||
EXPECT_STREQ(info->ied_name, "ied_name");
|
||||
EXPECT_STREQ(info->ldevice_inst, "ldevice_inst");
|
||||
EXPECT_STREQ(info->ln_class, "ln_class");
|
||||
EXPECT_EQ(info->buffered, true);
|
||||
EXPECT_STREQ(info->report_name, "report_name");
|
||||
EXPECT_STREQ(info->report_id, "report_id");
|
||||
EXPECT_STREQ(info->dataset_name, "dataset_name");
|
||||
free(info);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
zlog_init("./config/dev.conf");
|
||||
neuron = zlog_get_category("neuron");
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user