This commit is contained in:
Timur A. Fatkhullin 2024-05-28 11:59:56 +03:00
parent d7ed4fac6c
commit 3821d965cc
3 changed files with 54 additions and 3 deletions

View File

@ -36,6 +36,9 @@ if (BUILD_TESTS)
set(DEVATTR_TEST_APP adc_devattr_test) set(DEVATTR_TEST_APP adc_devattr_test)
add_executable(${DEVATTR_TEST_APP} tests/adc_devattr_test.cpp) add_executable(${DEVATTR_TEST_APP} tests/adc_devattr_test.cpp)
set(NETMSG_TEST_APP adc_netmsg_test)
add_executable(${NETMSG_TEST_APP} tests/adc_netmsg_test.cpp)
if (NOT doctest_FOUND) if (NOT doctest_FOUND)
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
@ -51,6 +54,7 @@ if (BUILD_TESTS)
include(CTest) include(CTest)
# add_test(VALUE_HOLDER ${VALUEHOLDER_TEST_APP}) # add_test(VALUE_HOLDER ${VALUEHOLDER_TEST_APP})
add_test(VALUE_HOLDER ${DEVATTR_TEST_APP}) add_test(VALUE_HOLDER ${DEVATTR_TEST_APP})
add_test(NETMSG_TEST ${NETMSG_TEST_APP})
enable_testing() enable_testing()
endif(BUILD_TESTS) endif(BUILD_TESTS)

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <ranges> #include <ranges>
#include <vector>
#include "../common/adc_traits.h" #include "../common/adc_traits.h"
#include "../common/adc_utils.h"
/* /*
@ -152,7 +152,7 @@ protected:
output_seq_t _outputSequence; output_seq_t _outputSequence;
AdcNetMessageInterface() : _storageSequence(), _outputSequence() {}; AdcNetMessageInterface() : _storageSequence(), _outputSequence(){};
template <typename T, typename... Ts> template <typename T, typename... Ts>
AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface() AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface()
@ -292,7 +292,8 @@ public:
auto max_el = _storageSequence.size() - start; auto max_el = _storageSequence.size() - start;
N >= max_el ? max_el * 2 - 1 : N * 2 - 1; N >= max_el ? max_el * 2 - 1 : N * 2 - 1;
std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N), std::back_inserter(r)); std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N) | std::views::join,
std::back_inserter(r));
} }
const output_seq_t& storageSeq() override const output_seq_t& storageSeq() override
@ -374,6 +375,8 @@ public:
this->_storageSequence.clear(); this->_storageSequence.clear();
this->_outputSequence.clear(); this->_outputSequence.clear();
// TODO: what if key is an empty range?!!!
if constexpr (traits::adc_input_char_range<KeyT> || traits::formattable<KeyT>) { if constexpr (traits::adc_input_char_range<KeyT> || traits::formattable<KeyT>) {
std::ranges::copy(key, std::back_inserter(_key)); std::ranges::copy(key, std::back_inserter(_key));
} else if constexpr (traits::formattable<KeyT>) { } else if constexpr (traits::formattable<KeyT>) {
@ -394,6 +397,7 @@ public:
void key(R& r) const void key(R& r) const
{ {
if constexpr (std::is_convertible_v<std::string, R>) { if constexpr (std::is_convertible_v<std::string, R>) {
r = _key;
return; return;
} }
@ -413,6 +417,23 @@ public:
this->tokensBytes(r, start, N); this->tokensBytes(r, start, N);
} }
template <std::input_iterator IterT>
void setFromBytes(IterT begin, IterT end)
{
auto sp = std::span(begin, end);
auto found = std::ranges::search(sp, keyparamDelimiter);
if (found.empty()) { // no parameters
setKeyParam(sp);
} else {
auto N = std::ranges::distance(sp.begin(), found.begin());
setKeyParam(sp.first(N), sp.last(sp.size() - N - keyparamDelimiter.size()));
}
}
protected: protected:
std::string _key; std::string _key;
// _outputSequence will store parameters // _outputSequence will store parameters

26
tests/adc_netmsg_test.cpp Normal file
View File

@ -0,0 +1,26 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <iostream>
#include "../net/adc_netmessage.h"
using namespace adc;
TEST_CASE("[ADC NET MESSAGE]")
{
AdcKeyParamNetMessage<> msg;
std::string_view bytes{"SET POS 1 2 3 4 5"};
std::cout << "BYTES: [" << bytes << "]\n";
msg.setFromBytes(bytes.begin(), bytes.end());
std::string key, pars;
msg.key(key);
std::cout << "KEY: [" << key << "]\n";
msg.paramsBytes(pars, 1);
// msg.paramsBytes(pars);
std::cout << "PARS BYTES: [" << pars << "]\n";
}