#include #include #include #include #include #include #include "CatchDefinitions.h" #include "mock/MessageQueueMock.h" #include "mock/TestPoolOwner.h" #include "tests/TestsConfig.h" using namespace returnvalue; using namespace lpool; TEST_CASE("DataSetTest", "[datapool]") { auto queue = MessageQueueMock(1, MessageQueueIF::NO_QUEUE); TestPoolOwner poolOwner(queue, objects::TEST_LOCAL_POOL_OWNER_BASE); poolOwner.initialize(); StaticTestDataset localSet; SECTION("BasicTest") { /* Test some basic functions */ CHECK(localSet.getReportingEnabled() == false); CHECK(localSet.getLocalPoolIdsSerializedSize() == 3 * sizeof(dp::id_t)); CHECK(localSet.getStructureId() == lpool::testSid1); CHECK(localSet.getCreatorObjectId() == objects::TEST_LOCAL_POOL_OWNER_BASE); size_t maxSize = localSet.getLocalPoolIdsSerializedSize(); uint8_t localPoolIdBuff[maxSize]; // Skip size field auto* lpIds = reinterpret_cast(localPoolIdBuff); size_t serSize = 0; auto* localPoolIdBuffPtr = reinterpret_cast(localPoolIdBuff); // Test local pool ID serialization CHECK(localSet.serializeLocalPoolIds(&localPoolIdBuffPtr, &serSize, maxSize, SerializeIF::Endianness::MACHINE) == returnvalue::OK); CHECK(serSize == maxSize); // CHECK(localPoolIdBuff[0] == 3); CHECK(lpIds[0] == localSet.localPoolVarUint8.getDataPoolId()); CHECK(lpIds[1] == localSet.localPoolVarFloat.getDataPoolId()); CHECK(lpIds[2] == localSet.localPoolUint16Vec.getDataPoolId()); // Now serialize without fill count lpIds = reinterpret_cast(localPoolIdBuff); localPoolIdBuffPtr = localPoolIdBuff; serSize = 0; CHECK(localSet.serializeLocalPoolIds(&localPoolIdBuffPtr, &serSize, maxSize, SerializeIF::Endianness::MACHINE) == OK); CHECK(serSize == maxSize); CHECK(lpIds[0] == localSet.localPoolVarUint8.getDataPoolId()); CHECK(lpIds[1] == localSet.localPoolVarFloat.getDataPoolId()); CHECK(lpIds[2] == localSet.localPoolUint16Vec.getDataPoolId()); { // Test read operation. Values should be all zeros PoolReadGuard readHelper(&localSet); REQUIRE(readHelper.getReadResult() == returnvalue::OK); CHECK(localSet.localPoolVarUint8.value == 0); CHECK(!localSet.localPoolVarUint8.isValid()); CHECK(localSet.localPoolVarFloat.value == Catch::Approx(0.0)); CHECK(!localSet.localPoolVarFloat.isValid()); CHECK(localSet.localPoolUint16Vec.value[0] == 0); CHECK(localSet.localPoolUint16Vec.value[1] == 0); CHECK(localSet.localPoolUint16Vec.value[2] == 0); CHECK(!localSet.localPoolUint16Vec.isValid()); // Now set new values, commit should be done by read helper automatically localSet.localPoolVarUint8 = 232; localSet.localPoolVarUint8.setValid(true); localSet.localPoolVarFloat = -2324.322; localSet.localPoolVarFloat.setValid(true); localSet.localPoolUint16Vec.value[0] = 232; localSet.localPoolUint16Vec.value[1] = 23923; localSet.localPoolUint16Vec.value[2] = 1; localSet.localPoolUint16Vec.setValid(true); } // Zero out some values for next test localSet.localPoolVarUint8 = 0; localSet.localPoolVarFloat = 0; localSet.setAllVariablesReadOnly(); CHECK(localSet.localPoolUint16Vec.getReadWriteMode() == pool_rwm_t::VAR_READ); CHECK(localSet.localPoolVarUint8.getReadWriteMode() == pool_rwm_t::VAR_READ); CHECK(localSet.localPoolVarFloat.getReadWriteMode() == pool_rwm_t::VAR_READ); { // Now we read again and check whether our zeroed values were overwritten with // the values in the pool PoolReadGuard readHelper(&localSet); REQUIRE(readHelper.getReadResult() == returnvalue::OK); CHECK(localSet.localPoolVarUint8.value == 232); CHECK(localSet.localPoolVarUint8.isValid()); CHECK(localSet.localPoolVarFloat.value == Catch::Approx(-2324.322)); CHECK(localSet.localPoolVarFloat.isValid()); CHECK(localSet.localPoolUint16Vec.value[0] == 232); CHECK(localSet.localPoolUint16Vec.value[1] == 23923); CHECK(localSet.localPoolUint16Vec.value[2] == 1); CHECK(localSet.localPoolUint16Vec.isValid()); // Now we serialize these values into a buffer without the validity buffer maxSize = localSet.getSerializedSize(); CHECK(maxSize == sizeof(uint8_t) + sizeof(uint16_t) * 3 + sizeof(float)); serSize = 0; // Already reserve additional space for validity buffer, will be needed later uint8_t buffer[maxSize + 1]; uint8_t* buffPtr = buffer; CHECK(localSet.serialize(&buffPtr, &serSize, maxSize, SerializeIF::Endianness::MACHINE) == returnvalue::OK); uint8_t rawUint8 = buffer[0]; CHECK(rawUint8 == 232); float rawFloat = 0.0; std::memcpy(&rawFloat, buffer + sizeof(uint8_t), sizeof(float)); CHECK(rawFloat == Catch::Approx(-2324.322)); uint16_t rawUint16Vec[3]; std::memcpy(&rawUint16Vec, buffer + sizeof(uint8_t) + sizeof(float), 3 * sizeof(uint16_t)); CHECK(rawUint16Vec[0] == 232); CHECK(rawUint16Vec[1] == 23923); CHECK(rawUint16Vec[2] == 1); size_t sizeToDeserialize = maxSize; // Now we zeros out the raw entries and deserialize back into the dataset std::memset(buffer, 0, sizeof(buffer)); const uint8_t* constBuffPtr = buffer; CHECK(localSet.deSerialize(&constBuffPtr, &sizeToDeserialize, SerializeIF::Endianness::MACHINE) == returnvalue::OK); // Check whether deserialization was successfull CHECK(localSet.localPoolVarUint8.value == 0); CHECK(localSet.localPoolVarFloat.value == Catch::Approx(0.0)); CHECK(localSet.localPoolVarUint8.value == 0); CHECK(localSet.localPoolUint16Vec.value[0] == 0); CHECK(localSet.localPoolUint16Vec.value[1] == 0); CHECK(localSet.localPoolUint16Vec.value[2] == 0); } } SECTION("SharedDataSet") { object_id_t sharedSetId = objects::SHARED_SET_ID; SharedSet sharedSet(poolOwner.sharedPool, sharedSetId, 5, false); localSet.localPoolVarUint8.setReadWriteMode(pool_rwm_t::VAR_WRITE); localSet.localPoolUint16Vec.setReadWriteMode(pool_rwm_t::VAR_WRITE); CHECK(sharedSet.registerVariable(&localSet.localPoolVarUint8) == returnvalue::OK); CHECK(sharedSet.registerVariable(&localSet.localPoolUint16Vec) == returnvalue::OK); { PoolReadGuard rg(&sharedSet); CHECK(rg.getReadResult() == returnvalue::OK); localSet.localPoolVarUint8.value = 5; localSet.localPoolUint16Vec.value[0] = 1; localSet.localPoolUint16Vec.value[1] = 2; localSet.localPoolUint16Vec.value[2] = 3; CHECK(sharedSet.commit() == returnvalue::OK); } } SECTION("Serialize with Validity Blob") { localSet.serializeWithValidityBlob = true; CHECK(!localSet.localPoolVarUint8.isValid()); CHECK(!localSet.localPoolUint16Vec.isValid()); CHECK(!localSet.localPoolVarFloat.isValid()); localSet.setChildrenValidity(true); CHECK(localSet.localPoolVarUint8.isValid()); CHECK(localSet.localPoolUint16Vec.isValid()); CHECK(localSet.localPoolVarFloat.isValid()); size_t serSize = 0; // Already reserve additional space for validity buffer, will be needed later uint8_t buffer[128]{}; uint8_t* buffPtr = buffer; CHECK(localSet.serialize(&buffPtr, &serSize, sizeof(buffer), SerializeIF::Endianness::MACHINE) == returnvalue::OK); CHECK(serSize == 12); CHECK(buffer[11] == 0b11100000); } }