Commit 90211755 authored by Jostein Bratlie's avatar Jostein Bratlie

Predefined unit-tests: set one

parent bc951650
#ifndef GAMEENGINE_H
#define GAMEENGINE_H
#include "interface/player_interface.h"
#include "interface/gameengine_interface.h"
#include "utility.h"
......
......@@ -43,8 +43,8 @@ namespace gameengine::utility
* \param[in] board_pos The board position where the next piece is to be
* placed
* \return Returns true if the move is legal */
bool isLegalMove(const BitBoard& board, const PlayerId&,
const BitPos& board_pos);
bool isLegalMove(const BitBoard& board, const PlayerId& player_id,
const BitPos& board_pos);
/*! Perform a move on a given board for current player id and valid board
* position
......
......@@ -8,7 +8,7 @@
// Testing fixture
struct PredefinedGameEngineTest : testing::Test {
std::unique_ptr<gameengine::GameEngine> engine;
gameengine::GameEngine engine;
PredefinedGameEngineTest();
~PredefinedGameEngineTest() override;
......@@ -28,8 +28,29 @@ PredefinedGameEngineTest::~PredefinedGameEngineTest()
TEST_F(PredefinedGameEngineTest, InitialCurrentPlayerIdIsOne)
{
engine->initPlayerType<gameengine::HumanPlayer, gameengine::PlayerId::One>();
engine->initPlayerType<gameengine::MonkeyAI, gameengine::PlayerId::Two>();
engine.initPlayerType<gameengine::HumanPlayer, gameengine::PlayerId::One>();
engine.initPlayerType<gameengine::MonkeyAI, gameengine::PlayerId::Two>();
EXPECT_EQ(engine->currentPlayerId(), gameengine::PlayerId::One);
EXPECT_EQ(engine.currentPlayerId(), gameengine::PlayerId::One);
}
TEST_F(PredefinedGameEngineTest, InitNewGameFail_OnlyPlayerOneInitialized)
{
engine.initPlayerType<gameengine::HumanPlayer, gameengine::PlayerId::One>();
auto state = engine.initNewGame();
EXPECT_EQ(state, false);
}
TEST_F(PredefinedGameEngineTest, InitNewGameSuccess_TwoHumans01)
{
engine.initPlayerType<gameengine::HumanPlayer, gameengine::PlayerId::One>();
engine.initPlayerType<gameengine::HumanPlayer, gameengine::PlayerId::Two>();
auto state = engine.initNewGame();
EXPECT_EQ(state, true);
}
......@@ -5,38 +5,217 @@
#include <engine/gameengine.h>
#include <engine/monkeyai.h>
using BitPos = gameengine::BitPos;
using MoveDir = gameengine::MoveDirection;
// stl
#include <variant>
using namespace gameengine;
//////////////////////
/// Occupied positions
/// Concatination
// Testing fixture
struct UtilityBoardTestParams {
BitPos pos;
MoveDir dir;
BitPos expected_next_pos;
struct OccupiedPositionsParams {
BitBoard board_in;
BitPieces pieces_expected;
};
struct OccupiedPositions
: testing::Test,
testing::WithParamInterface<OccupiedPositionsParams> {
};
// Tests
TEST_P(OccupiedPositions, EQ_Test)
{
auto params = GetParam();
auto conc_board = gameengine::utility::occupiedPositions(params.board_in);
EXPECT_EQ(conc_board, params.pieces_expected);
}
// Test set instantiations
/////////////////////////////
/// Occupied position
/// Pieces set and Board test
struct OccupiedParams {
std::variant<BitPieces, BitBoard> piece_set_in;
BitPos pos_in;
bool state_expected;
};
struct UtilityBoardTest : testing::Test, testing::WithParamInterface<UtilityBoardTestParams> {
UtilityBoardTest() {}
~UtilityBoardTest() override;
struct Occupied : testing::Test, testing::WithParamInterface<OccupiedParams> {
};
UtilityBoardTest::~UtilityBoardTest() {}
// Tests
TEST_P(Occupied, EQ_Test)
{
auto params = GetParam();
auto piece_set_in = params.piece_set_in;
bool state;
if (std::holds_alternative<BitPieces>(piece_set_in))
state = gameengine::utility::occupied(std::get<BitPieces>(piece_set_in),
params.pos_in);
else if (std::holds_alternative<BitBoard>(piece_set_in))
state = gameengine::utility::occupied(std::get<BitBoard>(piece_set_in),
params.pos_in);
else
FAIL() << "\"Expected\" value holds wrong type alternative";
EXPECT_EQ(state, params.state_expected);
}
// Test set instantiations
TEST_P(UtilityBoardTest, NextPosition)
/////////////////
/// Next Position
// Testing fixture
struct NextPositionParams {
BitPos pos_in;
MoveDirection dir_in;
BitPos next_pos_expected;
};
struct NextPosition : testing::Test,
testing::WithParamInterface<NextPositionParams> {
};
// Tests
TEST_P(NextPosition, EQ_Test)
{
auto params = GetParam();
auto next_pos = gameengine::utility::nextPosition(params.pos,params.dir);
EXPECT_EQ(params.expected_next_pos,next_pos);
auto next_pos
= gameengine::utility::nextPosition(params.pos_in, params.dir_in);
EXPECT_EQ(next_pos, params.next_pos_expected);
}
// Test set instantiations
INSTANTIATE_TEST_CASE_P(
ValidMoves, UtilityBoardTest,
testing::Values(UtilityBoardTestParams{BitPos(0), MoveDir::N, BitPos(8)},
UtilityBoardTestParams{BitPos(9), MoveDir::S, BitPos(1)}), );
ValidMoves_DataSet_0, NextPosition,
testing::Values(NextPositionParams{BitPos(0), MoveDirection::N, BitPos(8)},
NextPositionParams{BitPos(9), MoveDirection::S,
BitPos(1)}), );
INSTANTIATE_TEST_CASE_P(
InvalidMoves, UtilityBoardTest,
InvalidMoves_DataSet_0, NextPosition,
testing::Values(
UtilityBoardTestParams{BitPos(1), MoveDir::S, BitPos::invalid()},
UtilityBoardTestParams{BitPos(61), MoveDir::N, BitPos::invalid()}), );
NextPositionParams{BitPos(1), MoveDirection::S, BitPos::invalid()},
NextPositionParams{BitPos(61), MoveDirection::N, BitPos::invalid()}), );
///////////////
/// Legal moves
/// generation
struct LegalMovesParams {
BitBoard board_in;
PlayerId playerid_in;
BitPosSet bitposset_expected;
};
struct LegalMoves : testing::Test,
testing::WithParamInterface<LegalMovesParams> {
};
// Tests
TEST_P(LegalMoves, EQ_Test)
{
auto params = GetParam();
auto bit_pos_set
= gameengine::utility::legalMoves(params.board_in, params.playerid_in);
EXPECT_EQ(bit_pos_set, params.bitposset_expected);
}
// Test set instantiations
//////////////
/// Legal move
/// validation
struct IsLegalMovesParams {
BitBoard board_in;
PlayerId playerid_in;
BitPos pos_in;
bool state_expected;
};
struct IsLegalMoves : testing::Test,
testing::WithParamInterface<IsLegalMovesParams> {
};
// Tests
TEST_P(IsLegalMoves, EQ_Test)
{
auto params = GetParam();
auto state = gameengine::utility::isLegalMove(
params.board_in, params.playerid_in, params.pos_in);
EXPECT_EQ(state, params.state_expected);
}
// Test set instantiations
///////////
/// Move
/// execute
struct ExecuteMoveParams {
BitBoard board_in;
PlayerId playerid_in;
BitPos pos_in;
BitBoard board_expected;
};
struct ExecuteMove : testing::Test,
testing::WithParamInterface<ExecuteMoveParams> {
};
// Tests
TEST_P(ExecuteMove, EQ_Test)
{
auto params = GetParam();
auto board = params.board_in;
gameengine::utility::move(board, params.playerid_in, params.pos_in);
EXPECT_EQ(board, params.board_expected);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment