From 63029e608363f066708a60f0eb8a574e7a26e01a Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Mon, 31 Jul 2023 20:16:05 +0300 Subject: [PATCH] Adding Simple Objest test. Fixes some bugs --- Allocators/private/HeapAllocatorGlobal.cpp | 2 ++ Connection/private/LocalConnection.cpp | 13 ++++----- Connection/public/ConnectionCommon.hpp | 2 +- Modules/public/Archiver.hpp | 4 +-- Objects/CMakeLists.txt | 2 +- Objects/private/core/module.cpp | 1 + Objects/public/core/object.h | 8 ++++-- Objects/tests/Tests.cpp | 16 +++++++++++ Objects/tests/core/TestCore.cpp | 31 ++++++++++++++++++++++ Strings/public/Strings.hpp | 10 ++++--- 10 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 Objects/tests/core/TestCore.cpp diff --git a/Allocators/private/HeapAllocatorGlobal.cpp b/Allocators/private/HeapAllocatorGlobal.cpp index 7e41c6a..291ccd6 100644 --- a/Allocators/private/HeapAllocatorGlobal.cpp +++ b/Allocators/private/HeapAllocatorGlobal.cpp @@ -172,6 +172,8 @@ bool HeapAllocGlobal::checkLeaks() { } #endif + printf(" Count : %llu", mNumAllocations); + ASSERT(!"Destruction of not freed Allocator") return true; } diff --git a/Connection/private/LocalConnection.cpp b/Connection/private/LocalConnection.cpp index cd6f90c..cf90e2e 100644 --- a/Connection/private/LocalConnection.cpp +++ b/Connection/private/LocalConnection.cpp @@ -26,11 +26,11 @@ bool LocalConnection::connect(const Location& location, const Type& connectionIn switch (connectionInfo.getType()) { case Type::READ: - handle->open(location.getLocation().read(), false); + handle->open(location.getLocation().read(), true); break; case Type::WRITE: - handle->open(location.getLocation().read(), true); + handle->open(location.getLocation().read(), false); break; default: @@ -61,12 +61,13 @@ bool LocalConnection::disconnect() { bool LocalConnection::setPointer(BytePointer pointer) { DEBUG_ASSERT(mStatus.isOpened()); if (!mStatus.isOpened()) return false; + mPointer = pointer; return true; } bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) { - DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite()); - if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false; + DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead()); + if (!mStatus.isOpened() || !mConnectionType.isRead()) return false; mHandle->seekp(mPointer); mHandle->read(bytes, size); @@ -75,8 +76,8 @@ bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) { } bool LocalConnection::writeBytes(const Byte* bytes, SizeBytes size) { - DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead()); - if (!mStatus.isOpened() || !mConnectionType.isRead()) return false; + DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite()); + if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false; mHandle->seekp(mPointer); mHandle->write(bytes, size); diff --git a/Connection/public/ConnectionCommon.hpp b/Connection/public/ConnectionCommon.hpp index ab70d62..6c3cc1f 100644 --- a/Connection/public/ConnectionCommon.hpp +++ b/Connection/public/ConnectionCommon.hpp @@ -34,8 +34,8 @@ namespace tp { class Type { public: enum State { - READ, WRITE, + READ, READ_WRITE, NONE, }; diff --git a/Modules/public/Archiver.hpp b/Modules/public/Archiver.hpp index ba66bc2..6bc7ee9 100644 --- a/Modules/public/Archiver.hpp +++ b/Modules/public/Archiver.hpp @@ -48,7 +48,7 @@ namespace tp { if constexpr (HasFunc::template Write::value) { val.archiveWrite(*this); } else { - writeBytes((const int1*) &val, sizeof(val)); + writeBytes((const int1*) &val, sizeof(Type)); } } @@ -60,7 +60,7 @@ namespace tp { if constexpr (HasFunc::template Read::value) { val.archiveRead(*this); } else { - readBytes((int1*) &val, sizeof(val)); + readBytes((int1*) &val, sizeof(Type)); } } diff --git a/Objects/CMakeLists.txt b/Objects/CMakeLists.txt index 602e29a..91166d2 100644 --- a/Objects/CMakeLists.txt +++ b/Objects/CMakeLists.txt @@ -23,7 +23,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Strings Math Tokenizer CommandLine ### -------------------------- Tests -------------------------- ### enable_testing() -file(GLOB TEST_SOURCES "./tests/*.cpp") +file(GLOB TEST_SOURCES "./tests/*.cpp" "./tests/*/*.cpp") add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES}) target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils) add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests) diff --git a/Objects/private/core/module.cpp b/Objects/private/core/module.cpp index 22286c2..3140810 100644 --- a/Objects/private/core/module.cpp +++ b/Objects/private/core/module.cpp @@ -81,6 +81,7 @@ static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleMath, &tp::gModuleStrings, &tp::gModuleTokenizer, + &tp::gModuleConnection, NULL }; diff --git a/Objects/public/core/object.h b/Objects/public/core/object.h index 8782bdf..29ae4fa 100644 --- a/Objects/public/core/object.h +++ b/Objects/public/core/object.h @@ -46,14 +46,18 @@ namespace obj { public: Archiver() = default; - explicit Archiver(const char* location) {}; + explicit Archiver(const char* location) { + mConnection.connect(tp::LocalConnection::Location(location), tp::LocalConnection::Type(tRead)); + }; void writeBytes(const tp::int1* val, tp::ualni size) override { + mConnection.setPointer(mAddress); mConnection.writeBytes(val, size); incrementAddresses(size); } void readBytes(tp::int1* val, tp::ualni size) override { + mConnection.setPointer(mAddress); mConnection.readBytes(val, size); incrementAddresses(size); } @@ -68,7 +72,7 @@ namespace obj { bool isOpened() { return mConnection.getConnectionStatus().isOpened(); } - bool getSize() { return mConnection.size(); } + tp::ualni getSize() { return mConnection.size(); } private: void incrementAddresses(tp::ualni size) { diff --git a/Objects/tests/Tests.cpp b/Objects/tests/Tests.cpp index 52e3859..0a9b12d 100644 --- a/Objects/tests/Tests.cpp +++ b/Objects/tests/Tests.cpp @@ -4,6 +4,22 @@ #include "primitives/primitives.h" +using namespace tp; +using namespace obj; + + +void testCore(); + int main() { + + tp::ModuleManifest* deps[] = { &gModuleObjects, nullptr }; + tp::ModuleManifest module("ObjectsTests", nullptr, nullptr, deps); + + if (module.initialize()) { + + testCore(); + + module.deinitialize(); + } return 0; } \ No newline at end of file diff --git a/Objects/tests/core/TestCore.cpp b/Objects/tests/core/TestCore.cpp new file mode 100644 index 0000000..a482294 --- /dev/null +++ b/Objects/tests/core/TestCore.cpp @@ -0,0 +1,31 @@ + +#include "NewPlacement.hpp" + +#include "Testing.hpp" + +#include "core/object.h" + +#include "primitives/intobject.h" + +using namespace tp; +using namespace obj; + +TEST_DEF_STATIC(BasicAPI) { + auto integer = NDO_CAST(IntObject, NDO->create("int")); + + integer->val = 10; + + printf("%s\n", NDO->toString(integer).read()); + + NDO->save(integer, "tmp.o"); + auto savedInt = NDO->load("tmp.o"); + + printf("%s\n", NDO->toString(savedInt).read()); + + NDO->destroy(integer); + NDO->destroy(savedInt); +} + +TEST_DEF(Core) { + testBasicAPI(); +} \ No newline at end of file diff --git a/Strings/public/Strings.hpp b/Strings/public/Strings.hpp index b999f8d..9f61cfa 100644 --- a/Strings/public/Strings.hpp +++ b/Strings/public/Strings.hpp @@ -121,6 +121,7 @@ namespace tp { } void decReference(Data* dp) { + DEBUG_ASSERT(dp->mReferenceCount > 0) dp->mReferenceCount--; if (!dp->mReferenceCount) { mData->~StringData(); @@ -184,21 +185,24 @@ namespace tp { public: // Syntax sugars explicit StringTemplate(alni val) { - auto raw = new tChar[MAX_INT_STRING_LENGTH]; + tChar raw[MAX_INT_STRING_LENGTH]; Logic::convertValueToString(val, raw, MAX_INT_STRING_LENGTH); mData = new (sStringPool.allocate(0)) StringData(raw); + incReference(mData); } explicit StringTemplate(alnf val) { - auto raw = new tChar[MAX_FLOAT_STRING_LENGTH]; + tChar raw[MAX_INT_STRING_LENGTH]; Logic::convertValueToString(val, raw, MAX_FLOAT_STRING_LENGTH); mData = new (sStringPool.allocate(0)) StringData(raw); + incReference(mData); } explicit StringTemplate(bool val) { - auto raw = new tChar[MAX_BOOL_STRING_LENGTH]; + tChar raw[MAX_INT_STRING_LENGTH]; Logic::convertValueToString(val, raw, MAX_BOOL_STRING_LENGTH); mData = new (sStringPool.allocate(0)) StringData(raw); + incReference(mData); } explicit operator alni() {