gtest 学习之三 测试类

这是gtest中给出的第二个例子sample2

头文件sample2.h

#ifndef GTEST_SAMPLE2_H_
#define GTEST_SAMPLE2_H_

#include <string.h>


// 一个简单的string类.
class MyString {
private:
    const char* c_string_;
    const MyString& operator=(const MyString& rhs);

public:

    // 克隆一个0-terminated C string, 用new分配内存.
    static const char* CloneCString(const char* a_c_string);

    
    //
    // 构造函数
    // 默认构造函数构造一个 NULL string.
    MyString() : c_string_(NULL) {}

    // 通过克隆一个0-terminated C string,构造 MyString
    explicit MyString(const char* a_c_string) : c_string_(NULL) {
        Set(a_c_string);
    }

    // 拷贝构造函数
    MyString(const MyString& string) : c_string_(NULL) {
        Set(string.c_string_);
    }

    
    //
    // D'tor.  MyString 是一个final 类, 因此其析构函数不必用virtual
    ~MyString() { delete[] c_string_; }

    // 获取MyString所代表的 0-terminated C string.
    const char* c_string() const { return c_string_; }

    size_t Length() const {
        return c_string_ == NULL ? 0 : strlen(c_string_);
    }

    // 设置MyString所代表的 0-terminated C string.
    void Set(const char* c_string);
};


#endif  // GTEST_SAMPLE2_H_

 

实现文件sample2.cpp

#include "sample2.h"
#include <string.h>

//克隆一个0-terminated C string, 用new分配内存.
const char* MyString::CloneCString(const char* a_c_string) {
    if (a_c_string == NULL) return NULL;

    const size_t len = strlen(a_c_string);
    char* const clone = new char[ len + 1 ];
    memcpy(clone, a_c_string, len + 1);

    return clone;
}

// 设置MyString所代表的 0-terminated C string.
void MyString::Set(const char* a_c_string) {
    // Makes sure this works when c_string == c_string_
    const char* const temp = MyString::CloneCString(a_c_string);
    delete[] c_string_;
    c_string_ = temp;
}

 

main.cpp中的测试代码

#include "sample2.h"
#include "gtest/gtest.h"

// 本例用于测试MyString

// 测试默认构造函数
TEST(MyString, DefaultConstructor) {
    const MyString s;

    EXPECT_STREQ(NULL, s.c_string());
    EXPECT_EQ(0u, s.Length());
}

const char kHelloString[] = "Hello, world!";

//测试带一个c string的构造函数
TEST(MyString, ConstructorFromCString) {
    const MyString s(kHelloString);
    EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
    EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
        s.Length());
}

// 测试拷贝构造函数
TEST(MyString, CopyConstructor) {
    const MyString s1(kHelloString);
    const MyString s2 = s1;
    EXPECT_TRUE(strcmp(s2.c_string(), kHelloString) == 0);
}

// 测试Set方法
TEST(MyString, Set) {
    MyString s;

    s.Set(kHelloString);
    EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);

    // 当Set方法的参数与MyString中的c string 指针相同时,设定依然有作用
    s.Set(s.c_string());
    EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);

    // Can we set the MyString to NULL?
    s.Set(NULL);
    EXPECT_STREQ(NULL, s.c_string());
}


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

编译后的运行结果

转载于:https://www.cnblogs.com/fanx/p/4573720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于进行随机测试,你可以使用 Google Test(gtest)框架提供的参数化测试功能。参数化测试允许你在测试中使用不同的参数运行多次,从而覆盖更多的测试场景和边界条件。 首先,你需要在测试用例中定义一个参数化测试。你可以使用 `INSTANTIATE_TEST_SUITE_P` 宏来定义一个测试套件,并使用 `Values` 函数来指定不同的参数。例如,假设你要测试一个函数 `int add(int a, int b)`: ```cpp #include <gtest/gtest.h> #include <gmock/gmock.h> int add(int a, int b) { return a + b; } class AddTest : public testing::TestWithParam<std::pair<int, int>> { }; TEST_P(AddTest, TestAdd) { int a = GetParam().first; int b = GetParam().second; EXPECT_EQ(a + b, add(a, b)); } INSTANTIATE_TEST_SUITE_P(Default, AddTest, testing::Values( std::make_pair(1, 2), std::make_pair(5, 5), std::make_pair(-10, 10) )); ``` 在上面的例子中,我们定义了一个 `AddTest` 来作为参数化测试测试用例。使用 `TEST_P` 宏来定义测试方法,通过 `GetParam()` 获取当前的参数,并进行断言判断。最后,使用 `INSTANTIATE_TEST_SUITE_P` 宏将测试用例实例化,并通过 `testing::Values` 函数指定不同的参数。 通过以上步骤,你就可以使用 gtest 进行随机测试了。你可以根据需要添加更多的参数组合,以覆盖更多的测试场景。运行测试时,gtest 将会自动为每个参数组合生成一个独立的测试用例,并输出测试结果。 希望这个例子能帮助到你进行随机测试。如果有任何进一步的问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值