Organized tests according to folders where they are defined

This commit is contained in:
Vladyslav Baranovskyi
2024-11-10 19:57:40 +02:00
parent 29238d94d8
commit 0132fc677e
11 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,240 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include <vector>
#include <stdexcept>
#include "Data/Containers/Array.hpp"
class NonTrivial
{
public:
static int destructorCount;
NonTrivial() = default;
~NonTrivial() { destructorCount++; }
};
int NonTrivial::destructorCount = 0;
using namespace OpenVulkano;
TEST_CASE("Default constructor", "[Array]")
{
Array<int> array;
REQUIRE(array.Size() == 0);
REQUIRE(array.Empty());
}
TEST_CASE("Constructor with size", "[Array]")
{
Array<int> array(5);
REQUIRE(array.Size() == 5);
Array<int> arr(5, 42);
REQUIRE(arr.Size() == 5);
for (size_t i = 0; i < arr.Size(); ++i)
{
REQUIRE(arr[i] == 42);
}
}
TEST_CASE("Constructor with initializer list", "[Array]")
{
Array<int> array { 1, 2, 3 };
REQUIRE(array.Size() == 3);
REQUIRE(array[0] == 1);
REQUIRE(array[1] == 2);
REQUIRE(array[2] == 3);
}
TEST_CASE("Copy constructor", "[Array]")
{
Array<int> array1 { 1, 2, 3 };
Array<int> array2(array1);
REQUIRE(array2.Size() == 3);
REQUIRE(array2 == array1);
}
TEST_CASE("Move constructor", "[Array]")
{
Array<int> array1 { 1, 2, 3 };
Array<int> array2(std::move(array1));
REQUIRE(array2.Size() == 3);
REQUIRE(array1.Size() == 0);
REQUIRE(array1.Empty());
}
TEST_CASE("Constructor from std::vector", "[Array]")
{
std::vector<int> vec { 4, 5, 6 };
Array<int> array(vec);
REQUIRE(array.Size() == 3);
REQUIRE(array[0] == 4);
REQUIRE(array[1] == 5);
REQUIRE(array[2] == 6);
}
TEST_CASE("Constructor with default value", "[Array]")
{
Array<int> array(3, 7);
REQUIRE(array.Size() == 3);
for (size_t i = 0; i < 3; ++i)
{
REQUIRE(array[i] == 7);
}
}
TEST_CASE("Resize method", "[Array]")
{
Array<int> array { 1, 2, 3 };
array = { 4, 5 };
REQUIRE(array.Size() == 2);
REQUIRE(array[0] == 4);
REQUIRE(array[1] == 5);
}
TEST_CASE("Destructor for non-trivially destructible types", "[Array]")
{
{
NonTrivial::destructorCount = 0;
Array<NonTrivial> array(3);
}
REQUIRE(NonTrivial::destructorCount == 3);
}
TEST_CASE("At() method with bounds checking", "[Array]")
{
Array<int> array { 1, 2, 3 };
REQUIRE(array.At(1) == 2);
REQUIRE_THROWS_AS(array.At(5), std::out_of_range);
}
TEST_CASE("Assignment operator", "[Array]")
{
Array<int> array1 { 1, 2, 3 };
Array<int> array2;
array2 = array1;
REQUIRE(array2 == array1);
}
TEST_CASE("Move assignment operator", "[Array]")
{
Array<int> array1 { 1, 2, 3 };
Array<int> array2;
array2 = std::move(array1);
REQUIRE(array2.Size() == 3);
REQUIRE(array1.Size() == 0);
}
TEST_CASE("Comparison operators", "[Array]")
{
Array<int> array1 { 1, 2, 3 };
Array<int> array2 { 1, 2, 3 };
Array<int> array3 { 4, 5, 6 };
REQUIRE(array1 == array2);
REQUIRE(array1 != array3);
REQUIRE(array1 < array3);
REQUIRE(array3 > array1);
REQUIRE(array1 <= array2);
REQUIRE(array1 >= array2);
}
TEST_CASE("Fill method", "[Array]")
{
Array<int> array(5);
array.Fill(42);
for (size_t i = 0; i < 5; ++i)
{
REQUIRE(array[i] == 42);
}
}
TEST_CASE("Front and Back methods", "[Array]")
{
Array<int> array { 1, 2, 3 };
REQUIRE(array.Front() == 1);
REQUIRE(array.Back() == 3);
}
TEST_CASE("Iterators", "[Array]")
{
Array<int> array { 1, 2, 3 };
int sum = 0;
for (auto it = array.Begin(); it != array.End(); ++it)
{
sum += *it;
}
REQUIRE(sum == 6);
sum = 0;
for (auto it = array.ReverseBegin(); it != array.ReverseEnd(); ++it)
{
sum += *it;
}
REQUIRE(sum == 6);
Array<int> arr = { 1, 2, 3, 4, 5 };
{
int sum = 0;
for (auto it = arr.Begin(); it != arr.End(); ++it)
{
sum += *it;
}
REQUIRE(sum == 15);
}
{
std::vector<int> reversedElements;
for (auto it = arr.ReverseBegin(); it != arr.ReverseEnd(); ++it)
{
reversedElements.push_back(*it);
}
REQUIRE(reversedElements == std::vector<int>({ 5, 4, 3, 2, 1 }));
}
{
const Array<int> constArr = { 1, 2, 3, 4, 5 };
int sum = 0;
for (auto it = constArr.Begin(); it != constArr.End(); ++it)
{
sum += *it;
}
REQUIRE(sum == 15);
}
{
const Array<int> constArr = { 1, 2, 3, 4, 5 };
std::vector<int> reversedElements;
for (auto it = constArr.ReverseBegin(); it != constArr.ReverseEnd(); ++it)
{
reversedElements.push_back(*it);
}
REQUIRE(reversedElements == std::vector<int>({ 5, 4, 3, 2, 1 }));
}
}
TEST_CASE("Data method", "[Array]")
{
Array<int> array { 1, 2, 3 };
int* data = array.Data();
REQUIRE(data[0] == 1);
REQUIRE(data[1] == 2);
REQUIRE(data[2] == 3);
}
TEST_CASE("Swap method", "[Array]")
{
Array<int> arr1 = { 1, 2, 3 };
Array<int> arr2 = { 4, 5, 6 };
arr1.Swap(arr2);
REQUIRE(arr1[0] == 4);
REQUIRE(arr2[0] == 1);
}

View File

@@ -0,0 +1,141 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include <vector>
#include <map>
#include <utility>
#include <type_traits>
#include "Data/Containers/BinSearchArrayMap.hpp"
#include "Data/Containers/StableVector.hpp"
using namespace OpenVulkano;
TEST_CASE("BinSearchArrayMap With Default")
{
SECTION("Insert")
{
BinSearchArrayMap<int, std::string> map;
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
REQUIRE(map.Size() == 50);
REQUIRE(map.Get(16) == "16");
REQUIRE(map.Get(23) == "23");
REQUIRE(map.Get(48) == "48");
}
SECTION("Remove")
{
BinSearchArrayMap<int, std::string> map;
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
map.Remove(16);
map.Remove(23);
map.Remove(48);
REQUIRE(map.Size() == 47);
for (int i = 0; i < 50; i++)
{
if (i == 16 || i == 23 || i == 48)
{
REQUIRE(!map.Contains(i));
}
else
{
REQUIRE(map.Get(i) == std::to_string(i));
}
}
}
SECTION("Emplace")
{
BinSearchArrayMap<int, std::string> map;
for (int i = 0; i < 50; i++)
{
map.Emplace(i, std::to_string(i));
}
REQUIRE(map.Size() == 50);
REQUIRE(map.Get(16) == "16");
REQUIRE(map.Get(23) == "23");
REQUIRE(map.Get(48) == "48");
}
SECTION("FindPair")
{
BinSearchArrayMap<int, std::string> map;
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
auto pair = map.FindPair(16);
REQUIRE(pair.first == 16);
REQUIRE(pair.second == "16");
}
}
TEST_CASE("BinSearchArrayMap With StableVector")
{
SECTION("Insert")
{
BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
REQUIRE(map.Size() == 50);
REQUIRE(map.Get(16) == "16");
REQUIRE(map.Get(23) == "23");
REQUIRE(map.Get(48) == "48");
for (int i = 0; i < 50; i++)
{
REQUIRE(map.Get(i) == std::to_string(i));
}
}
SECTION("Remove")
{
BinSearchArrayMap<int, std::string, std::pair, StableVector> map;
for (int i = 0; i < 50; i++)
{
map.Insert(i, std::to_string(i));
}
map.Remove(16);
map.Remove(23);
map.Remove(48);
for (int i = 0; i < 50; i++)
{
if (i == 16 || i == 23 || i == 48)
{
REQUIRE(!map.Contains(i));
}
else
{
REQUIRE(map.Get(i) == std::to_string(i));
}
}
}
}

View File

@@ -0,0 +1,192 @@
#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);
REQUIRE(vec[56] == "a");
vec.Push("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);
}
}

View File

@@ -0,0 +1,281 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_all.hpp>
#include "Data/Containers/String.hpp"
using namespace OpenVulkano;
TEST_CASE("Constructors")
{
String str1;
REQUIRE(str1 == "");
String str2("Hello");
REQUIRE(str2 == "Hello");
String str3(std::string("World"));
REQUIRE(str3 == "World");
String str4(str2);
REQUIRE(str4 == "Hello");
String str5(std::move(str3));
REQUIRE(str5 == "World");
}
TEST_CASE("Assignment")
{
String str1;
str1 = "Hello";
REQUIRE(str1 == "Hello");
String str2;
str2 = std::string("World");
REQUIRE(str2 == "World");
String str3;
str3 = str1;
REQUIRE(str3 == "Hello");
String str4;
str4 = std::move(str2);
REQUIRE(str4 == "World");
}
TEST_CASE("Concatenation")
{
String str1("Hello");
str1 += " World";
REQUIRE(str1 == "Hello World");
String str2("Hello");
String str3 = str2 + " World";
REQUIRE(str3 == "Hello World");
String str4("Hello");
str4 += std::string(" World");
REQUIRE(str4 == "Hello World");
String str5("Hello");
String str6 = str5 + std::string(" World");
REQUIRE(str6 == "Hello World");
String str7("Hello");
str7 += String(" World");
REQUIRE(str7 == "Hello World");
String str8("Hello");
String str9 = str8 + String(" World");
REQUIRE(str9 == "Hello World");
String str10 = String("Hello") + " World";
REQUIRE(str10 == "Hello World");
String str11 = String("Hello") + std::string(" World");
REQUIRE(str11 == "Hello World");
String str12 = std::string("Hello") + String(" World");
REQUIRE(str12 == "Hello World");
}
TEST_CASE("Comparison")
{
String str1("Hello");
REQUIRE(str1 == "Hello");
REQUIRE(str1 != "World");
String str2("World");
REQUIRE(str2 == "World");
REQUIRE(str2 != "Hello");
}
TEST_CASE("Trim")
{
String str1(" Hello World ");
REQUIRE(str1.Trim() == "Hello World");
}
TEST_CASE("FindIndex")
{
String str1("Georg will save us all from our doom");
REQUIRE(str1.FindStartIndexOf("save") == 11);
}
TEST_CASE("Upper/Lower")
{
String str1("Hello World");
str1.ToUpper();
REQUIRE(str1 == "HELLO WORLD");
String str2("Hello World");
str2.ToLower();
REQUIRE(str2 == "hello world");
}
TEST_CASE("Substring")
{
String str1("Hello World");
REQUIRE(str1.SubString(0, 5) == "Hello");
REQUIRE(str1.SubString(6, 11) == "World");
}
TEST_CASE("FromString")
{
String str1("42");
REQUIRE(str1.FromString<int>() == 42);
String str2("42.42");
REQUIRE(str2.FromString<float>() == 42.42f);
}
TEST_CASE("StartsWith")
{
String str1("Hello World");
REQUIRE(str1.StartsWith("Hello"));
REQUIRE(!str1.StartsWith("World"));
}
TEST_CASE("EndsWith")
{
String str1("Hello World");
REQUIRE(str1.EndsWith("World"));
REQUIRE(!str1.EndsWith("Hello"));
}
TEST_CASE("Contains")
{
String str1("Hello World");
REQUIRE(str1.Contains("Hello"));
REQUIRE(str1.Contains("World"));
REQUIRE(!str1.Contains("Georg"));
}
TEST_CASE("FindEndIndexOf")
{
String str1("Georg will save us all from our doom");
REQUIRE(str1.FindEndIndexOf("save") == 11);
}
TEST_CASE("TrimFront")
{
String str1(" Hello World");
REQUIRE(str1.TrimFront() == "Hello World");
}
TEST_CASE("TrimBack")
{
String str1("Hello World ");
REQUIRE(str1.TrimBack() == "Hello World");
}
TEST_CASE("Empty")
{
String str1;
REQUIRE(str1.Empty());
REQUIRE(!str1);
String str2("Hello World");
REQUIRE(!str2.Empty());
REQUIRE(!!str2);
}
TEST_CASE("Size")
{
String str1("Hello World");
REQUIRE(str1.Size() == 11);
}
TEST_CASE("Capacity")
{
String str1("Hello World");
REQUIRE(str1.Capacity() >= 11);
}
TEST_CASE("CharCount")
{
String str1("Hello World");
REQUIRE(str1.CharCount() == 11);
}
TEST_CASE("PopBack")
{
String str1("Hello World");
str1.PopBack();
REQUIRE(str1 == "Hello Worl");
}
TEST_CASE("Clear")
{
String str1("Hello World");
str1.Clear();
REQUIRE(str1.Empty());
}
TEST_CASE("Split")
{
String str1("Hello,World");
auto split = str1.Split(",");
REQUIRE(split.size() == 2);
REQUIRE(split[0] == "Hello");
REQUIRE(split[1] == "World");
}
TEST_CASE("SplitAtLastOccurrence")
{
String str1 = "Hello,World,Georg";
auto split = str1.SplitAtLastOccurrence(",");
REQUIRE(split.first == "Hello,World");
REQUIRE(split.second == "Georg");
}
TEST_CASE("SplitAtFirstOccurrence")
{
String str1 = "Hello,World,Georg";
auto split = str1.SplitAtFirstOccurrence(",");
REQUIRE(split.first == "Hello");
REQUIRE(split.second == "World,Georg");
}
TEST_CASE("SplitAsStringViews")
{
String str1("Hello,World");
auto split = str1.SplitAsStringViews(",");
REQUIRE(split.size() == 2);
REQUIRE(split[0] == "Hello");
REQUIRE(split[1] == "World");
}
TEST_CASE("SplitAtFirstOccurrenceAsStringViews")
{
String str1 = "Hello,World,Georg";
auto split = str1.SplitAtFirstOccurenceAsStringViews(",");
REQUIRE(split.first == "Hello");
REQUIRE(split.second == "World,Georg");
}
TEST_CASE("SplitAtLastOccurenceAsStringViews")
{
String str1 = "Hello,World,Georg";
auto split = str1.SplitAtLastOccurenceAsStringViews(",");
REQUIRE(split.first == "Hello,World");
REQUIRE(split.second == "Georg");
}
TEST_CASE("OctToInt")
{
String str1("47");
REQUIRE(str1.OctToInt() == 39);
REQUIRE(String::OctToInt("552") == 362);
}
TEST_CASE("HexToInt")
{
String str1("2A");
REQUIRE(str1.HexToInt() == 42);
REQUIRE(String::HexToInt("FF") == 255);
}