#include #include #include #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 //int TEST_IS_COPY(const A& a, const B& b) //{ // pEpLog("Test is copy"); // assert(b == a); // assert(!b.is(a)); // return 0; //} // //template //int TEST_IS_REF(const A& a, const B& b) //{ // pEpLog("Test is ref"); // assert(b == a); // assert(b.is(a)); // return 0; //} //template //class TestSleeve; // ------------------------------------------------------------------------------------------------- // FUNDAMENTAL // ------------------------------------------------------------------------------------------------- template class PODFactory { public: using FactoryFunc = const std::function; 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 class TestSleeve; // VALUE ------------------------------------------------------------------------------------------------- template class TestSleeve { public: TestSleeve(PODFactory init1, PODFactory 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 b{ a }; pEpLog(b.to_string()); TEST_IS_COPY(a, b); } { pEpLogH2("Test assign HS"); T a1 = factory1; POTS::Sleeve b1{ a1 }; pEpLog(b1.to_string()); T a2 = factory2; POTS::Sleeve 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 b1{ a1 }; pEpLog(b1.to_string()); T a2 = factory2; ; POTS::Sleeve 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 b1{ a }; pEpLog(b1.to_string()); POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); TEST_IS_REF(a1, b1); } { pEpLogH2("Test assign HS"); T a1 = factory1; POTS::Sleeve b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); T a2 = factory2; ; POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); T a2 = factory2; ; POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1); pEpLog(b1.to_string()); POTS::Sleeve b2{ b1 }; pEpLog(b2.to_string()); TEST_IS_COPY(b1, b2); } return 0; } private: PODFactory factory1; PODFactory factory2; }; // POINTER ------------------------------------------------------------------------------------------------- template class TestSleeve { public: TestSleeve(PODFactory init1, PODFactory 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 b1{}; pEpLog(b1.to_string()); } { pEpLogH2("Test assign RAW-T (value)"); POTS::Sleeve 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 b1{}; pEpLog(b1.to_string()); POTS::Sleeve b2{}; pEpLog(b2.to_string()); b1 = b2; pEpLog(b1.to_string()); TEST_IS_REF(b1, b2); } { pEpLogH2("Test assign BS"); POTS::Sleeve b1{}; pEpLog(b1.to_string()); T* a2 = factory1; POTS::Sleeve 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 b1{}; pEpLog(b1.to_string()); POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); TEST_IS_REF(a1, b1); } { pEpLogH2("Test assign RAW-T (value)"); T* a1 = factory1; POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); T* a2 = factory2; POTS::Sleeve 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 b1{ [&a1]() { return &a1; } }; pEpLog(b1.to_string()); POTS::Sleeve b2{ b1 }; pEpLog(b2.to_string()); TEST_IS_REF(b1, b2); } return 0; } private: PODFactory factory1; PODFactory factory2; }; // STRUCT P ------------------------------------------------------------------------------------------------- template<> //class TestSleeve::value || std::is_enum::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 int test_sleeve(PODFactory init1, PODFactory init2) { // TestSleeve{ init1, init2 }; TestSleeve{ init1, init2 }; return 0; } int main() { // Utils::readKey(); pEp::Adapter::pEpLog::set_enabled(true); test_sleeve(PODFactory(23), PODFactory(42)); // test_sleeve(PODFactory('a'), PODFactory('b')); // test_sleeve(PODFactory(2.3f), PODFactory(4.2f)); // test_sleeve(PODFactory(true), PODFactory(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(23); ::C99_C b{}; b.i = 2; b.pi = PODFactory(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(val.i); a->pi = PODFactory(*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(val.i); a->pi = PODFactory(*val.pi); return a; }, b)); /* { ::C99_C c{}; c.i = 1; c.pi = PODFactory(23); ::C99_B b{}; b.c = PODFactory<::C99_C>(c); ::C99_A a{}; a.b = PODFactory<::C99_B>(b); } */ return 0; }