You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

593 lines
15 KiB

#include <iostream>
#include <utility>
#include <pEp/pEpLog.hh>
#include "../src/POTS.hh"
#include "../src/libc99.hh"
//using namespace pEp;
using POTS::operator==;
#define TEST_IS_COPY(a, b) \
do { \
pEpLog("Test is copy"); \
assert(b == a); \
assert(!b.is(a)); \
} while (0);
#define TEST_IS_REF(a, b) \
do { \
pEpLog("Test is ref"); \
assert(b == a); \
assert(b.is(a)); \
} while (0);
//template<typename A, typename B>
//int TEST_IS_COPY(const A& a, const B& b)
//{
// pEpLog("Test is copy");
// assert(b == a);
// assert(!b.is(a));
// return 0;
//}
//
//template<typename A, typename B>
//int TEST_IS_REF(const A& a, const B& b)
//{
// pEpLog("Test is ref");
// assert(b == a);
// assert(b.is(a));
// return 0;
//}
//template<typename T, typename Enabled = void>
//class TestSleeve;
// -------------------------------------------------------------------------------------------------
// FUNDAMENTAL
// -------------------------------------------------------------------------------------------------
template<typename T>
class PODFactory {
public:
using FactoryFunc = const std::function<T*(const T&)>;
public:
PODFactory() = delete;
PODFactory(const T& value) : _factory_func{ new_ }, _value{ value } {}
PODFactory(FactoryFunc& init1, const T& value) : _factory_func{ init1 }, _value{ value } {}
operator T()
{
return new_value();
}
operator T*()
{
return new_obj();
}
T* new_obj()
{
return _factory_func(_value);
}
T& new_value()
{
return *new_obj();
}
private:
static T* new_(const T& val)
{
T* ret = (T*)calloc(1, sizeof(T));
*ret = val;
return ret;
}
FactoryFunc _factory_func;
const T& _value;
};
template<typename T>
class TestSleeve;
// VALUE -------------------------------------------------------------------------------------------------
template<typename T>
class TestSleeve {
public:
TestSleeve(PODFactory<T> init1, PODFactory<T> init2) : factory1{ init1 }, factory2{ init2 }
{
test_sleeve();
}
int test_sleeve()
{
pEpLogH1("Test sleeve<" + std::string(typeid(T).name()) + ">");
test_HS();
test_BS();
return 0;
}
int test_HS()
{
pEpLogH1("Test HS<" + std::string(typeid(T).name()) + ">");
{
pEpLogH2("Test HS ctor");
T a = factory1;
POTS::Sleeve<T> b{ a };
pEpLog(b.to_string());
TEST_IS_COPY(a, b);
}
{
pEpLogH2("Test assign HS");
T a1 = factory1;
POTS::Sleeve<T> b1{ a1 };
pEpLog(b1.to_string());
T a2 = factory2;
POTS::Sleeve<T> b2{ a2 };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b1, b2);
}
{
pEpLogH2("Test assign BS");
T a1 = factory1;
POTS::Sleeve<T> b1{ a1 };
pEpLog(b1.to_string());
T a2 = factory2;
;
POTS::Sleeve<T> b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b1, b2);
}
{
pEpLogH2("Test copy ctor");
T a = factory1;
POTS::Sleeve<T> b1{ a };
pEpLog(b1.to_string());
POTS::Sleeve<T> b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_COPY(b1, b2);
}
return 0;
}
int test_BS()
{
pEpLogH1("Test BS<" + std::string(typeid(T).name()) + ">");
{
pEpLogH2("Test BS ctor");
T a1 = factory1;
POTS::Sleeve<T> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
TEST_IS_REF(a1, b1);
}
{
pEpLogH2("Test assign HS");
T a1 = factory1;
POTS::Sleeve<T> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
T a2 = factory2;
;
POTS::Sleeve<T> b2{ a2 };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b1, b2);
}
{
pEpLogH2("Test assign BS");
T a1 = factory1;
POTS::Sleeve<T> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
T a2 = factory2;
;
POTS::Sleeve<T> b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b1, b2);
}
{
pEpLogH2("Test copy ctor");
T a1 = factory1;
pEpLog(a1);
POTS::Sleeve<T> b1{ [&a1]() { return &a1; } };
pEpLog(b1);
pEpLog(b1.to_string());
POTS::Sleeve<T> b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_COPY(b1, b2);
}
return 0;
}
private:
PODFactory<T> factory1;
PODFactory<T> factory2;
};
// POINTER -------------------------------------------------------------------------------------------------
template<typename T>
class TestSleeve<T*> {
public:
TestSleeve(PODFactory<T> init1, PODFactory<T> init2) : factory1{ init1 }, factory2{ init2 }
{
test_sleeve();
}
int test_sleeve()
{
pEpLogH1("Test sleeve<" + std::string(typeid(T*).name()) + ">");
test_HS();
test_BS();
return 0;
}
int test_HS()
{
pEpLogH1("Test HS<" + std::string(typeid(T*).name()) + ">");
{
pEpLogH2("Test HS ctor");
POTS::Sleeve<T*> b1{};
pEpLog(b1.to_string());
}
{
pEpLogH2("Test assign RAW-T (value)");
POTS::Sleeve<T*> b1{};
pEpLog(b1.to_string());
T b2 = factory2;
pEpLog(b2);
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b2, b1);
}
{
pEpLogH2("Test assign HS");
POTS::Sleeve<T*> b1{};
pEpLog(b1.to_string());
POTS::Sleeve<T*> b2{};
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test assign BS");
POTS::Sleeve<T*> b1{};
pEpLog(b1.to_string());
T* a2 = factory1;
POTS::Sleeve<T*> b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test copy ctor");
POTS::Sleeve<T*> b1{};
pEpLog(b1.to_string());
POTS::Sleeve<T*> b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_REF(b1, b2);
}
return 0;
}
int test_BS()
{
pEpLogH1("Test BS");
{
pEpLogH2("Test BS ctor");
T* a1 = factory1;
POTS::Sleeve<T*> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
TEST_IS_REF(a1, b1);
}
{
pEpLogH2("Test assign RAW-T (value)");
T* a1 = factory1;
POTS::Sleeve<T*> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
T b2 = factory2;
pEpLog(b2);
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_COPY(b2, b1);
}
{
pEpLogH2("Test assign HS");
T* a1 = factory1;
POTS::Sleeve<T*> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
POTS::Sleeve<T*> b2{};
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test assign BS");
T* a1 = factory1;
POTS::Sleeve<T*> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
T* a2 = factory2;
POTS::Sleeve<T*> b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test copy ctor");
T* a1 = factory1;
POTS::Sleeve<T*> b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
POTS::Sleeve<T*> b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_REF(b1, b2);
}
return 0;
}
private:
PODFactory<T> factory1;
PODFactory<T> factory2;
};
// STRUCT P -------------------------------------------------------------------------------------------------
template<>
//class TestSleeve<T*, typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type> {
class TestSleeve<::C99_C*> {
public:
TestSleeve(PODFactory<::C99_C> init1, PODFactory<::C99_C> init2) :
factory1{ init1 },
factory2{ init2 }
{
test_sleeve();
}
int test_sleeve()
{
pEpLogH1("Test sleeve<" + std::string(typeid(::C99_C*).name()) + ">");
test_HS();
test_BS();
return 0;
}
int test_HS()
{
pEpLogH1("Test HS<" + std::string(typeid(::C99_C*).name()) + ">");
{
pEpLogH2("Test HS ctor");
LIBC99::C99_C b1{};
pEpLog(b1.to_string());
pEpLog(b1.to_string());
}
{
pEpLogH2("Test assign RAW-T (value)");
LIBC99::C99_C b1{};
pEpLog(b1.to_string());
::C99_C b2 = factory2;
pEpLog(b2);
pEpLog(CXX::Inspect::all(b2.pi));
b1 = b2;
pEpLog(b1.to_string());
// TODO: cant test yet
// TEST_IS_COPY(b2, b1);
}
{
pEpLogH2("Test assign HS");
LIBC99::C99_C b1{};
pEpLog(b1.to_string());
LIBC99::C99_C b2{};
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test assign BS");
LIBC99::C99_C b1{};
pEpLog(b1.to_string());
::C99_C* a2 = factory1;
LIBC99::C99_C b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b2.to_string());
TEST_IS_REF(b1, b2);
}
if (1 ) {
pEpLogH2("Test copy ctor");
LIBC99::C99_C b1{};
b1.pi = 23;
pEpLog(b1.to_string());
LIBC99::C99_C b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_REF(b1, b2);
}
return 0;
}
int test_BS()
{
pEpLogH1("Test BS");
{
pEpLogH2("Test BS ctor");
::C99_C* a1 = factory1;
LIBC99::C99_C b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
TEST_IS_REF(a1, b1);
}
{
pEpLogH2("Test assign RAW-T (value)");
::C99_C* a1 = factory1;
LIBC99::C99_C b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
::C99_C b2 = factory2;
pEpLog(b2);
pEpLog(CXX::Inspect::all(b2.pi));
b1 = b2;
pEpLog(b1.to_string());
// TODO: cant test yet
// TEST_IS_COPY(b2, b1);
}
{
pEpLogH2("Test assign HS");
::C99_C* a1 = factory1;
LIBC99::C99_C b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
LIBC99::C99_C b2{};
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
{
pEpLogH2("Test assign BS");
::C99_C* a1 = factory1;
LIBC99::C99_C b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
::C99_C* a2 = factory2;
LIBC99::C99_C b2{ [&a2]() { return &a2; } };
pEpLog(b2.to_string());
b1 = b2;
pEpLog(b1.to_string());
TEST_IS_REF(b1, b2);
}
if (0) {
pEpLogH2("Test copy ctor");
::C99_C* a1 = factory1;
LIBC99::C99_C b1{ [&a1]() { return &a1; } };
pEpLog(b1.to_string());
LIBC99::C99_C b2{ b1 };
pEpLog(b2.to_string());
TEST_IS_REF(b1, b2);
}
return 0;
}
private:
PODFactory<::C99_C> factory1;
PODFactory<::C99_C> factory2;
};
template<typename T>
int test_sleeve(PODFactory<T> init1, PODFactory<T> init2)
{
// TestSleeve<T>{ init1, init2 };
TestSleeve<T*>{ init1, init2 };
return 0;
}
int main()
{
// Utils::readKey();
pEp::Adapter::pEpLog::set_enabled(true);
test_sleeve<int>(PODFactory<int>(23), PODFactory<int>(42));
// test_sleeve<char>(PODFactory<char>('a'), PODFactory<char>('b'));
// test_sleeve<float>(PODFactory<float>(2.3f), PODFactory<float>(4.2f));
// test_sleeve<bool>(PODFactory<bool>(true), PODFactory<bool>(false));
// test_sleeve<::C99_Status>(PODFactory<::C99_Status>(OK), PODFactory<::C99_Status>(ERROR));
// pEpLog(CXX::Inspect::all(*a.pi));
::C99_C a{};
a.i = 1;
a.pi = PODFactory<int>(23);
::C99_C b{};
b.i = 2;
b.pi = PODFactory<int>(42);
test_sleeve<::C99_C>(
PODFactory<::C99_C>(
[](const ::C99_C& val) {
::C99_C* a = (::C99_C*)calloc(1, sizeof(::C99_C));
a->i = PODFactory<int>(val.i);
a->pi = PODFactory<int>(*val.pi);
return a;
},
a),
PODFactory<::C99_C>(
[](const ::C99_C& val) {
::C99_C* a = (::C99_C*)calloc(1, sizeof(::C99_C));
a->i = PODFactory<int>(val.i);
a->pi = PODFactory<int>(*val.pi);
return a;
},
b));
/* {
::C99_C c{};
c.i = 1;
c.pi = PODFactory<int>(23);
::C99_B b{};
b.c = PODFactory<::C99_C>(c);
::C99_A a{};
a.b = PODFactory<::C99_B>(b);
}
*/
return 0;
}