MDGSF Software Engineer

[C/C++] gtest 使用

2017-07-04

main.cpp

#include <stdio.h>
#include "gtest/gtest.h"
#include "TestDemo.h"
#include "TestFanShu.h"

int main(int argc, char * argv[])
{
    testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();
    return 0;
}

TestDemo.h

#pragma once

int iAdd(int a, int b)
{
    return a + b;
}

TEST(TestDemo, iAdd_test)
{
    EXPECT_EQ(3, iAdd(1, 2));
}


TEST(TestDemo, cout_test)
{
    int ax[3] = { 0, 1, 2 };
    int ay[3] = { 0, 2, 2 };
    for (int i = 0; i < 3; i++)
    {
        //补充输出信息
        //EXPECT_EQ(ax[i], ay[i]) << "i = " << i;
    }
}

TestFanShu.h

#pragma once

#include "public.h"


class CFanShuTest : public testing::Test
{
protected:
    virtual void SetUp()
    {
        oCurRoomRule.set_ding_pai(LZMJ::SAN_YAO_FEI);
        oCurRoomRule.set_hei_san_feng(1);
        oCurRoomRule.set_zhong_fa_bai(1);
        oCurRoomRule.set_jiang_pai(LZMJ::WU_JIANG_PAI);
        oGameLogic.vSetDingPaiType(oCurRoomRule.ding_pai());

        oGameLogic.m_vInitRoomRule(&oCurRoomRule);
        oGameLogic.m_vInitIdentifier("");
        oGameLogic.m_vInitLeftCards(&m_oLeftCards);
        oGameLogic.vSetDingPaiType(oCurRoomRule.ding_pai());

        memset(&stHandCards, 0, sizeof(stHandCards));
    }

    virtual void TearDown()
    {

    }

    LZMJ::LZMJM_TableRule oCurRoomRule;
    CGameLogic oGameLogic;
    SHandCards stHandCards;
    CLeftCards m_oLeftCards;
};


TEST_F(CFanShuTest, FanShu1)
{
    Card aucHandCards[] = {
        0x11, 0x11, 0x11,
        0x12, 0x13, 0x14,
        0x15, 0x15, 0x15,
        0x16, 0x17, 0x18,
        0x19, 0x19
    };
    uint8_t ucHandCardsNum = sizeof(aucHandCards) / sizeof(aucHandCards[0]);
    vGenHandCards(stHandCards, aucHandCards, ucHandCardsNum);

    bool bRet = oGameLogic.m_bHu(stHandCards);
    EXPECT_EQ(true, bRet);

    EXPECT_EQ(2, oGameLogic.GetFanShu());
}


weixingongzhonghao

Comments

Content