From 52b39b815e29be70539829d779a5a487f6feab1c Mon Sep 17 00:00:00 2001 From: Duzzuti Date: Fri, 10 May 2024 16:25:55 +0000 Subject: [PATCH] Add hand strengths tool unittest --- CMakeLists.txt | 1 + tests/unittests/CMakeLists.txt | 5 ++++ tests/unittests/thandutils_unittest.cpp | 32 +++++++++++++++++++++++++ tools/CMakeLists.txt | 2 +- tools/hand_strengths/hand_utils.h | 2 +- 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/unittests/thandutils_unittest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 79fa2d8..616c3fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) set(SRC_DIR ${PROJECT_SOURCE_DIR}/src) set(TEST_DIR ${PROJECT_SOURCE_DIR}/tests) set(TOOLS_DIR ${PROJECT_SOURCE_DIR}/tools) +set(THAND_STRENGTHS_DIR ${TOOLS_DIR}/hand_strengths) set(PLAYER_DIR ${SRC_DIR}/players) set(CHECK_PLAYER ${PLAYER_DIR}/check_player/check_player.cpp) set(RAND_PLAYER ${PLAYER_DIR}/rand_player/rand_player.cpp) diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 1861625..40ac1e8 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -33,6 +33,10 @@ add_executable(poker_test_gametest main_test.cpp gametest_unittest.cpp ${SRC_DIR target_link_libraries(poker_test_gametest gtest_main) target_include_directories(poker_test_gametest PUBLIC ${INCLUDE_DIR} ${PLAYER_DIR} ${TEST_DIR}) +add_executable(poker_test_thandstrengths main_test.cpp thandutils_unittest.cpp ${THAND_STRENGTHS_DIR}/hand_utils.cpp) +target_link_libraries(poker_test_thandstrengths gtest_main) +target_include_directories(poker_test_thandstrengths PUBLIC ${INCLUDE_DIR} ${THAND_STRENGTHS_DIR}) + add_executable(test main_test.cpp test_test.cpp) target_link_libraries(test gtest_main) target_include_directories(test PUBLIC ${INCLUDE_DIR}) @@ -43,4 +47,5 @@ add_test(POT_TEST poker_test_pot) add_test(UTILS_TEST poker_test_utils) add_test(CONST_TEST poker_test_const) add_test(GAME_TEST poker_test_gametest) +add_test(THANDSTRENGTHS_TEST poker_test_thandstrengths) add_test(TEST_TEST test) \ No newline at end of file diff --git a/tests/unittests/thandutils_unittest.cpp b/tests/unittests/thandutils_unittest.cpp new file mode 100644 index 0000000..741061b --- /dev/null +++ b/tests/unittests/thandutils_unittest.cpp @@ -0,0 +1,32 @@ +#include + +#include "hand_utils.h" + +TEST(THandUtils, handIndex) { + // iterates through all card combinations where r1 >= r2 and through all suit combinations + // checks if the index correct for all combinations and if the back-converted name is also correct + u_int8_t expectedInd = 0; + const std::string rankNames = "23456789TJQKA"; + for (u_int8_t r1 = 2; r1 < 15; r1++) { + for (u_int8_t r2 = 2; r2 <= r1; r2++) { + if (!(r1 == 2 && r2 == 2)) expectedInd++; + for (u_int8_t s1 = 0; s1 < 4; s1++) { + for (u_int8_t s2 = 0; s2 < 4; s2++) { + if (r1 == r2 && s1 == s2) continue; // skip same cards (impossible hand) + std::pair cards = {Card{r1, s1}, Card{r2, s2}}; + std::pair cards2 = {Card{r2, s2}, Card{r1, s1}}; + std::pair cards3 = {Card{r1, s2}, Card{r2, s1}}; + std::pair cards4 = {Card{r2, s1}, Card{r1, s2}}; + u_int8_t index = HandUtils::getHandIndex(cards); + EXPECT_EQ(index, HandUtils::getHandIndex(cards2)); + EXPECT_EQ(index, HandUtils::getHandIndex(cards3)); + EXPECT_EQ(index, HandUtils::getHandIndex(cards4)); + EXPECT_EQ(index, expectedInd); + std::string name = HandUtils::getHandName(index); + EXPECT_EQ(name, std::string() + rankNames[r1 - 2] + rankNames[r2 - 2]); + } + } + } + } + EXPECT_EQ(expectedInd, 90); +} \ No newline at end of file diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3fe2e47..6d4ffa7 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,5 +1,5 @@ # Add the executable target -add_executable(hand_strengths ${SRC_DIR}/deck.cpp hand_strengths/main.cpp hand_strengths/hand_utils.cpp) +add_executable(hand_strengths ${SRC_DIR}/deck.cpp ${THAND_STRENGTHS_DIR}/main.cpp ${THAND_STRENGTHS_DIR}/hand_utils.cpp) # Include headers target_include_directories(hand_strengths PUBLIC ${INCLUDE_DIR}) diff --git a/tools/hand_strengths/hand_utils.h b/tools/hand_strengths/hand_utils.h index f026dea..489b8cb 100644 --- a/tools/hand_strengths/hand_utils.h +++ b/tools/hand_strengths/hand_utils.h @@ -1,7 +1,7 @@ #pragma once #include "hand_strengths.h" -// TODO: unittests + class HandUtils { public: /// @brief Constructor clears all arrays