Witam, proszę o ocenu kodu źródłowego. Ma on za zadanie przechowywanie informacji o tym, którego gracza jest obecnie tura. Czy takie testy jednostowe są wystarczające? Powinno się testować każdą metodę z osobna? Bo na przykład teraz metoda set pozostaje nieprzetestowana.
PS (namespace ttt pochodzi od TicTacToe).
WhoseTurn.h
#pragma once
namespace ttt {
class WhoseTurn
{
public:
enum class PlayerNumber { FIRST, SECOND };
enum class ErrorType { BAD_PLAYER_NUMBER };
WhoseTurn(const PlayerNumber& playerNumber = PlayerNumber::FIRST);
void set(const PlayerNumber& playerNumber);
void setTurnToNextPlayer();
void setRandomly();
bool isFirstPlayerTurn() const { return playerNumber == PlayerNumber::FIRST; };
bool isSecondPlayerTurn() const { return playerNumber == PlayerNumber::SECOND; };
private:
PlayerNumber playerNumber;
};
}
WhoseTurn.cpp
#include "WhoseTurn.h"
#include <ctime>
#include <cstdlib>
namespace ttt {
WhoseTurn::WhoseTurn(const PlayerNumber& playerNumber)
{
if (playerNumber != PlayerNumber::FIRST && playerNumber != PlayerNumber::SECOND)
throw ErrorType::BAD_PLAYER_NUMBER;
this->playerNumber = playerNumber;
srand(time(NULL));
}
void WhoseTurn::set(const PlayerNumber& playerNumber)
{
if (playerNumber != PlayerNumber::FIRST && playerNumber != PlayerNumber::SECOND)
throw ErrorType::BAD_PLAYER_NUMBER;
this->playerNumber = playerNumber;
}
void WhoseTurn::setTurnToNextPlayer()
{
playerNumber = (playerNumber == PlayerNumber::FIRST) ? PlayerNumber::SECOND : PlayerNumber::FIRST;
}
void WhoseTurn::setRandomly()
{
playerNumber = (rand() % 2) ? PlayerNumber::FIRST : PlayerNumber::SECOND;
}
}
WhoseTurnTest.cpp
#include "CppUnitTest.h"
#include "WhoseTurn.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TicTacToeEngineTest
{
TEST_CLASS(WhoseTurnTest)
{
public:
TEST_METHOD(WhoseTurnCheck)
{
ttt::WhoseTurn wt(ttt::WhoseTurn::PlayerNumber::FIRST);
wt.setTurnToNextPlayer();
Assert::IsTrue(wt.isSecondPlayerTurn());
wt.setTurnToNextPlayer();
Assert::IsTrue(wt.isFirstPlayerTurn());
}
TEST_METHOD(RandomWhoseTurn)
{
ttt::WhoseTurn wt(ttt::WhoseTurn::PlayerNumber::FIRST);
const int maxAmountOfTries = 1000000;
for (int i = 0; i < maxAmountOfTries; i++)
{
wt.setRandomly();
if (wt.isSecondPlayerTurn())
break;
else if (i + 1 == maxAmountOfTries)
Assert::Fail();
}
}
TEST_METHOD(BadPlayerNumber)
{
try {
ttt::WhoseTurn wt(ttt::WhoseTurn::PlayerNumber(-1));
Assert::Fail();
}
catch (ttt::WhoseTurn::ErrorType e) {
;
}
}
};
}