Skip to content

Commit

Permalink
Add hand strengths tool unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzzuti committed May 10, 2024
1 parent 9d13cbc commit 52b39b8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
32 changes: 32 additions & 0 deletions tests/unittests/thandutils_unittest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <gtest/gtest.h>

#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<Card, Card> cards = {Card{r1, s1}, Card{r2, s2}};
std::pair<Card, Card> cards2 = {Card{r2, s2}, Card{r1, s1}};
std::pair<Card, Card> cards3 = {Card{r1, s2}, Card{r2, s1}};
std::pair<Card, Card> 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);
}
2 changes: 1 addition & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

Expand Down
2 changes: 1 addition & 1 deletion tools/hand_strengths/hand_utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "hand_strengths.h"
// TODO: unittests

class HandUtils {
public:
/// @brief Constructor clears all arrays
Expand Down

0 comments on commit 52b39b8

Please sign in to comment.