Adding Simple Objest test. Fixes some bugs

This commit is contained in:
IlushaShurupov 2023-07-31 20:16:05 +03:00 committed by Ilya Shurupov
parent edf0548046
commit 63029e6083
10 changed files with 74 additions and 15 deletions

View file

@ -172,6 +172,8 @@ bool HeapAllocGlobal::checkLeaks() {
} }
#endif #endif
printf(" Count : %llu", mNumAllocations);
ASSERT(!"Destruction of not freed Allocator") ASSERT(!"Destruction of not freed Allocator")
return true; return true;
} }

View file

@ -26,11 +26,11 @@ bool LocalConnection::connect(const Location& location, const Type& connectionIn
switch (connectionInfo.getType()) { switch (connectionInfo.getType()) {
case Type::READ: case Type::READ:
handle->open(location.getLocation().read(), false); handle->open(location.getLocation().read(), true);
break; break;
case Type::WRITE: case Type::WRITE:
handle->open(location.getLocation().read(), true); handle->open(location.getLocation().read(), false);
break; break;
default: default:
@ -61,12 +61,13 @@ bool LocalConnection::disconnect() {
bool LocalConnection::setPointer(BytePointer pointer) { bool LocalConnection::setPointer(BytePointer pointer) {
DEBUG_ASSERT(mStatus.isOpened()); DEBUG_ASSERT(mStatus.isOpened());
if (!mStatus.isOpened()) return false; if (!mStatus.isOpened()) return false;
mPointer = pointer;
return true; return true;
} }
bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) { bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) {
DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite()); DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead());
if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false; if (!mStatus.isOpened() || !mConnectionType.isRead()) return false;
mHandle->seekp(mPointer); mHandle->seekp(mPointer);
mHandle->read(bytes, size); mHandle->read(bytes, size);
@ -75,8 +76,8 @@ bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) {
} }
bool LocalConnection::writeBytes(const Byte* bytes, SizeBytes size) { bool LocalConnection::writeBytes(const Byte* bytes, SizeBytes size) {
DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead()); DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite());
if (!mStatus.isOpened() || !mConnectionType.isRead()) return false; if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false;
mHandle->seekp(mPointer); mHandle->seekp(mPointer);
mHandle->write(bytes, size); mHandle->write(bytes, size);

View file

@ -34,8 +34,8 @@ namespace tp {
class Type { class Type {
public: public:
enum State { enum State {
READ,
WRITE, WRITE,
READ,
READ_WRITE, READ_WRITE,
NONE, NONE,
}; };

View file

@ -48,7 +48,7 @@ namespace tp {
if constexpr (HasFunc::template Write<Type>::value) { if constexpr (HasFunc::template Write<Type>::value) {
val.archiveWrite(*this); val.archiveWrite(*this);
} else { } else {
writeBytes((const int1*) &val, sizeof(val)); writeBytes((const int1*) &val, sizeof(Type));
} }
} }
@ -60,7 +60,7 @@ namespace tp {
if constexpr (HasFunc::template Read<Type>::value) { if constexpr (HasFunc::template Read<Type>::value) {
val.archiveRead(*this); val.archiveRead(*this);
} else { } else {
readBytes((int1*) &val, sizeof(val)); readBytes((int1*) &val, sizeof(Type));
} }
} }

View file

@ -23,7 +23,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Strings Math Tokenizer CommandLine
### -------------------------- Tests -------------------------- ### ### -------------------------- Tests -------------------------- ###
enable_testing() enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp") file(GLOB TEST_SOURCES "./tests/*.cpp" "./tests/*/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES}) add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils) target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests) add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)

View file

@ -81,6 +81,7 @@ static tp::ModuleManifest* sModuleDependencies[] = {
&tp::gModuleMath, &tp::gModuleMath,
&tp::gModuleStrings, &tp::gModuleStrings,
&tp::gModuleTokenizer, &tp::gModuleTokenizer,
&tp::gModuleConnection,
NULL NULL
}; };

View file

@ -46,14 +46,18 @@ namespace obj {
public: public:
Archiver() = default; 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 { void writeBytes(const tp::int1* val, tp::ualni size) override {
mConnection.setPointer(mAddress);
mConnection.writeBytes(val, size); mConnection.writeBytes(val, size);
incrementAddresses(size); incrementAddresses(size);
} }
void readBytes(tp::int1* val, tp::ualni size) override { void readBytes(tp::int1* val, tp::ualni size) override {
mConnection.setPointer(mAddress);
mConnection.readBytes(val, size); mConnection.readBytes(val, size);
incrementAddresses(size); incrementAddresses(size);
} }
@ -68,7 +72,7 @@ namespace obj {
bool isOpened() { return mConnection.getConnectionStatus().isOpened(); } bool isOpened() { return mConnection.getConnectionStatus().isOpened(); }
bool getSize() { return mConnection.size(); } tp::ualni getSize() { return mConnection.size(); }
private: private:
void incrementAddresses(tp::ualni size) { void incrementAddresses(tp::ualni size) {

View file

@ -4,6 +4,22 @@
#include "primitives/primitives.h" #include "primitives/primitives.h"
using namespace tp;
using namespace obj;
void testCore();
int main() { int main() {
tp::ModuleManifest* deps[] = { &gModuleObjects, nullptr };
tp::ModuleManifest module("ObjectsTests", nullptr, nullptr, deps);
if (module.initialize()) {
testCore();
module.deinitialize();
}
return 0; return 0;
} }

View file

@ -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();
}

View file

@ -121,6 +121,7 @@ namespace tp {
} }
void decReference(Data* dp) { void decReference(Data* dp) {
DEBUG_ASSERT(dp->mReferenceCount > 0)
dp->mReferenceCount--; dp->mReferenceCount--;
if (!dp->mReferenceCount) { if (!dp->mReferenceCount) {
mData->~StringData(); mData->~StringData();
@ -184,21 +185,24 @@ namespace tp {
public: // Syntax sugars public: // Syntax sugars
explicit StringTemplate(alni val) { 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); Logic::convertValueToString(val, raw, MAX_INT_STRING_LENGTH);
mData = new (sStringPool.allocate(0)) StringData(raw); mData = new (sStringPool.allocate(0)) StringData(raw);
incReference(mData);
} }
explicit StringTemplate(alnf val) { 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); Logic::convertValueToString(val, raw, MAX_FLOAT_STRING_LENGTH);
mData = new (sStringPool.allocate(0)) StringData(raw); mData = new (sStringPool.allocate(0)) StringData(raw);
incReference(mData);
} }
explicit StringTemplate(bool val) { 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); Logic::convertValueToString(val, raw, MAX_BOOL_STRING_LENGTH);
mData = new (sStringPool.allocate(0)) StringData(raw); mData = new (sStringPool.allocate(0)) StringData(raw);
incReference(mData);
} }
explicit operator alni() { explicit operator alni() {