测试随机失败

时间:2018-01-05 15:50:13

标签: c++ testing do-while

我正在编写棋盘游戏,我需要以下功能:玩家滚动两个骰子,如果他掷双打(两个骰子上的数字相同),他再次滚动,如果他再次滚动双打,他就会进入监狱。

在我的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>,{{1这样做,

startMoving()

需要进行令牌移动,所以这里无关紧要。

我需要向您展示的void logic::Player::startMoving() { m_isMoving = true; } 课程的最后一项功能是Game,主要用于测试目的

reset()

现在finnaly单元测试有时会出错。有时,我的意思是完全随机,比如10-20次中的1次。

void logic::Game::reset() {
    m_throwsInCurrentTurn = 0;
    m_doublesInCurrentTurn = 0;
    m_totalRollResult = 0;
}

这一行确切地说,//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);循环后等于truedo-while设置为m_canThrow后应该结束

1 个答案:

答案 0 :(得分:0)

不应该&#39; T:

} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() != 1);

} while (game.getThrowsInCurrentTurn() != 2 && game.getDoublesInCurrentTurn() <= 1);

你想允许最多两个回合但是0或1个双倍。