Google Test Sample02
-
- 一、环境信息
- 二、Google Test Sample02
-
- 1. 示例描述
- 2. 单元测试用例解析
一、环境信息
- Visual Studio 2019
- Windows 10
- 前导文档:gtest基础使用02,C++ 类 对象
二、Google Test Sample02
1. 示例描述
1.1 Google Test sample02主要演示了如何测试类
1.2 sample02由三个部分组成:sample02.h , sample02.cpp , sample02UnitTest.cpp。sample02.h中声明了待测类,定义了成员函数和成员变量; sample02.cpp对成员函数进行了具体实现;sample02UnitTest.cpp是对应的单元测试用例
1.3 sample02.h中用到了 构造函数、拷贝构造函数、析构函数,并定义了成员函数
#include <string.h>
// A simple string class.
class MyString
{
private:
const char* c_string_; //声明一个字符指针常量 c_string_
const MyString& operator=(const MyString& rhs);
//赋值运算符重载函数,在本例中仅进行了声明,并未看到具体的实现,但是又不可或缺 why?
public:
// Clones a 0-terminated C string, allocating memory using new. //克隆一个以 \0 结尾的字符串,通过 new 申请内存空间
static const char* CloneCString(const char* a_c_string); //CloneCString() 函数将在 sample02.cpp中进行具体的实现
// The default c'tor constructs a NULL string.
MyString() : c_string_(nullptr) {} //构造函数,将 c_string_ 赋值为 空指针nullptr(相当于NULL。C++中推荐nullptr)
// Constructs a MyString by cloning a 0-terminated C string.
explicit MyString(const char* a_c_string) : c_string_(nullptr)
{ //构造函数,克隆一个以 \0 结尾的字符串。 explicit?
Set(a_c_string);
}
// Copy constructor Func.
MyString(const MyString &string) : c_string_(nullptr) //string是一个对象引用,用于初始化另一个对象
{ //拷贝构造函数
Set(string.c_string_);
}
//MyString is intended to be a final class, so the d'tor doesn't need to be virtual.
~MyString() { delete[] c_string_; } //final class? virtual? //析构函数 deconstructor Func.
// Gets the 0-terminated C string this MyString object represents.
const char* c_string() const { return c_string_; } // c_string()函数用于获取字符串 c_string_
// Length()函数用于返回字符串 c_string_的长度
size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }
//Sets the 0-terminated C string this MyString object represents.
void Set(const char* c_string); //set()函数将在 sample02.cpp中进行具体的实现
};
1.4 sample02.cpp对成员函数CloneCString()、Set()进行了具体的实现
#include "sample02.h"
#include <string.h>
// Clones a 0-terminated C string, allocating memory using new.
const char* MyString::CloneCString(const char* a_c_string)
{
if (a_c_string == nullptr) return nullptr; //空指针的情况
const size_t len = strlen(a_c_string); //size_t类似于int
char* const clone = new char[len + 1]; //const有时前 有时候 why?
memcpy(clone, a_c_string, len + 1);
return clone;
}
// Sets the 0-terminated C string this MyString object represents.
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;
}
2. 单元测试用例解析
2.1 具体解析见注释
#include "pch.h"
#include "sample02.h"
//
In this example, we test the MyString class (a simple string).
//
Tests the default c'tor.
TEST(MyString, DefaultConstructor) // 1.测试默认构造函数 MyString() : c_string_(nullptr) {}
{
const MyString s;
EXPECT_STREQ(nullptr, s.c_string()); //const char* c_string() const { return c_string_; }
EXPECT_EQ(0, s.Length());
}
//
const char kHelloString[] = "Google Test Framwork!";
//
// Tests the c'tor that accepts a C string.
TEST(MyString, ConstructorFromCString) // 2.测试构造函数(该测试点包含成员函数Set()、Length()、c_string() )
{ //explicit MyString(const char* a_c_string) : c_string_(nullptr) { Set(a_c_string);}
const MyString s(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
EXPECT_EQ( sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length() );
}
//
// Tests the copy c'tor.
TEST(MyString, CopyConstructor) //3.测试拷贝构造函数
{
const MyString s1(kHelloString); //构造函数
const MyString s2 = s1; //拷贝构造函数 MyString(const MyString &string) : c_string_(nullptr) { Set(string.c_string_); }
EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
}
// Tests the Set method.
TEST(MyString, Set) //4.测试Set()
{
MyString s;
s.Set(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
// Set should work when the input pointer is the same as the one already in the MyString object.
s.Set(s.c_string()); //5.测试Set()的特殊情况:自身
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
// Can we set the MyString to NULL?
s.Set(nullptr); //6.测试特殊情况:NULL
EXPECT_STREQ(nullptr, s.c_string());
}
2.2 运行结果
