Files
uno-game/test/unit/game/GameStateTest.cpp
Kieran Kihn c6342469df test(game): remove player name from ClientGameState constructor in test
- Simplified `ClientGameState` initialization in `GameStateTest`.
2025-12-09 20:03:37 +08:00

159 lines
6.3 KiB
C++

/**
* @file GameStateTest.cpp
*
* @author Yuzhe Guo
* @date 2025.11.16
*/
#include "../../../src/game/GameState.h"
#include <gtest/gtest.h>
TEST(game_state_test, game_state_test_1)
{
UNO::GAME::ClientGameState clientGameState;
UNO::GAME::ClientPlayerState playerState1("pkq", 100, false);
UNO::GAME::ClientPlayerState playerState2("kpq", 100, false);
UNO::GAME::ClientPlayerState playerState3("qkp", 100, false);
UNO::GAME::ClientPlayerState playerState4("lzh", 100, false);
clientGameState.init({playerState1, playerState2, playerState3, playerState4}, {}, {}, 0, 3);
const auto &players = clientGameState.getPlayers();
ASSERT_EQ(players[0].getName(), "pkq");
ASSERT_EQ(players[1].getName(), "kpq");
ASSERT_EQ(players[2].getName(), "qkp");
ASSERT_EQ(players[3].getName(), "lzh");
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "pkq");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "kpq");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "qkp");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "lzh");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "pkq");
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::REVERSE));
ASSERT_EQ(clientGameState.getIsReversed(), true);
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "lzh");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "qkp");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "kpq");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "pkq");
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "lzh");
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::SKIP));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "kpq");
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::DRAW2));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "pkq");
ASSERT_EQ(clientGameState.getDrawCount(), 2);
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::DRAW2));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "lzh");
ASSERT_EQ(clientGameState.getDrawCount(), 4);
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::WILDDRAWFOUR));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "qkp");
ASSERT_EQ(clientGameState.getDrawCount(), 8);
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::WILDDRAWFOUR));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "kpq");
ASSERT_EQ(clientGameState.getDrawCount(), 12);
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::WILDDRAWFOUR));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "pkq");
ASSERT_EQ(clientGameState.getDrawCount(), 16);
clientGameState.updateStateByCard(UNO::GAME::Card(UNO::GAME::CardColor::BLUE, UNO::GAME::CardType::WILDDRAWFOUR));
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "lzh");
ASSERT_EQ(clientGameState.getDrawCount(), 20);
clientGameState.updateStateByDraw();
ASSERT_EQ(clientGameState.getPlayers()[3].getName(), "lzh");
ASSERT_EQ(clientGameState.getPlayers()[3].getRemainingCardCount(), 120);
ASSERT_EQ(clientGameState.getCurrentPlayer()->getName(), "qkp");
ASSERT_EQ(clientGameState.getDrawCount(), 0);
}
TEST(game_state_test, game_state_test_2)
{
UNO::GAME::ServerGameState serverGameState;
serverGameState.addPlayer(UNO::GAME::ServerPlayerState("pkq1", 0, false));
serverGameState.addPlayer(UNO::GAME::ServerPlayerState("pkq2", 0, false));
serverGameState.addPlayer(UNO::GAME::ServerPlayerState("pkq3", 0, false));
serverGameState.addPlayer(UNO::GAME::ServerPlayerState("lzh", 0, false));
serverGameState.init();
for (const auto &i : serverGameState.getPlayers()) {
ASSERT_EQ(i.getRemainingCardCount(), 7);
}
while (true) {
for (auto it = serverGameState.getCurrentPlayer()->getCards().begin();; it++) {
const auto player = serverGameState.getCurrentPlayer();
auto prevCards = player->getCards();
if (it == serverGameState.getCurrentPlayer()->getCards().end()) {
size_t prevCount = player->getRemainingCardCount();
size_t drawCount = serverGameState.getDrawCount();
if (drawCount == 0) {
drawCount = 1;
}
serverGameState.updateStateByDraw();
size_t afterCount = player->getRemainingCardCount();
ASSERT_EQ(prevCount + drawCount, afterCount);
for (auto card : prevCards) {
ASSERT_LE(prevCards.count(card), player->getCards().count(card));
}
break;
}
if (it->canBePlayedOn(serverGameState.getDiscardPile().getFront(), serverGameState.getDrawCount())) {
auto card = *it;
size_t prevCount = player->getCards().count(card);
serverGameState.updateStateByCard(card);
size_t afterCount = player->getCards().count(card);
ASSERT_EQ(prevCount - 1, afterCount);
ASSERT_EQ(prevCards.size() - 1, player->getCards().size());
for (auto i : player->getCards()) {
if (i.getType() != card.getType() || i.getColor() != card.getColor()) {
ASSERT_EQ(player->getCards().count(i), prevCards.count(i));
}
}
break;
}
}
ASSERT_EQ(serverGameState.getCurrentPlayer()->getRemainingCardCount(), serverGameState.getCurrentPlayer()->getCards().size());
if (serverGameState.getCurrentPlayer()->getRemainingCardCount() == 0) {
break;
}
}
}