测试随机失败
Tests randomly fails
我正在写棋盘游戏,我需要以下功能:玩家掷两个骰子,如果他掷出双骰子(两个骰子上的数字相同(,他可以再次掷骰子,如果他再次掷出双骰子,他就会入狱。
在我的Game
课上,它看起来像那样
void logic::Game::rollTheDice() {
m_throwsInCurrentTurn++;
int firstThrow = m_firstDice.roll();
int secondThrow = m_secondDice.roll();
m_totalRollResult += firstThrow + secondThrow;
if (firstThrow == secondThrow) m_doublesInCurrentTurn++;
}
std::string logic::Game::checkForDoubles() {
std::string message;
if (m_doublesInCurrentTurn == 0 && m_throwsInCurrentTurn == 1) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 1) {
message = "Doubles! Roll again.";
m_canThrow = true;
m_canMove = false;
}
if (m_doublesInCurrentTurn == 1 && m_throwsInCurrentTurn == 2) {
m_canThrow = false;
m_canMove = true;
}
if (m_doublesInCurrentTurn == 2 && m_throwsInCurrentTurn == 2) {
message = "Doubles again! You are going to jail.";
m_canThrow = false;
m_canMove = false;
getActivePlayer().lockInJail();
}
return message;
}
void logic::Game::setInMotion(unsigned number) {
m_players[m_activePlayer].startMoving();
m_players[m_activePlayer].incrementPosition(number);
}
m_canThrow
基本上启用或禁用单击"掷骰子"按钮的功能,m_canMove
决定玩家令牌是否可以开始移动,m_players[m_activePlayer]
std::vector<Player>
,startMoving()
这样做,
void logic::Player::startMoving() {
m_isMoving = true;
}
令牌移动需要,因此在这里无关紧要。
我需要向您展示Game
类中的最后一个函数是 reset()
,主要用于测试目的
void logic::Game::reset() {
m_throwsInCurrentTurn = 0;
m_doublesInCurrentTurn = 0;
m_totalRollResult = 0;
}
现在,单元测试有时会出错。有时,我的意思是完全随机,例如 10-20 次中有 1 次。
//first throw is double, second throw is not
TEST_F(GameTestSuite, shouldFinishAfterSecondRollAndMove) {
auto game = m_sut.get();
do {
if (game.getThrowsInCurrentTurn() == 2) game.reset();
game.rollTheDice();
game.checkForDoubles();
if (game.getThrowsInCurrentTurn() == 1 && game.getDoublesInCurrentTurn() == 1) {
ASSERT_EQ(game.canThrow(), true);
ASSERT_EQ(game.canMove(), false);
}
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
ASSERT_EQ(game.canThrow(), false);
ASSERT_EQ(game.canMove(), true);
game.setInMotion(game.getTotalRollResult());
ASSERT_EQ(game.getActivePlayer().isMoving(), true);
ASSERT_EQ(game.getActivePlayer().getPosition(), game.getTotalRollResult());
}
正是这条线,ASSERT_EQ(game.canThrow(), false);
有时等于do-while
循环之后true
,一旦m_canThrow
设置为false
不应该:
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);
是
} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() <= 1);
您希望最多允许两圈,但 0 或 1 双倍。
相关文章:
- 如果没有malloc,链表实现将失败
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- OpenSSL RSA 解密随机失败 C/C++
- 测试随机失败
- 如何创建随机内存分配失败
- 似乎是随机程序在完成C++时失败
- 使用快速英特尔随机生成器(SSE2)会因堆栈问题而失败..已损坏
- 在Windows上等待进程随机失败
- 从管道读取随机失败
- RSA_Private_Decrypt随机失败
- 在CentOS 6.4上使用Boost.Regex 1.53和gcc 4.8.1随机断言失败
- 随机运行失败与简单的计数项目