diff --git a/CMakeLists.txt b/CMakeLists.txt index f1687da0aeaffa907b459d4280e944b3d49392d9..b130c84102eb591a7ff16272863cfd837a91ea50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES) option(FGGL_CONAN "Should we use conan to find missing dependencies?" OFF) option(FGGL_EXAMPLES "Should we build examples or just the library" ON) +option(FGGL_TESTS "Should we enable the testing suite?" ON) option(FGGL_DOCS "Should we build documentation?" ON) set(CONAN_BUILD_TYPE "Debug") @@ -68,7 +69,10 @@ target_compile_options( ${PROJECT_NAME} PRIVATE -Wall -Wpedantic -Wextra -Wodr - set_property(TARGET fggl PROPERTY INTERPROCEDURAL_OPTIMIZATION True) # extras -#add_subdirectory(tests) +if (FGGL_TESTS) + add_subdirectory(tests) +endif() + if (FGGL_EXAMPLES) add_subdirectory(demo) target_compile_options( demo PRIVATE -Wall -Wextra -Wodr -Wdouble-promotion -fno-strict-aliasing -fno-strict-overflow ) diff --git a/include/fggl/util/guid.hpp b/include/fggl/util/guid.hpp index 1c66d8d6e6228a51d2f0b2eaf89809271ea9096a..a4ac1e805c920998a607d0a643f44e520f9870f1 100644 --- a/include/fggl/util/guid.hpp +++ b/include/fggl/util/guid.hpp @@ -14,6 +14,7 @@ // // Created by webpigeon on 23/07/22. +// See http://www.isthe.com/chongo/tech/comp/fnv/ // #ifndef FGGL_UTIL_GUID_HPP @@ -38,6 +39,7 @@ namespace fggl::util { * @return the hashed value */ constexpr uint32_t hash_fnv1a_32(const char* str) { + assert(str != nullptr); uint32_t hash = FNV_OFFSET_BASIS_32; for (int i = 0; str[i] != '\0'; i++) { hash = hash ^ str[i]; @@ -53,6 +55,7 @@ namespace fggl::util { * @return the hashed value */ constexpr uint64_t hash_fnv1a_64(const char* str) { + assert(str != nullptr); uint64_t hash = FNV_OFFSET_BASIS_64; for (int i = 0; str[i] != '\0'; i++) { hash = hash ^ str[i]; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2ea02a987b482d0f4443aa7864ff6980e7e30d0b..7cf6ffe62648435299b1a71d628937b251a866f9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,16 +1,17 @@ find_package(Threads REQUIRED) # GTest Dependency -find_package(GTest) +find_package( GTest REQUIRED ) if ( NOT GTest_FOUND ) -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip -) -# For Windows: Prevent overriding the parent project's compiler/linker settings -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(googletest) + message(NOTICE "GTest not found, installing...") + include(FetchContent) + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip + ) + # For Windows: Prevent overriding the parent project's compiler/linker settings + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(googletest) endif () add_subdirectory(testfggl) diff --git a/tests/testfggl/CMakeLists.txt b/tests/testfggl/CMakeLists.txt index 284b79ccfeaeadc652fc0e0993370b75e0ad0869..13fa77df7212bcac817d883a71ef23e4ae993cb7 100644 --- a/tests/testfggl/CMakeLists.txt +++ b/tests/testfggl/CMakeLists.txt @@ -1,11 +1,12 @@ enable_testing() add_executable( fggl_test - # TestFggl.cpp - ecs/ecs.cpp - ecs3/ecs.cpp - math/types.cpp - ecs3/easing.cpp + TestFggl.cpp +# ecs/ecs.cpp +# ecs3/ecs.cpp +# ecs3/easing.cpp +# math/types.cpp + util/guid.cpp ) target_include_directories(fggl_test PUBLIC @@ -14,6 +15,7 @@ target_include_directories(fggl_test target_link_libraries(fggl_test fggl + gtest gtest_main gmock ) diff --git a/tests/testfggl/util/guid.cpp b/tests/testfggl/util/guid.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d2baa97f9c69a423a135bf8a6a68b3145254b11 --- /dev/null +++ b/tests/testfggl/util/guid.cpp @@ -0,0 +1,85 @@ +/* + * This file is part of FGGL. + * + * FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with FGGL. + * If not, see <https://www.gnu.org/licenses/>. + */ + +// +// Created by webpigeon on 23/07/22. +// + +#include <gtest/gtest.h> +#include <gmock/gmock.h> + +#include "fggl/util/guid.hpp" + +namespace { + // expect blank (0-width) strings to be the offset basis + TEST(UtilHash32, Empty) { + auto value = fggl::util::hash_fnv1a_32(""); + EXPECT_EQ( fggl::util::FNV_OFFSET_BASIS_32, value ); + } + + TEST(UtilHash64, Empty) { + auto value = fggl::util::hash_fnv1a_64(""); + EXPECT_EQ( fggl::util::FNV_OFFSET_BASIS_64, value ); + } + + // expect single characters to work correctly + TEST(UtilHash32, SingleChar) { + auto value = fggl::util::hash_fnv1a_32("a"); + EXPECT_EQ( 0xe40c292c, value ); + } + + TEST(UtilHash64, SingleChar) { + auto value = fggl::util::hash_fnv1a_64("a"); + EXPECT_EQ( 0xaf63dc4c8601ec8c, value ); + } + + // expect hello world to known values + + TEST(UtilHash32, HelloWorld) { + auto value = fggl::util::hash_fnv1a_32("Hello World"); + EXPECT_EQ( 0xb3902527, value ); + } + + TEST(UtilHash64, HelloWorld) { + auto value = fggl::util::hash_fnv1a_64("Hello World"); + EXPECT_EQ( 0x3d58dee72d4e0c27, value ); + } + + // expect scoped (::) notation to work correctly + + TEST(UtilHash32, Scoped) { + auto value = fggl::util::hash_fnv1a_32("fggl::test::scoped"); + EXPECT_EQ( 0x27fd4589, value ); + } + + TEST(UtilHash64, Scoped) { + auto value = fggl::util::hash_fnv1a_64("fggl::test::scoped"); + EXPECT_EQ( 0xd2929655f3b0cf49, value ); + } + + // sanity checks + + TEST(UtilHash32, RepeatsAreEqual) { + auto value = fggl::util::hash_fnv1a_32("fggl::test::scoped"); + auto value2 = fggl::util::hash_fnv1a_32("fggl::test::scoped"); + EXPECT_EQ( value, value2 ); + } + + TEST(UtilHash64, RepeatsAreEqual) { + auto value = fggl::util::hash_fnv1a_64("fggl::test::scoped"); + auto value2 = fggl::util::hash_fnv1a_64("fggl::test::scoped"); + EXPECT_EQ( value, value2 ); + } + +}