2021.1.13 (含Gtest第二个sample)

第29天

早上

老师早上说按照新的要求帮我重新写个毕设任务书,希望不要在这边成天zmq了。

所以继续读Gtest的sample,顺便补补C++,并记录一下学到的内容(翻译注释 )。

Gtest-sample2

这是一个对类操作的简单测试程序,测试的类是一个简单的自定义字符串。

Gtest 小技巧 - 3

   如果我们在使用断言语句EXPECT_EQ时,必须指定参数的类型,以便在发生错误时print调试信息。所以,如果我们使用 EXPECT_EQ判断指针类型时,请勿使用NULL 而是使用 static_cast<const char *>(NULL) 或更推荐的 nullptr。否则会产生一个gcc警告。
    问题的根源是NULL在C++中的宏定义为0(注意在C中的NULL被定义为(void*)0,是没有问题的,之所以C++这么定义是为了满足重载需求),也就是说,对于NULL的数据格式,C++编译器是默认为int的,而gcc会认为NULL应该是指针类型,所以警告。
   因此为了区别整数0和空指针常量,在C++中最好使用nullptr而避免NULL。

sample2源码:

// sample2.h
#ifndef GTEST_SAMPLES_SAMPLE2_H_
#define GTEST_SAMPLES_SAMPLE2_H_

#include <string.h>


// A simple string class.
class MyString {
 private:
  const char* c_string_;
  const MyString& operator=(const MyString& rhs);

 public:
  // Clones a 0-terminated C string, allocating memory using new.
  static const char* CloneCString(const char* a_c_string);

  
  //
  // C'tors

  // The default c'tor constructs a NULL string.
  MyString() : c_string_(nullptr) {}

  // Constructs a MyString by cloning a 0-terminated C string.
  explicit MyString(const char* a_c_string) : c_string_(nullptr) {
    Set(a_c_string);
  }

  // Copy c'tor
  MyString(const MyString& string) : c_string_(nullptr) {
    Set(string.c_string_);
  }

  
  //
  // D'tor.  MyString is intended to be a final class, so the d'tor
  // doesn't need to be virtual.
  ~MyString() { delete[] c_string_; }

  // Gets the 0-terminated C string this MyString object represents.
  const char* c_string() const { return 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);
};


#endif  // GTEST_SAMPLES_SAMPLE2_H_
//sample2.cc
#include "sample2.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);
  char* const clone = new char[ len + 1 ];
  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;
}

//sample2_unittest.cc
#include "sample2.h"
#include "gtest/gtest.h"
namespace {
// In this example, we test the MyString class (a simple string).

// Tests the default c'tor.
TEST(MyString, DefaultConstructor) {
  const MyString s;

  EXPECT_STREQ(nullptr, s.c_string());

  EXPECT_EQ(0u, s.Length());
}

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

// Tests the c'tor that accepts a C string.
TEST(MyString, ConstructorFromCString) {
  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) {
  const MyString s1(kHelloString);
  const MyString s2 = s1;
  EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
}

// Tests the Set method.
TEST(MyString, 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());
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

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

学到的C++用法:
   explict关键字——在构造函数只有一个参数时有效,避免在定义时产生隐式转换,从而造成执行过程与看上去的书写逻辑不同,造成隐患。

    构造冒号xx::gouzao(abc) : c_string_(nullptr) ——列表初始化,一般用于在构造函数后初始化函数中的私有变量或父类,比写在构造函数内效率更高。

    static_cast 与dynamic_cast
    强制类型转换操作符 static_cast——static_cast相当于传统的C语言里的强制转换,该运算符把expression转换为new_type类型:static_cast< new_type >(expression)
    无名命名空间
    namespace{xxxxxx};——没有名字,因此不能被其他文件调用,生命周期为本文件结尾(限制条件甚至强于static,这就是为什么把测试语句放到里面的原因),本文件中可以不声明使用其中变量,一个文件只能定义一个无名空间。

    u1s1,谷歌的框架代码还是很专业的,有一些内容过于深入,知道是干嘛的就行了,关于继承类和模版的东西,可以用到了再学。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值