add common/adc_valholder.h header:

new conception: 1) no validator
                  2) AdcSerializingValueHolder is now non-templated
                     class
This commit is contained in:
Timur A. Fatkhullin
2024-05-12 01:54:32 +03:00
parent 070922e5d5
commit 6d28bf7747
3 changed files with 650 additions and 7 deletions

View File

@@ -1,12 +1,15 @@
#include <list>
// #include <list>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#include <chrono>
// #include <chrono>
#include <iostream>
#include "../common/adc_utils.h"
// #include "../common/adc_utils.h"
// #include "../common/adc_value_holder.h"
#include "../common/adc_value.h"
// #include "../common/adc_value.h"
#include "../common/adc_valholder.h"
static double dbl_val = 99.99;
@@ -33,6 +36,96 @@ bool vdbl(const double& v)
}
TEST_CASE("[ADC VALUEHOLDER]")
{
int int_val = 10;
auto getter = [&int_val]() { return int_val; };
auto setter = [&int_val](const int& v) { int_val = v; };
adc::AdcValueHolder vh(getter, setter);
auto vah = adc::makeArithmValue<adc::AdcValueHolder>(getter, setter);
vh = 77;
REQUIRE_EQ(int_val, 77);
vh = 100;
int res = vh;
REQUIRE_EQ(res, 100);
CHECK_THROWS_WITH_AS(vh = 1.1, "conversion function is not defined", std::system_error);
std::cout << "int_val = " << int_val << "\n";
const double dv = 33.33;
vah = dv;
REQUIRE_EQ(int_val, 33);
vah = 100;
auto res_long = static_cast<unsigned long>(vah);
REQUIRE_EQ(res_long, 100ul);
vah = -10.1234;
REQUIRE_EQ(int_val, -10);
std::string str = "THIS IS A STRING";
auto sget = [&str]() { return str; };
auto sset = [&str](const std::string& s) { str = s; };
adc::AdcValueHolder vsh(sget, sset);
auto lls = [](const char* v) { return std::string(v); };
vsh.addConvertFunc([&str](const std::string&) { return str.c_str(); }, lls);
vsh = "NEW VALUE";
REQUIRE_EQ(str, "NEW VALUE");
const char* sptr = vsh;
std::cout << "SPTR: " << sptr << "\n";
REQUIRE_EQ(std::strcmp(sptr, "NEW VALUE"), 0);
// vah = vsh;
// vah = 10;
// adc::AdcSerializingValueHolder svh(adc::AdcValueHolder::defaultTrivialConvTypes, getter, setter);
auto svh = adc::makeArithmValue<adc::AdcSerializingValueHolder>(getter, setter);
svh = 77.65412;
std::cout << "SERIALIZED: " << svh.serialize<std::string>() << "\n";
std::string sval = "123";
svh.deserialize(sval);
float fl = svh;
std::cout << "DESERIALIZED: " << fl << "\n";
// adc::AdcSerializingValueHolder svhs(
// sget, sset, [](const std::string& s) { return s; }, [](const std::string& s) { return s; });
// svh = svhs;
// svh = 10;
}
/*
TEST_CASE("[ADC VALUEHOLDER]")
{
int int_val = 10;
@@ -81,7 +174,6 @@ TEST_CASE("[ADC VALUEHOLDER]")
REQUIRE_EQ(int_val, -10);
/* std::string holder */
std::string str = "THIS IS A STRING";
@@ -204,3 +296,5 @@ TEST_CASE("[ADC VALUEHOLDER]")
res = svh;
std::cout << "DESERIALIZED: " << res << "\n";
}
*/