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
printf(" Count : %llu", mNumAllocations);
ASSERT(!"Destruction of not freed Allocator")
return true;
}

View file

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

View file

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

View file

@ -48,7 +48,7 @@ namespace tp {
if constexpr (HasFunc::template Write<Type>::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<Type>::value) {
val.archiveRead(*this);
} 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 -------------------------- ###
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)

View file

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

View file

@ -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) {

View file

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

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) {
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() {