From 2cfde5a685b4d86555e407ada6b3efd9c7c72904 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Sat, 14 Dec 2024 01:35:58 +0300 Subject: [PATCH] add tests/adc_dev_test.cpp --- CMakeLists.txt | 3 + net/adc_device_netserver.h | 4 +- tests/adc_dev_test.cpp | 131 +++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/adc_dev_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3afeec9..e64a794 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,6 +167,9 @@ if (BUILD_TESTS) set(DEVATTR_TEST_APP adc_devattr_test) add_executable(${DEVATTR_TEST_APP} tests/adc_devattr_test.cpp) + set(DEV_TEST_APP adc_dev_test) + add_executable(${DEV_TEST_APP} tests/adc_dev_test.cpp) + set(NETMSG_TEST_APP adc_netmsg_test) add_executable(${NETMSG_TEST_APP} tests/adc_netmsg_test.cpp) diff --git a/net/adc_device_netserver.h b/net/adc_device_netserver.h index 4b0cb1c..ebd6550 100644 --- a/net/adc_device_netserver.h +++ b/net/adc_device_netserver.h @@ -155,8 +155,8 @@ protected: CmdIdDeserialT&& cmd_id_deser_func = {}) : _id(id) { - _get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward(attr_id_deser_func)), - this](const auto& attr_name) mutable { + _get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward(attr_id_deser_func))]( + const auto& attr_name) mutable { auto attr_id = std::get<0>(wrapper)(attr_name); auto& attr = (*dev_ptr)[attr_id]; // auto& attr = dev_ptr->operator[](attr_id); diff --git a/tests/adc_dev_test.cpp b/tests/adc_dev_test.cpp new file mode 100644 index 0000000..65fca3c --- /dev/null +++ b/tests/adc_dev_test.cpp @@ -0,0 +1,131 @@ +#include + +#include "../common/adc_traits.h" +#include "../device/adc_device.h" +#include "../device/adc_device_attribute.h" +#include "../device/adc_device_command.h" + +using namespace adc; + +typedef std::vector serialized_t; + +class DeviceWrapper +{ +public: + // using char_range_t = std::span; + using char_range_t = std::span; + +private: + serialized_t _id; + + std::function _get_attr = [](auto) -> serialized_t { + throw std::invalid_argument("BAD _get_attr"); + }; + std::function _set_attr = [](auto, auto) { + throw std::invalid_argument("BAD _set_attr"); + }; + std::function _exec_cmd = [](auto) { throw std::invalid_argument("BAD _exec_cmd"); }; + +public: + DeviceWrapper() : _id() + { + // null device + std::string_view id{"NULL-DEVICE"}; + std::ranges::copy(id, std::back_inserter(_id)); + }; + + template , + typename CmdIdDeserialT = traits::adc_char_identity> + DeviceWrapper(DeviceT* dev_ptr, + const serialized_t& id, + AttrIdDeserialT&& attr_id_deser_func = {}, + CmdIdDeserialT&& cmd_id_deser_func = {}) + : _id(id) + { + _get_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward(attr_id_deser_func))]( + const auto& attr_name) mutable { + auto attr_id = std::get<0>(wrapper)(attr_name); + auto& attr = (*dev_ptr)[attr_id]; + // auto& attr = dev_ptr->operator[](attr_id); + auto val = attr.serialize(); + return val; + }; + + _set_attr = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward(attr_id_deser_func))]( + const auto& attr_name, const auto& val) mutable { + auto attr_id = std::get<0>(wrapper)(attr_name); + (*dev_ptr)[attr_id].deserialize(val); + }; + + _exec_cmd = [dev_ptr, wrapper = traits::adc_pf_wrapper(std::forward(cmd_id_deser_func))]( + const auto& cmd_name) mutable { + auto cmd_id = std::get<0>(wrapper)(cmd_name); + (*dev_ptr)(cmd_id); + }; + } + + + serialized_t ident() const + { + // return _get_id(); + return _id; + } + + serialized_t getAttr(const serialized_t& attr_name) + { + return _get_attr(attr_name); + } + + void setAttr(const serialized_t& attr_name, const serialized_t& val) + { + _set_attr(attr_name, val); + } + + void exec(const serialized_t& cmd_name) + { + _exec_cmd(cmd_name); + } +}; + + +static std::vector devs; + + +auto getAttr(size_t dev_idx, const serialized_t& name) +{ + DeviceWrapper& dev = devs[dev_idx]; + + return dev.getAttr(name); +} + +int main() +{ + typedef AdcGenericDevice, AdcDeviceCommand<>> + dev_t; + + dev_t dev1("DEV1"); + + int attr1_val = 10; + // dev1.addAttribute(dev_t::attribute_t::makeArithAttr( + // "ATTR1", [&attr1_val]() { return attr1_val; }, [&attr1_val](const int& v) { attr1_val = v; })); + dev1.addAttribute(dev_t::attribute_t::makeArithAttr( + "ATTR1", [&attr1_val]() { return attr1_val; }, [&attr1_val](const int& v) { attr1_val = v; }, + utils::AdcDefaultValueConverter<>::serialize)); + + devs.push_back({&dev1, {'D', '1'}}); + + serialized_t sn; + std::ranges::copy(std::string_view("ATTR1"), std::back_inserter(sn)); + + devs[0].setAttr(sn, {'7', '7'}); + auto r = getAttr(0, sn); + + std::string rs; + std::ranges::copy(r, std::back_inserter(rs)); + + std::cout << "ATTR1 = " << rs << "\n"; + + + return 0; +}