Gtest相关材料

http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

#include "gtest/gtest.h"

extern "C" {//C functions in C++
    #include "common.h"
    #include "util.h"
}

static MemSpace* ams=NULL;
static HashTable* table=NULL;
static FILE* directoryFp = NULL;
static const UInt32 DIRECTORY_FILE_LINE_SIZE = 98568;
#define DIRECTORY_DATA_SIZE (1024)
static char DIRECTORY_DATA[100000][DIRECTORY_DATA_SIZE];

static void resetDirectoryFP()
{
    fseek(directoryFp, 0L, SEEK_SET);
}

/*
 * 测试用例级别包裹
 */
class HashTest : public testing::Test
{
    protected:
        virtual void SetUp()
        {
            ams = memSpaceCreate();
            table = hashTableCreate(ams, StringHashFuncHook, StringHashCmpHook);
        }
        virtual void TearDown()
        {
            freeMemSpace(ams);
        }
};

/*
 * 整个运行前后包裹
 */
class HashEnvironment : public testing::Environment
{
    public:
        virtual ~HashEnvironment() {}
        virtual void SetUp()
        {
            char filename[1024];
            sprintf(filename, "%s/%s",__TEST_RES_DIR__,"american-english.txt");
            directoryFp = fopen(filename, "r");
            UInt32 tot = 0;
            while(fgets(DIRECTORY_DATA[tot], DIRECTORY_DATA_SIZE, directoryFp)){
                tot ++;
            }
            EXPECT_EQ(DIRECTORY_FILE_LINE_SIZE, tot);
            fclose(directoryFp);
        }
        virtual void TearDown()
        {
        }
};

/*
 * TEST_F 会进行包裹: 需要与
 * class HashTest : public testing::Test
 * HashTest 名字一致
 */
TEST_F(HashTest, Create)
{
    EXPECT_TRUE(NULL!=ams);
    EXPECT_TRUE(NULL!=table);
}

TEST_F(HashTest, Contain)
{
    UInt32 i;
    i = 0;
    while(i<DIRECTORY_FILE_LINE_SIZE){
        EXPECT_FALSE(HashTableContain(table, DIRECTORY_DATA[i]));
        i ++;
    }
}

TEST_F(HashTest, Insert)
{
    UInt32 i;
    i = 0;
    while(i<DIRECTORY_FILE_LINE_SIZE){
        EXPECT_TRUE(HashTableInsert(table, DIRECTORY_DATA[i]));
        i ++;
    }

    i = 0;
    while(i<DIRECTORY_FILE_LINE_SIZE){
        EXPECT_TRUE(HashTableContain(table, DIRECTORY_DATA[i]));
        i ++;
    }
}


int main(int argc, char* argv[])
{
    /*
     * 这个是整个用例跑前跑后的状态设置
     */
    testing::AddGlobalTestEnvironment(new HashEnvironment);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
 

猜你喜欢

转载自qianjigui.iteye.com/blog/1617000