天天看點

gtest基礎使用05:Google Test自帶示例二:Class

Google Test Sample02

    • 一、環境資訊
    • 二、Google Test Sample02
      • 1. 示例描述
      • 2. 單元測試用例解析

一、環境資訊

  1. Visual Studio 2019
  2. Windows 10
  3. 前導文檔: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 運作結果

gtest基礎使用05:Google Test自帶示例二:Class

繼續閱讀