From 7c8aac19374013e7366b5afbf5d85fe151de3669 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 5 Feb 2023 01:18:18 +0100 Subject: Added simple test for HitPoints This should have been added earlier. The test itself is questionable and overall very optimistic about the edge cases. The intent of this commit is purely set up of the testing framework. --- CMakeLists.txt | 4 +++- README.md | 4 +++- sim/CMakeLists.txt | 10 ++++++++++ sim/tests/HitPoints.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 sim/tests/HitPoints.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae2a9b8..da56f8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,12 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.20) project(kurator) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_EXTENSIONS No) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -D_USE_MATH_DEFINES") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") +enable_testing() find_package(EnTT 3 REQUIRED) +find_package(GTest 1 REQUIRED) find_package(nlohmann_json 3 REQUIRED) find_package(raylib 4 REQUIRED) include(AddResources) diff --git a/README.md b/README.md index bb68061..2e931df 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,14 @@ Third-party libraries used: * EnTT + * GTest * nlohmann\_json * raylib Both Raylib and Nlohmann's JSON libraries are available in mainstream distribution repositories. Confirmed with Arch Linux and Ubuntu. In case they are not, you can install Raylib[1] and JSON[2] from source. EnTT is rarely available. For -Arch Linux you can use AUR package[3]. Otherwise, install from source git repository[4]. +Arch Linux you can use AUR package[3]. Otherwise, install from source git repository[4]. GTest is available in Arch, but +Ubuntu package repositories contain only sources. After making sure all dependencies are available, run CMake to generate and build: diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index ef80c77..4ddd15d 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -22,3 +22,13 @@ target_link_libraries( PRIVATE stats PUBLIC universe ) +add_executable( + ${PROJECT_NAME}_test + tests/HitPoints.cpp +) +target_link_libraries( + ${PROJECT_NAME}_test + PRIVATE GTest::gtest_main + PRIVATE ${PROJECT_NAME} +) +gtest_discover_tests(${PROJECT_NAME}_test) diff --git a/sim/tests/HitPoints.cpp b/sim/tests/HitPoints.cpp new file mode 100644 index 0000000..8b8ef84 --- /dev/null +++ b/sim/tests/HitPoints.cpp @@ -0,0 +1,44 @@ +#include + +#include +#include + + +namespace kurator +{ +namespace tests +{ + + +const universe::ShipType SHIP_TYPE { + "", + 100.0, + 100.0, + 100.0, + 0.0, + 0.5, + 0.2, + 0.5, +}; + + + +TEST(HitPoints, Deal) +{ + sim::HitPoints points {SHIP_TYPE}; + EXPECT_TRUE(points.is_alive()); + EXPECT_DOUBLE_EQ(300.0, points.total()); + EXPECT_DOUBLE_EQ(50.0, points.deal(100.0)); + EXPECT_DOUBLE_EQ(250.0, points.total()); + EXPECT_DOUBLE_EQ(50.0, points.deal(100.0)); + EXPECT_DOUBLE_EQ(200.0, points.total()); + EXPECT_DOUBLE_EQ(100.0, points.deal(125.0)); + EXPECT_DOUBLE_EQ(100.0, points.total()); + EXPECT_DOUBLE_EQ(100.0, points.deal(200.0)); + EXPECT_DOUBLE_EQ(0.0, points.total()); + EXPECT_FALSE(points.is_alive()); +} + + +} // namespace tests +} // namespace kurator -- cgit v1.1