From 617bb2828d20313a961421d815bef4e9caf338bd Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 18 Dec 2023 08:37:25 +0100 Subject: [PATCH] Transitioning unit tests to gtest --- CMakeLists.txt | 1 + CMakeSettings.json | 14 +- test/CMakeLists.txt | 35 --- test/Engine/CMakeLists.txt | 12 - test/Engine/Containers/Array.cpp | 220 ------------------ test/Engine/Containers/Map.cpp | 95 -------- test/Engine/EngineTest.cpp | 119 ---------- test/Engine/EngineTest.h | 20 -- test/Engine/Graphics/GraphicsResources.cpp | 13 -- test/Engine/Math/CMakeLists.txt | 4 - test/Engine/Math/Vector.cpp | 46 ---- test/Engine/Serialization/ArchiveBuffer.cpp | 10 - test/Engine/Serialization/CMakeLists.txt | 3 - test/Engine/ThreadPool.cpp | 220 ------------------ tests/CMakeLists.txt | 14 ++ tests/Engine/CMakeLists.txt | 10 + tests/Engine/Containers/Array.cpp | 195 ++++++++++++++++ .../Engine/Containers/CMakeLists.txt | 2 +- {test => tests}/Engine/Containers/List.cpp | 33 ++- tests/Engine/Containers/Map.cpp | 91 ++++++++ tests/Engine/EngineTest.h | 5 + .../Engine/Graphics/CMakeLists.txt | 2 +- tests/Engine/Graphics/GraphicsResources.cpp | 2 + tests/Engine/Math/CMakeLists.txt | 3 + tests/Engine/Math/Vector.cpp | 38 +++ tests/Engine/Serialization/ArchiveBuffer.cpp | 1 + tests/Engine/Serialization/CMakeLists.txt | 3 + tests/Engine/ThreadPool.cpp | 22 ++ vcpkg.json | 3 +- 29 files changed, 416 insertions(+), 820 deletions(-) delete mode 100644 test/CMakeLists.txt delete mode 100644 test/Engine/CMakeLists.txt delete mode 100644 test/Engine/Containers/Array.cpp delete mode 100644 test/Engine/Containers/Map.cpp delete mode 100644 test/Engine/EngineTest.cpp delete mode 100644 test/Engine/EngineTest.h delete mode 100644 test/Engine/Graphics/GraphicsResources.cpp delete mode 100644 test/Engine/Math/CMakeLists.txt delete mode 100644 test/Engine/Math/Vector.cpp delete mode 100644 test/Engine/Serialization/ArchiveBuffer.cpp delete mode 100644 test/Engine/Serialization/CMakeLists.txt delete mode 100644 test/Engine/ThreadPool.cpp create mode 100644 tests/CMakeLists.txt create mode 100644 tests/Engine/CMakeLists.txt create mode 100644 tests/Engine/Containers/Array.cpp rename {test => tests}/Engine/Containers/CMakeLists.txt (56%) rename {test => tests}/Engine/Containers/List.cpp (50%) create mode 100644 tests/Engine/Containers/Map.cpp create mode 100644 tests/Engine/EngineTest.h rename {test => tests}/Engine/Graphics/CMakeLists.txt (52%) create mode 100644 tests/Engine/Graphics/GraphicsResources.cpp create mode 100644 tests/Engine/Math/CMakeLists.txt create mode 100644 tests/Engine/Math/Vector.cpp create mode 100644 tests/Engine/Serialization/ArchiveBuffer.cpp create mode 100644 tests/Engine/Serialization/CMakeLists.txt create mode 100644 tests/Engine/ThreadPool.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 70d2044..93fb8de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,7 @@ target_compile_options(Engine PUBLIC "$<$:-DENABLE_VALIDATION>") target_compile_options(Engine PUBLIC "$<$:-DSEELE_DEBUG>") add_subdirectory(src/) +add_subdirectory(tests/) if(WIN32) add_custom_target(dll_copy ALL diff --git a/CMakeSettings.json b/CMakeSettings.json index 6f5248a..3e49ade 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -11,7 +11,6 @@ "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=\"\" -DCMAKE_PLATFORM=x64", "buildRoot": "C:/Users/Dynamitos/Seele/bin/", "installRoot": "C:/Program Files/Seele", - "addressSanitizerEnabled": true }, { "name": "Release", @@ -36,6 +35,19 @@ "ctestCommandArgs": "", "inheritEnvironments": [ "msvc_x64" ], "intelliSenseMode": "windows-msvc-x64" + }, + { + "name": "AddressSanitizer", + "buildCommandArgs": "", + "ctestCommandArgs": "", + "configurationType": "Debug", + "generator": "Visual Studio 17 2022 Win64", + "intelliSenseMode": "windows-msvc-x64", + "inheritEnvironments": [ "msvc_x64" ], + "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=\"\" -DCMAKE_PLATFORM=x64", + "buildRoot": "C:/Users/Dynamitos/Seele/bin/", + "installRoot": "C:/Program Files/Seele", + "addressSanitizerEnabled": true } ] } \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index e6312e5..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ".") - -find_package(Boost COMPONENTS unit_test_framework REQUIRED) -find_package(Vulkan REQUIRED) - -add_subdirectory(Engine/) - -target_include_directories(Seele_unit_tests PRIVATE ${ENGINE_ROOT}) - -target_link_libraries(Seele_unit_tests PRIVATE Engine) -target_link_libraries(Seele_unit_tests PRIVATE Boost::unit_test_framework) -if(UNIX) - target_compile_definitions(Seele_unit_tests PRIVATE -DBOOST_TEST_DYN_LINK) -endif() - -target_precompile_headers(Seele_unit_tests - PRIVATE - - - - - - - - - - - - - - ) - - -enable_testing() -add_test(SeeleEngineTest Seele_unit_tests) diff --git a/test/Engine/CMakeLists.txt b/test/Engine/CMakeLists.txt deleted file mode 100644 index 5fb8aa3..0000000 --- a/test/Engine/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -target_sources(Seele_unit_tests - PRIVATE - ../../src/Engine/MinimalEngine.cpp - ../../src/Engine/ThreadPool.cpp - EngineTest.h - EngineTest.cpp) - #ThreadPool.cpp) -target_include_directories(Seele_unit_tests PUBLIC ./) -add_subdirectory(Containers/) -add_subdirectory(Graphics/) -add_subdirectory(Math/) -add_subdirectory(Serialization/) \ No newline at end of file diff --git a/test/Engine/Containers/Array.cpp b/test/Engine/Containers/Array.cpp deleted file mode 100644 index 267f6a2..0000000 --- a/test/Engine/Containers/Array.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include "EngineTest.h" -#include "Containers/Array.h" -#include "MinimalEngine.h" -#include -#include -#include -#include - -using namespace Seele; - -BOOST_AUTO_TEST_SUITE(Array_Suite) - -BOOST_AUTO_TEST_CASE(empty_constructur) -{ - Array array; - BOOST_CHECK_EQUAL(array.size(), 0); -} - -BOOST_AUTO_TEST_CASE(initialial_size) -{ - Array array(3); - BOOST_CHECK_EQUAL(array.size(), 3); -} - -BOOST_AUTO_TEST_CASE(resize) -{ - Array array; - array.add(2); - array.add(3); - BOOST_CHECK_EQUAL(array.size(), 2); - array.resize(5); - BOOST_CHECK_EQUAL(array.size(), 5); -} -BOOST_AUTO_TEST_CASE(clear) -{ - Array array; - array.add(3); - array.add(2); - array.add(6); - BOOST_CHECK_EQUAL(array.size(), 3); - array.clear(); - BOOST_CHECK_EQUAL(array.size(), 0); - array.add(2); -} -BOOST_AUTO_TEST_CASE(remove_keeporder) -{ - Array array; - array.add(1); - array.add(2); - array.add(3); - array.add(4); - array.add(5); - array.remove(1); - BOOST_CHECK_EQUAL(array[1], 3); - BOOST_CHECK_EQUAL(array[2], 4); - BOOST_CHECK_EQUAL(array[3], 5); - BOOST_CHECK_EQUAL(array.size(), 4); -} - -BOOST_AUTO_TEST_CASE(find) -{ - Array array; - for(uint8 i = 0; i < 100; ++i) - { - array.add(i); - } - auto it = array.find([](uint8 elem){ return elem / 9 == 9; }); - BOOST_CHECK_EQUAL(*it, 81); -} - -BOOST_AUTO_TEST_CASE(remove_swap) -{ - Array array; - array.add(1); - array.add(2); - array.add(3); - array.add(4); - array.add(5); - array.removeAt(1, false); - BOOST_CHECK_EQUAL(array[1], 5); - BOOST_CHECK_EQUAL(array[2], 3); - BOOST_CHECK_EQUAL(array[3], 4); - BOOST_CHECK_EQUAL(array.size(), 4); -} - -BOOST_AUTO_TEST_CASE(remove_iterator) -{ - Array array; - array.add(1); - array.add(2); - array.add(3); - array.add(4); - array.add(5); - array.remove(array.find(1), false); - BOOST_CHECK_EQUAL(array[0], 5); - BOOST_CHECK_EQUAL(array[1], 2); - BOOST_CHECK_EQUAL(array[2], 3); - BOOST_CHECK_EQUAL(array[3], 4); - BOOST_CHECK_EQUAL(array.size(), 4); -} - -BOOST_AUTO_TEST_CASE(remove_iterator_keep_order) -{ - Array array; - array.add(1); - array.add(2); - array.add(3); - array.add(4); - array.add(5); - array.remove(array.find(1)); - BOOST_CHECK_EQUAL(array[0], 2); - BOOST_CHECK_EQUAL(array[1], 3); - BOOST_CHECK_EQUAL(array[2], 4); - BOOST_CHECK_EQUAL(array[3], 5); - BOOST_CHECK_EQUAL(array.size(), 4); -} - -BOOST_AUTO_TEST_CASE(random_access) -{ - Array array; - array.add(4); - array.add(5); - array.add(6); - BOOST_CHECK_EQUAL(array[2], 6); - BOOST_CHECK_EQUAL(array[0], 4); -} - -BOOST_AUTO_TEST_CASE(copy) -{ - Array array; - array.add(0); - array.add(1); - array.add(2); - array.add(3); - Array copy = array; - BOOST_CHECK_EQUAL_COLLECTIONS(array.begin(), array.end(), copy.begin(), copy.end()); - Array copy2(copy); - BOOST_CHECK_EQUAL_COLLECTIONS(array.begin(), array.end(), copy2.begin(), copy2.end()); -} - -class BaseElement -{ - uint64 test; -}; - -class BaseContainer -{ - Array elements; -}; - -class DerivedContainer : public BaseContainer -{ - uint64 test; -}; - -BOOST_AUTO_TEST_CASE(virtual_classes) -{ - Array array(64); - for(uint32 i = 0; i < 100; ++i) - { - Array copy = array; - BOOST_CHECK_EQUAL(array.size(), copy.size()); - } -} - -class TestStruct -{ -public: - uint32 data; - ~TestStruct() - { - data = 1; - } -}; -DECLARE_REF(TestStruct); - -BOOST_AUTO_TEST_CASE(refptr_interaction) -{ - uint32* dataPtr; - { - PTestStruct test = new TestStruct(); - test->data = 32123; - dataPtr = &test->data; - { - Array arr(1); - arr[0] = test; - } - BOOST_CHECK_EQUAL(test->data, 32123); - } -} - -BOOST_AUTO_TEST_SUITE_END() - - -BOOST_AUTO_TEST_SUITE(StaticArray_Suite) - -BOOST_AUTO_TEST_CASE(empty_constructur) -{ - StaticArray array; - BOOST_CHECK_EQUAL(array.size(), 2); -} - -BOOST_AUTO_TEST_CASE(initialial_size) -{ - StaticArray array(3); - BOOST_CHECK_EQUAL(array.size(), 3); - BOOST_CHECK_EQUAL(array[1], 3); -} - -BOOST_AUTO_TEST_CASE(random_access) -{ - StaticArray array; - array[0] = 4; - array[1] = 5; - array[2] = 6; - BOOST_CHECK_EQUAL(array[0], 4); - BOOST_CHECK_EQUAL(array[2], 6); -} - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/Engine/Containers/Map.cpp b/test/Engine/Containers/Map.cpp deleted file mode 100644 index 16b2bbf..0000000 --- a/test/Engine/Containers/Map.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "EngineTest.h" -#include "Containers/Map.h" -#include - -using namespace Seele; - -BOOST_AUTO_TEST_SUITE(CachedMap) - -BOOST_AUTO_TEST_CASE(insert_find_basic) -{ - Map map; - map[2] = 3; - map[1] = 5; - map[6] = 4; - map[4] = 7; - BOOST_REQUIRE_EQUAL(map[2], 3); - BOOST_REQUIRE_EQUAL(map[1], 5); - BOOST_REQUIRE_EQUAL(map[6], 4); - BOOST_REQUIRE_EQUAL(map[4], 7); - map[2] = 5; - map[4] = 4; - BOOST_REQUIRE_EQUAL(map[2], 5); - BOOST_REQUIRE_EQUAL(map[4], 4); - BOOST_REQUIRE_EQUAL(map.find(2)->value, 5); - BOOST_REQUIRE_EQUAL(map.find(4)->value, 4); - //BOOST_REQUIRE_EQUAL(map.find(5), map.end()); -} - -BOOST_AUTO_TEST_CASE(for_each) -{ - Map map; - map[2] = 3; - map[1] = 5; - map[6] = 4; - map[4] = 7; - int count = 0; - for(auto it : map) - { - count++; - } - BOOST_REQUIRE_EQUAL(count, 4); -} - -BOOST_AUTO_TEST_CASE(remove_entry) -{ - Map map; - map[1] = 5; - map[4] = 1; - map[2] = 6; - map[3] = 4; - map.erase(2); - BOOST_REQUIRE_EQUAL(map.size(), 3); - BOOST_REQUIRE_EQUAL(map[1], 5); - BOOST_REQUIRE_EQUAL(map[4], 1); - BOOST_REQUIRE_EQUAL(map[3], 4); -} - -BOOST_AUTO_TEST_CASE(key_exists) -{ - Map map; - map[2] = 3; - BOOST_REQUIRE_EQUAL(map.exists(2), true); - BOOST_REQUIRE_EQUAL(map.exists(4), false); - map.erase(2); - BOOST_REQUIRE_EQUAL(map.exists(2), false); -} - -BOOST_AUTO_TEST_CASE(custom_key) -{ - struct Key - { - int id; - bool operator<(const Key& other) const - { - return id < other.id; - } - }; - Map map; - map[Key{ 2 }] = 3; - map[Key{ 3 }] = 4; - BOOST_REQUIRE_EQUAL(map[Key{ 2 }], 3); - BOOST_REQUIRE_EQUAL(map[Key{ 3 }], 4); - -} - -BOOST_AUTO_TEST_CASE(string_key) -{ - std::map map; - map["Test"] = 2; - map["Test2"] = 3; - BOOST_REQUIRE_EQUAL(map["Test"], 2); - BOOST_REQUIRE_EQUAL(map["Test2"], 3); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/test/Engine/EngineTest.cpp b/test/Engine/EngineTest.cpp deleted file mode 100644 index 9bf57ed..0000000 --- a/test/Engine/EngineTest.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "EngineTest.h" -#include "MinimalEngine.h" -#define BOOST_TEST_MODULE SeeleEngine -#include -//#include - -using namespace Seele; - -Seele::GlobalFixture::~GlobalFixture() -{ - //getGlobalThreadPool().waitIdle(); -} - -BOOST_TEST_GLOBAL_FIXTURE(GlobalFixture); -BOOST_AUTO_TEST_SUITE(RefPtr) - -struct TestStruct -{ - TestStruct() - : data(10) - { - } - virtual ~TestStruct() - { - data = 15; - } - uint32 data; -}; - -struct DeclStruct; -BOOST_AUTO_TEST_CASE(basic_refcount) -{ - { - Seele::RefPtr ptr = new TestStruct(); - BOOST_REQUIRE_EQUAL(ptr->data, 10); - { - Seele::RefPtr secondPtr = ptr; - BOOST_REQUIRE_EQUAL(secondPtr->data, 10); - BOOST_REQUIRE_EQUAL(ptr->data, 10); - } - BOOST_REQUIRE_EQUAL(ptr->data, 10); - } -} -struct DeclStruct -{ - ~DeclStruct() - { - data = 10; - } - uint32 data = 20; -}; - -struct DerivedStruct : public TestStruct -{ - DerivedStruct() - :data2(20) - { - - } - ~DerivedStruct() - { - data2 = 30; - } - uint32 data2; -}; - -BOOST_AUTO_TEST_CASE(inheritance_cast) -{ - Seele::RefPtr backCast; - { - Seele::RefPtr derived = new DerivedStruct(); - Seele::RefPtr base = derived; - BOOST_REQUIRE_EQUAL(base->data, 10); - backCast = base.cast(); - BOOST_REQUIRE_EQUAL(backCast->data, 10); - BOOST_REQUIRE_EQUAL(backCast->data2, 20); - } - BOOST_REQUIRE_EQUAL(backCast->data, 10); - BOOST_REQUIRE_EQUAL(backCast->data2, 20); -} - -BOOST_AUTO_TEST_CASE(unique_ptr) -{ - Seele::UniquePtr uptr = new TestStruct(); - Seele::UniquePtr uptr2 = std::move(uptr); - BOOST_REQUIRE_EQUAL(uptr2->data, 10); - BOOST_REQUIRE_EQUAL(uptr.isValid(), false); -} -struct ThisReference -{ - ThisReference() - { - } - ~ThisReference() - { - data = 12; - } - Seele::RefPtr getRef() - { - return this; - } - uint32 data = 1; -}; - -BOOST_AUTO_TEST_CASE(this_reference) -{ - Seele::RefPtr ptr2; - { - Seele::RefPtr ptr = new ThisReference(); - BOOST_REQUIRE_EQUAL(ptr->data, 1); - ptr2 = ptr->getRef(); - BOOST_REQUIRE_EQUAL(ptr2->data, 1); - ptr2->data = 5; - BOOST_REQUIRE_EQUAL(ptr->data, 5); - } - BOOST_REQUIRE_EQUAL(ptr2->data, 5); -} - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/Engine/EngineTest.h b/test/Engine/EngineTest.h deleted file mode 100644 index df9cb74..0000000 --- a/test/Engine/EngineTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -namespace Seele -{ - struct GlobalFixture - { - GlobalFixture() - { - _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); - } - ~GlobalFixture(); - void setup() - { - } - void teardown() - { - } - }; -}; \ No newline at end of file diff --git a/test/Engine/Graphics/GraphicsResources.cpp b/test/Engine/Graphics/GraphicsResources.cpp deleted file mode 100644 index 04c5d8a..0000000 --- a/test/Engine/Graphics/GraphicsResources.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "EngineTest.h" -#include "Graphics/Vulkan/VulkanGraphics.h" -#include - -using namespace Seele; - -BOOST_AUTO_TEST_SUITE(Vulkan) - -BOOST_AUTO_TEST_CASE(init) -{ -} - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/Engine/Math/CMakeLists.txt b/test/Engine/Math/CMakeLists.txt deleted file mode 100644 index 1b16358..0000000 --- a/test/Engine/Math/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -target_sources(Seele_unit_tests - PRIVATE - ../../../src/Engine/Math/Vector.cpp - Vector.cpp) \ No newline at end of file diff --git a/test/Engine/Math/Vector.cpp b/test/Engine/Math/Vector.cpp deleted file mode 100644 index 9f7fb01..0000000 --- a/test/Engine/Math/Vector.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "EngineTest.h" -#include "Math/Vector.h" -#include - -using namespace Seele; -using namespace Seele::Math; - -BOOST_AUTO_TEST_SUITE(VectorParse) - -BOOST_AUTO_TEST_CASE(parse_vector_basic) -{ - const char* str = "float3(0, 0, 0)"; - Vector vec = parseVector(str); - BOOST_CHECK_EQUAL(vec.x, 0); - BOOST_CHECK_EQUAL(vec.y, 0); - BOOST_CHECK_EQUAL(vec.z, 0); -} - -BOOST_AUTO_TEST_CASE(parse_vector_literal) -{ - const char* str = "float3(0.0f, 0.0f, 0.0f)"; - Vector vec = parseVector(str); - BOOST_CHECK_EQUAL(vec.x, 0); - BOOST_CHECK_EQUAL(vec.y, 0); - BOOST_CHECK_EQUAL(vec.z, 0); -} - -BOOST_AUTO_TEST_CASE(parse_vector_no_literal) -{ - const char* str = "float3(0.0, 0.0, 0.0)"; - Vector vec = parseVector(str); - BOOST_CHECK_EQUAL(vec.x, 0); - BOOST_CHECK_EQUAL(vec.y, 0); - BOOST_CHECK_EQUAL(vec.z, 0); -} - -BOOST_AUTO_TEST_CASE(parse_vector_float) -{ - const char* str = "float3(0.01, 0.01f, 0.01f)"; - Vector vec = parseVector(str); - BOOST_CHECK_EQUAL(vec.x, 0.01f); - BOOST_CHECK_EQUAL(vec.y, 0.01f); - BOOST_CHECK_EQUAL(vec.z, 0.01f); -} - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/Engine/Serialization/ArchiveBuffer.cpp b/test/Engine/Serialization/ArchiveBuffer.cpp deleted file mode 100644 index 4f7f2db..0000000 --- a/test/Engine/Serialization/ArchiveBuffer.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "EngineTest.h" -#include - -using namespace Seele; - -BOOST_AUTO_TEST_SUITE(SaveLoadKtx) - -BOOST_AUTO_TEST_CASE(empty_constructur) -{ -} \ No newline at end of file diff --git a/test/Engine/Serialization/CMakeLists.txt b/test/Engine/Serialization/CMakeLists.txt deleted file mode 100644 index 3bc4f3c..0000000 --- a/test/Engine/Serialization/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -target_sources(Seele_unit_tests - PRIVATE - ArchiveBuffer.cpp) \ No newline at end of file diff --git a/test/Engine/ThreadPool.cpp b/test/Engine/ThreadPool.cpp deleted file mode 100644 index 40f2f04..0000000 --- a/test/Engine/ThreadPool.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include "EngineTest.h" -#include "ThreadPool.h" -#include -#include - -using namespace std::chrono_literals; - -BOOST_AUTO_TEST_SUITE(ThreadPool) - -/* -uint64 basicAwaitState = 0; - -Job basicAwaitFirst() -{ - BOOST_REQUIRE_EQUAL(basicAwaitState, 5); - std::this_thread::sleep_for(500ms); - basicAwaitState = 10; - co_return; -} -Job basicAwaitSecond() -{ - BOOST_REQUIRE_EQUAL(basicAwaitState, 15); - std::this_thread::sleep_for(500ms); - basicAwaitState = 20; - co_return; -} -Job basicAwaitThird() -{ - BOOST_REQUIRE_EQUAL(basicAwaitState, 25); - std::this_thread::sleep_for(500ms); - basicAwaitState = 30; - co_return; -} - -Job basicAwaitBase() -{ - basicAwaitState = 5; - co_await basicAwaitFirst(); - BOOST_REQUIRE_EQUAL(basicAwaitState, 10); - basicAwaitState = 15; - co_await basicAwaitSecond(); - BOOST_REQUIRE_EQUAL(basicAwaitState, 20); - basicAwaitState = 25; - co_await basicAwaitThird(); - BOOST_REQUIRE_EQUAL(basicAwaitState, 30); - co_return; -} - -BOOST_AUTO_TEST_CASE(basic_coawait) -{ - basicAwaitBase(); -} - -uint64 basicThenState = 5; - -Job basicThenFirst() -{ - BOOST_REQUIRE_EQUAL(basicThenState, 5); - std::this_thread::sleep_for(500ms); - basicThenState = 10; - co_return; -} -Job basicThenSecond() -{ - BOOST_REQUIRE_EQUAL(basicThenState, 15); - std::this_thread::sleep_for(500ms); - basicThenState = 20; - co_return; -} -Job basicThenThird() -{ - BOOST_REQUIRE_EQUAL(basicThenState, 25); - std::this_thread::sleep_for(500ms); - basicThenState = 30; - co_return; -} - -BOOST_AUTO_TEST_CASE(basic_thenchain) -{ - basicThenFirst() - .then([]() -> Job - { - BOOST_REQUIRE_EQUAL(basicThenState, 10); - std::this_thread::sleep_for(500ms); - basicThenState = 15; - co_return; - }) - .then(basicThenSecond()) - .then([]() -> Job - { - BOOST_REQUIRE_EQUAL(basicThenState, 20); - std::this_thread::sleep_for(500ms); - basicThenState = 25; - co_return; - }) - .then(basicThenThird()) - .then([]() -> Job - { - BOOST_REQUIRE_EQUAL(basicThenState, 30); - co_return; - }); -} -*/ -uint64 basicAllState1 = 0; -uint64 basicAllState2 = 0; -Job basicAllFirst() -{ - std::this_thread::sleep_for(500ms); - basicAllState1 = 10; - co_return; -} -Job basicAllSecond() -{ - std::this_thread::sleep_for(500ms); - basicAllState2 = 10; - co_return; -} -Job basicAllThen() -{ - BOOST_REQUIRE_EQUAL(basicAllState1, 10); - BOOST_REQUIRE_EQUAL(basicAllState2, 10); - co_return; -} - -BOOST_AUTO_TEST_CASE(basic_all) -{ - Job allJob = Job::all(basicAllFirst(), basicAllSecond()); - allJob.then(basicAllThen()); -} -/* -uint64 allThenState = 0; - -Job allThenInitial() -{ - std::this_thread::sleep_for(500ms); - allThenState = 10; - co_return; -} - -Job allThenIntermediate() -{ - std::this_thread::sleep_for(500ms); - BOOST_REQUIRE_EQUAL(allThenState, 10); - allThenState = 20; - co_return; -} - -Job allThenFinal() -{ - BOOST_REQUIRE_EQUAL(allThenState, 20); - co_return; -} - -BOOST_AUTO_TEST_CASE(all_then_interaction) -{ - Job::all(allThenInitial()) - .then(allThenIntermediate()) - .then(Job::all(allThenFinal())); -} - -uint64 basicCallable = 0; - -Job basicCallableFunc() -{ - std::this_thread::sleep_for(500ms); - basicCallable = 10; - co_return; -} - -BOOST_AUTO_TEST_CASE(basic_callable) -{ - basicCallableFunc() - .then([=]() -> Job - { - BOOST_REQUIRE_EQUAL(basicCallable, 10); - co_return; - }); -} - -struct Payload -{ - StaticArray data; - uint64 result = 0; -}; - -Job worker(Payload& payload) -{ - for(uint32 i = 0; i < 100000; ++i) - { - payload.result = std::accumulate(payload.data.begin(), payload.data.end(), (uint64)1, std::multiplies()); - } - co_return; -} - -Job launchStressTest() -{ - Array payloads(100); - Array jobs; - for(auto& payload : payloads) - { - for(uint64 i = 0; i < payload.data.size(); ++i) - { - payload.data[i] = i; - } - } - auto startTime = std::chrono::high_resolution_clock::now(); - std::cout << "Starting up" << std::endl; - co_await Job::launchJobs(&worker, payloads); - std::cout << "Finished waiting" << std::endl; - auto endTime = std::chrono::high_resolution_clock::now(); - float delta = std::chrono::duration_cast(endTime - startTime).count(); - std::cout << "Update took " << delta << " seconds"; -} - -BOOST_AUTO_TEST_CASE(stress_test) -{ - launchStressTest(); -}*/ - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..c907993 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(GTest CONFIG REQUIRED) + +add_executable(SeeleUnitTests "") + +add_subdirectory(Engine/) + +target_link_libraries(SeeleUnitTests PRIVATE Engine) +target_link_libraries(SeeleUnitTests PRIVATE GTest::gtest_main) + +enable_testing() +add_test(SeeleEngineTest SeeleUnitTests) + +include(GoogleTest) +gtest_discover_tests(SeeleUnitTests) \ No newline at end of file diff --git a/tests/Engine/CMakeLists.txt b/tests/Engine/CMakeLists.txt new file mode 100644 index 0000000..5639dba --- /dev/null +++ b/tests/Engine/CMakeLists.txt @@ -0,0 +1,10 @@ +target_sources(SeeleUnitTests + PRIVATE + EngineTest.h + ThreadPool.cpp) + +target_include_directories(SeeleUnitTests PUBLIC ./) +add_subdirectory(Containers/) +add_subdirectory(Graphics/) +add_subdirectory(Math/) +add_subdirectory(Serialization/) \ No newline at end of file diff --git a/tests/Engine/Containers/Array.cpp b/tests/Engine/Containers/Array.cpp new file mode 100644 index 0000000..9512bcf --- /dev/null +++ b/tests/Engine/Containers/Array.cpp @@ -0,0 +1,195 @@ +#include "EngineTest.h" +#include "Containers/Array.h" +#include "MinimalEngine.h" +#include +#include +#include + +using namespace Seele; + +TEST(ArraySuite, empty_constructur) +{ + Array array; + ASSERT_EQ(array.size(), 0); +} + +TEST(ArraySuite, initialial_size) +{ + Array array(3); + ASSERT_EQ(array.size(), 3); +} + +TEST(ArraySuite, resize) +{ + Array array; + array.add(2); + array.add(3); + ASSERT_EQ(array.size(), 2); + array.resize(5); + ASSERT_EQ(array.size(), 5); +} +TEST(ArraySuite, clear) +{ + Array array; + array.add(3); + array.add(2); + array.add(6); + ASSERT_EQ(array.size(), 3); + array.clear(); + ASSERT_EQ(array.size(), 0); + array.add(2); +} +TEST(ArraySuite, remove_keeporder) +{ + Array array; + array.add(1); + array.add(2); + array.add(3); + array.add(4); + array.add(5); + array.remove(1); + ASSERT_EQ(array[1], 3); + ASSERT_EQ(array[2], 4); + ASSERT_EQ(array[3], 5); + ASSERT_EQ(array.size(), 4); +} + +TEST(ArraySuite, find) +{ + Array array; + for(uint8 i = 0; i < 100; ++i) + { + array.add(i); + } + auto it = array.find([](uint8 elem){ return elem / 9 == 9; }); + ASSERT_EQ(*it, 81); +} + +TEST(ArraySuite, remove_swap) +{ + Array array; + array.add(1); + array.add(2); + array.add(3); + array.add(4); + array.add(5); + array.removeAt(1, false); + ASSERT_EQ(array[1], 5); + ASSERT_EQ(array[2], 3); + ASSERT_EQ(array[3], 4); + ASSERT_EQ(array.size(), 4); +} + +TEST(ArraySuite, remove_iterator) +{ + Array array; + array.add(1); + array.add(2); + array.add(3); + array.add(4); + array.add(5); + array.remove(array.find(1), false); + ASSERT_EQ(array[0], 5); + ASSERT_EQ(array[1], 2); + ASSERT_EQ(array[2], 3); + ASSERT_EQ(array[3], 4); + ASSERT_EQ(array.size(), 4); +} + +TEST(ArraySuite, remove_iterator_keep_order) +{ + Array array; + array.add(1); + array.add(2); + array.add(3); + array.add(4); + array.add(5); + array.remove(array.find(1)); + ASSERT_EQ(array[0], 2); + ASSERT_EQ(array[1], 3); + ASSERT_EQ(array[2], 4); + ASSERT_EQ(array[3], 5); + ASSERT_EQ(array.size(), 4); +} + +TEST(ArraySuite, random_access) +{ + Array array; + array.add(4); + array.add(5); + array.add(6); + ASSERT_EQ(array[2], 6); + ASSERT_EQ(array[0], 4); +} + +TEST(ArraySuite, copy) +{ + Array array; + array.add(0); + array.add(1); + array.add(2); + array.add(3); + Array copy = array; + ASSERT_EQ(array, copy); + Array copy2(copy); + ASSERT_EQ(array, copy2); +} + +class BaseElement +{ + uint64 test; +}; + +class BaseContainer +{ + Array elements; +}; + +class DerivedContainer : public BaseContainer +{ + uint64 test; +}; + +TEST(ArraySuite, virtual_classes) +{ + Array array(64); + for(uint32 i = 0; i < 100; ++i) + { + Array copy = array; + ASSERT_EQ(array.size(), copy.size()); + } +} + +class TestStruct +{ +public: + uint32 data; + ~TestStruct() + { + data = 1; + } +}; +DECLARE_REF(TestStruct); + +TEST(StaticArraySuite, empty_constructur) +{ + StaticArray array; + ASSERT_EQ(array.size(), 2); +} + +TEST(StaticArraySuite, initialial_size) +{ + StaticArray array(3); + ASSERT_EQ(array.size(), 3); + ASSERT_EQ(array[1], 3); +} + +TEST(StaticArraySuite, random_access) +{ + StaticArray array; + array[0] = 4; + array[1] = 5; + array[2] = 6; + ASSERT_EQ(array[0], 4); + ASSERT_EQ(array[2], 6); +} \ No newline at end of file diff --git a/test/Engine/Containers/CMakeLists.txt b/tests/Engine/Containers/CMakeLists.txt similarity index 56% rename from test/Engine/Containers/CMakeLists.txt rename to tests/Engine/Containers/CMakeLists.txt index 6971eb0..69b4de0 100644 --- a/test/Engine/Containers/CMakeLists.txt +++ b/tests/Engine/Containers/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(Seele_unit_tests +target_sources(SeeleUnitTests PRIVATE Array.cpp Map.cpp diff --git a/test/Engine/Containers/List.cpp b/tests/Engine/Containers/List.cpp similarity index 50% rename from test/Engine/Containers/List.cpp rename to tests/Engine/Containers/List.cpp index 0088420..2d17c4d 100644 --- a/test/Engine/Containers/List.cpp +++ b/tests/Engine/Containers/List.cpp @@ -1,23 +1,20 @@ #include "EngineTest.h" #include "Containers/List.h" -#include using namespace Seele; -BOOST_AUTO_TEST_SUITE(List_Suite) - -BOOST_AUTO_TEST_CASE(basic_add) +TEST(ListSuite, basic_add) { List list; list.add(2); - BOOST_REQUIRE_EQUAL(list.size(), 1); + ASSERT_EQ(list.size(), 1); list.add(4); - BOOST_REQUIRE_EQUAL(list.size(), 2); + ASSERT_EQ(list.size(), 2); List::Iterator it = list.find(2); - BOOST_REQUIRE_EQUAL(*it, 2); + ASSERT_EQ(*it, 2); } -BOOST_AUTO_TEST_CASE(basic_insert) +TEST(ListSuite, basic_insert) { List list; list.add(2); @@ -25,10 +22,10 @@ BOOST_AUTO_TEST_CASE(basic_insert) list.add(4); List::Iterator it = list.find(3); it = list.insert(it, 1); - BOOST_REQUIRE_EQUAL(*it, 1); - BOOST_REQUIRE_EQUAL(list.size(), 4); + ASSERT_EQ(*it, 1); + ASSERT_EQ(list.size(), 4); } -BOOST_AUTO_TEST_CASE(basic_remove) +TEST(ListSuite, basic_remove) { List list; list.add(2); @@ -36,11 +33,11 @@ BOOST_AUTO_TEST_CASE(basic_remove) list.add(4); List::Iterator it = list.find(3); it = list.remove(it); - BOOST_REQUIRE_EQUAL(*it, 4); - BOOST_REQUIRE_EQUAL(list.size(), 2); + ASSERT_EQ(*it, 4); + ASSERT_EQ(list.size(), 2); } -BOOST_AUTO_TEST_CASE(list_join) +TEST(ListSuite, list_join) { List list1; List list2; @@ -53,9 +50,7 @@ BOOST_AUTO_TEST_CASE(list_join) list2.add(8); list2.add(6); list2.add(9); - list1.moveElements(list2); - BOOST_REQUIRE_EQUAL(list1.size(), 9); - BOOST_REQUIRE_EQUAL(list2.size(), 0); + list1.moveElements(std::move(list2)); + ASSERT_EQ(list1.size(), 9); + ASSERT_EQ(list2.size(), 0); } - -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/tests/Engine/Containers/Map.cpp b/tests/Engine/Containers/Map.cpp new file mode 100644 index 0000000..ab070fc --- /dev/null +++ b/tests/Engine/Containers/Map.cpp @@ -0,0 +1,91 @@ +#include "EngineTest.h" +#include "Containers/Map.h" + +using namespace Seele; + +TEST(CachedMap, insert_find_basic) +{ + Map map; + map[2] = 3; + map[1] = 5; + map[6] = 4; + map[4] = 7; + ASSERT_EQ(map[2], 3); + ASSERT_EQ(map[1], 5); + ASSERT_EQ(map[6], 4); + ASSERT_EQ(map[4], 7); + map[2] = 5; + map[4] = 4; + ASSERT_EQ(map[2], 5); + ASSERT_EQ(map[4], 4); + ASSERT_EQ(map.find(2)->value, 5); + ASSERT_EQ(map.find(4)->value, 4); + //ASSERT_EQ(map.find(5), map.end()); +} + +TEST(CachedMap, for_each) +{ + Map map; + map[2] = 3; + map[1] = 5; + map[6] = 4; + map[4] = 7; + int count = 0; + for(auto it : map) + { + count++; + } + ASSERT_EQ(count, 4); +} + +TEST(CachedMap, remove_entry) +{ + Map map; + map[1] = 5; + map[4] = 1; + map[2] = 6; + map[3] = 4; + map.erase(2); + ASSERT_EQ(map.size(), 3); + ASSERT_EQ(map[1], 5); + ASSERT_EQ(map[4], 1); + ASSERT_EQ(map[3], 4); +} + +TEST(CachedMap, key_exists) +{ + Map map; + map[2] = 3; + ASSERT_EQ(map.exists(2), true); + ASSERT_EQ(map.exists(4), false); + map.erase(2); + ASSERT_EQ(map.exists(2), false); +} + +TEST(CachedMap, custom_key) +{ + struct Key + { + int id; + bool operator<(const Key& other) const + { + return id < other.id; + } + }; + Map map; + map[Key{ 2 }] = 3; + map[Key{ 3 }] = 4; + ASSERT_EQ(map[Key{ 2 }], 3); + ASSERT_EQ(map[Key{ 3 }], 4); + +} + +TEST(CachedMap, string_key) +{ + std::map map; + map["Test"] = 2; + map["Test2"] = 3; + ASSERT_EQ(map["Test"], 2); + ASSERT_EQ(map["Test2"], 3); +} + diff --git a/tests/Engine/EngineTest.h b/tests/Engine/EngineTest.h new file mode 100644 index 0000000..42e5303 --- /dev/null +++ b/tests/Engine/EngineTest.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include "MinimalEngine.h" + +using namespace Seele; \ No newline at end of file diff --git a/test/Engine/Graphics/CMakeLists.txt b/tests/Engine/Graphics/CMakeLists.txt similarity index 52% rename from test/Engine/Graphics/CMakeLists.txt rename to tests/Engine/Graphics/CMakeLists.txt index 53c062b..3a0ae99 100644 --- a/test/Engine/Graphics/CMakeLists.txt +++ b/tests/Engine/Graphics/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(Seele_unit_tests +target_sources(SeeleUnitTests PRIVATE GraphicsResources.cpp) \ No newline at end of file diff --git a/tests/Engine/Graphics/GraphicsResources.cpp b/tests/Engine/Graphics/GraphicsResources.cpp new file mode 100644 index 0000000..1d6575b --- /dev/null +++ b/tests/Engine/Graphics/GraphicsResources.cpp @@ -0,0 +1,2 @@ +#include "EngineTest.h" +#include "Graphics/Vulkan/Graphics.h" diff --git a/tests/Engine/Math/CMakeLists.txt b/tests/Engine/Math/CMakeLists.txt new file mode 100644 index 0000000..101ae35 --- /dev/null +++ b/tests/Engine/Math/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(SeeleUnitTests + PRIVATE + Vector.cpp) \ No newline at end of file diff --git a/tests/Engine/Math/Vector.cpp b/tests/Engine/Math/Vector.cpp new file mode 100644 index 0000000..ced28b1 --- /dev/null +++ b/tests/Engine/Math/Vector.cpp @@ -0,0 +1,38 @@ +#include "EngineTest.h" +#include "Math/Vector.h" + +TEST(VectorParse, parse_vector_basic) +{ + const char* str = "float3(0, 0, 0)"; + Vector vec = parseVector(str); + ASSERT_EQ(vec.x, 0); + ASSERT_EQ(vec.y, 0); + ASSERT_EQ(vec.z, 0); +} + +TEST(VectorParse, parse_vector_literal) +{ + const char* str = "float3(0.0f, 0.0f, 0.0f)"; + Vector vec = parseVector(str); + ASSERT_EQ(vec.x, 0); + ASSERT_EQ(vec.y, 0); + ASSERT_EQ(vec.z, 0); +} + +TEST(VectorParse, parse_vector_no_literal) +{ + const char* str = "float3(0.0, 0.0, 0.0)"; + Vector vec = parseVector(str); + ASSERT_EQ(vec.x, 0); + ASSERT_EQ(vec.y, 0); + ASSERT_EQ(vec.z, 0); +} + +TEST(VectorParse, parse_vector_float) +{ + const char* str = "float3(0.01, 0.01f, 0.01f)"; + Vector vec = parseVector(str); + ASSERT_EQ(vec.x, 0.01f); + ASSERT_EQ(vec.y, 0.01f); + ASSERT_EQ(vec.z, 0.01f); +} diff --git a/tests/Engine/Serialization/ArchiveBuffer.cpp b/tests/Engine/Serialization/ArchiveBuffer.cpp new file mode 100644 index 0000000..84e5b37 --- /dev/null +++ b/tests/Engine/Serialization/ArchiveBuffer.cpp @@ -0,0 +1 @@ +#include "EngineTest.h" diff --git a/tests/Engine/Serialization/CMakeLists.txt b/tests/Engine/Serialization/CMakeLists.txt new file mode 100644 index 0000000..d6620d8 --- /dev/null +++ b/tests/Engine/Serialization/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(SeeleUnitTests + PRIVATE + ArchiveBuffer.cpp) \ No newline at end of file diff --git a/tests/Engine/ThreadPool.cpp b/tests/Engine/ThreadPool.cpp new file mode 100644 index 0000000..2d7792f --- /dev/null +++ b/tests/Engine/ThreadPool.cpp @@ -0,0 +1,22 @@ +#include "EngineTest.h" +#include "ThreadPool.h" +#include + +TEST(ThreadPool, RunBatch) +{ + ThreadPool t(10); + uint32 test = 20; + std::mutex m; + List work; + for (uint32 i = 0; i < 400; ++i) + { + work.add([&]() -> Task { + std::unique_lock l(m); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + test++; + co_return; + }()); + } + t.runAndWait(std::move(work)); + ASSERT_EQ(test, 420); +} \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json index 4664e2b..3790b0f 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -9,6 +9,7 @@ "glm", "ktx", "nlohmann-json", - "shader-slang" + "shader-slang", + "gtest" ] } \ No newline at end of file