Merge remote-tracking branch 'origin/master' into

feature/BinSearchArrayMap
This commit is contained in:
Metehan Tuncbilek
2024-09-23 14:28:30 +03:00
24 changed files with 1516 additions and 153 deletions

126
tests/MemFileTests.cpp Normal file
View File

@@ -0,0 +1,126 @@
#include <catch2/catch_all.hpp>
#include "IO/MemMappedFile.hpp"
#include "IO/MemMappedFileWriteHelper.hpp"
#include "IO/AppFolders.hpp"
#include "Base/Logger.hpp"
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
TEST_CASE("MemMappedFileWrite")
{
OpenVulkano::Logger::SetupLogger("", "tests.log");
auto path = OpenVulkano::AppFolders::GetAppTempDir();
path += "/MemFileTest.txt";
std::string data = "Hello World X\n"
"Hello World Y\n"
"Hello World Z\n"
"Hello World W\n"
"Hello World A\n"
"Hello World B\n"
"Hello World C\n"
"Hello World D\n"
"Hello World E\n"
"Hello World F\n"
"Hello World G\n"
"Hello World H\n"
"Hello World I\n"
"Hello World J\n"
"Hello World K\n"
"Hello World L\n"
"Hello World M\n"
"Hello World N\n";
SECTION("Write to MemMappedFile")
{
OpenVulkano::MemMappedFileWriter writer(path, 1024);
writer.Write(data.data(), data.size());
REQUIRE(writer.GetOffset() == data.size());
}
SECTION("Control Size")
{
OpenVulkano::MemMappedFile memFile(path, OpenVulkano::MemMappedFile::READ_ONLY);
REQUIRE(memFile.Size() == 1024); // The size that comes from the first Test.
}
SECTION("Compare String Data for per char")
{
OpenVulkano::MemMappedFileWriteHelper memFileHelper(path, OpenVulkano::MemMappedFile::READ_ONLY);
REQUIRE(memFileHelper.Trim(data.size()));
std::string testData((char*) memFileHelper.Data());
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] != testData[i])
{
REQUIRE(false);
}
}
memFileHelper.Close();
}
SECTION("Trim File")
{
OpenVulkano::MemMappedFileWriteHelper helper(path,
OpenVulkano::MemMappedFileWriteHelper::USE_CURRENT_FILE_SIZE);
REQUIRE(helper.Trim(100));
REQUIRE(helper.Size() == 100);
helper.Close();
}
SECTION("Write Data 2")
{
OpenVulkano::MemMappedFileWriter writer(path, OpenVulkano::MemMappedFileWriteHelper::USE_CURRENT_FILE_SIZE);
writer.Write(data.data(), data.size());
writer.Write(data.data(), data.size());
writer.Write(data.data(), data.size());
REQUIRE(writer.GetOffset() == data.size() * 3);
}
SECTION("Compare Data")
{
OpenVulkano::MemMappedFileWriteHelper helper(path,
OpenVulkano::MemMappedFileWriteHelper::USE_CURRENT_FILE_SIZE);
REQUIRE(helper.Data() != nullptr);
std::string testData((char*) helper.Data());
printf("size: %llu", helper.Size());
helper.Close();
std::ifstream file(path, std::ios::binary);
std::string streamData;
std::getline(file, streamData, '\0');
file.close();
for (size_t i = 0; i < testData.size(); i++)
{
if (streamData[i] != testData[i])
{
REQUIRE(false);
}
}
REQUIRE(streamData.size() == testData.size());
printf("helper size: %llu\n", helper.Size());
}
SECTION("Actual Size")
{
std::error_code ec;
std::uintmax_t size = fs::file_size(path, ec);
REQUIRE(size == 756);
}
}

190
tests/StableVectorTest.cpp Normal file
View File

@@ -0,0 +1,190 @@
#include <catch2/catch_all.hpp>
#include "Data/Containers/StableVector.hpp"
using namespace OpenVulkano;
struct Test
{
uint32_t mValue;
Test(uint32_t value) : mValue(value) {}
Test() : mValue(0) {}
};
static int incrementCounter = 0;
static int decrementCounter = 0;
struct TestCount
{
TestCount() : m_value("") { ++incrementCounter; }
TestCount(const std::string& val) : m_value(val) { ++incrementCounter; }
TestCount(TestCount&& other) noexcept : m_value(std::move(other.m_value)) { ++incrementCounter; }
TestCount(const TestCount& other) : m_value(other.m_value) { ++incrementCounter; }
TestCount& operator=(const TestCount& other)
{
m_value = other.m_value;
return *this;
}
TestCount& operator=(TestCount&& other) noexcept
{
m_value = std::move(other.m_value);
return *this;
}
~TestCount() { ++decrementCounter; }
std::string m_value;
};
TEST_CASE("ChunkVector")
{
SECTION("PushBack")
{
StableVector<uint32_t> vec;
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
for (uint32_t i = 0; i < 100; ++i)
{
vec.PushBack(i);
}
REQUIRE(vec.Size() == 100);
REQUIRE(vec.Capacity() == 224); // 32 + 64 + 128 = 3 | previous chunk size multiplied by 2
}
SECTION("EmplaceBack")
{
StableVector<Test> vec;
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
for (uint32_t i = 0; i < 100; ++i)
{
vec.EmplaceBack(i);
}
REQUIRE(vec.Size() == 100);
for (uint32_t i = 0; i < 100; ++i)
{
REQUIRE(vec[i].mValue == i);
}
}
SECTION("PopBack")
{
StableVector<uint32_t> vec;
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
for (uint32_t i = 0; i < 100; ++i)
{
vec.PushBack(i);
}
REQUIRE(vec.Size() == 100);
uint64_t tempVal = vec.Capacity();
for (uint32_t i = 0; i < 50; ++i)
{
vec.PopBack();
}
REQUIRE(vec.Size() == 50);
REQUIRE(vec.Capacity() == tempVal);
}
SECTION("Clear")
{
StableVector<uint32_t> vec;
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
for (uint32_t i = 0; i < 100; ++i)
{
vec.PushBack(i);
}
REQUIRE(vec.Size() == 100);
vec.Clear();
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
}
SECTION("Add/Fill")
{
StableVector<std::string> vec;
REQUIRE(vec.Size() == 0);
REQUIRE(vec.Capacity() == 32);
for (uint32_t i = 0; i < 100; ++i)
{
vec.PushBack("a");
}
REQUIRE(vec.Size() == 100);
vec.Remove(56);
vec.Add("z");
REQUIRE(vec.Size() == 100);
REQUIRE(vec[56] == "z");
}
SECTION("Correct Initialization")
{
StableVector<TestCount> vec;
REQUIRE(incrementCounter == 0);
REQUIRE(decrementCounter == 0);
vec.EmplaceBack("a");
REQUIRE(incrementCounter == 1);
REQUIRE(decrementCounter == 0);
vec.PushBack(TestCount("b"));
REQUIRE(incrementCounter == 3);
REQUIRE(decrementCounter == 1);
TestCount testClass = TestCount("c");
vec.PushBack(std::move(testClass));
REQUIRE(incrementCounter == 5);
REQUIRE(decrementCounter == 1);
vec.Clear();
REQUIRE(incrementCounter == 5);
REQUIRE(decrementCounter == 4);
vec.PushBack(TestCount("d"));
REQUIRE(incrementCounter == 7);
REQUIRE(decrementCounter == 5);
TestCount testClass2 = TestCount("e");
vec.PushBack(testClass2);
REQUIRE(incrementCounter == 9);
REQUIRE(decrementCounter == 5);
}
SECTION("Out of scope check")
{
REQUIRE(incrementCounter == 9);
REQUIRE(decrementCounter == 9);
}
}