Skip to content
Snippets Groups Projects
Commit 0ad658fa authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

add unit tests for hash functions

parent 5838b0e8
No related branches found
No related tags found
No related merge requests found
......@@ -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 )
......
......@@ -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];
......
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)
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
)
......
/*
* 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 );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment