Google单元测试sample分析(一)

news/2024/7/5 2:27:56

本文开始从googletest提供的sample案例分析如何使用单元测试,

代码路径在googletest/googletest/samples/sample1.unittest.cc
本文件主要介绍EXPECT
*相关宏使用

EXPECT_EQ 判断是否相等
EXPECT_TRUE 是否为True
EXPECT_FALSE 是否为False

TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ(1, Factorial(-5));
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_GT(Factorial(-10), 0);

  // <TechnicalDetails>
  //
  // EXPECT_EQ(expected, actual) is the same as
  //
  //   EXPECT_TRUE((expected) == (actual))
  //
  // except that it will print both the expected value and the actual
  // value when the assertion fails.  This is very helpful for
  // debugging.  Therefore in this case EXPECT_EQ is preferred.
  //
  // On the other hand, EXPECT_TRUE accepts any Boolean expression,
  // and is thus more general.
  //
  // </TechnicalDetails>
}

下面介绍sample2_unittest.cc文件

这里测试MyString类,
EXPECT_STREQ(nullptr, s.c_string()); //这里的STREQ是判断字符串内容,s为空类比较要使用nullptr

TEST(MyString, DefaultConstructor) {
  const MyString s;

  // Asserts that s.c_string() returns NULL.
  //
  // <TechnicalDetails>
  //
  // If we write NULL instead of
  //
  //   static_cast<const char *>(NULL)
  //
  // in this assertion, it will generate a warning on gcc 3.4.  The
  // reason is that EXPECT_EQ needs to know the types of its
  // arguments in order to print them when it fails.  Since NULL is
  // #defined as 0, the compiler will use the formatter function for
  // int to print it.  However, gcc thinks that NULL should be used as
  // a pointer, not an int, and therefore complains.
  //
  // The root of the problem is C++'s lack of distinction between the
  // integer number 0 and the null pointer constant.  Unfortunately,
  // we have to live with this fact.
  //
  // </TechnicalDetails>
  EXPECT_STREQ(nullptr, s.c_string());

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

http://lihuaxi.xjx100.cn/news/1723455.html

相关文章

媒介易发稿教程,在人民网投稿的指南与技巧

在当今信息快速传播的时代&#xff0c;网络媒体成为了人们获取信息、表达观点的重要平台。而在这个庞大信息海洋中&#xff0c;人民网作为中国最具影响力的新闻网站之一&#xff0c;扮演着引领舆论、传播价值观念的重要角色。无论是个人经历、社会观察&#xff0c;还是学术研究…

酒店宾馆在线订房小程序源码系统:轻松预订 出行无忧 带完整搭建教程

大家好啊&#xff0c;罗峰来给大家推荐一款酒店宾馆在线订房小程序源码系统&#xff0c;随着互联网技术的发展和普及&#xff0c;越来越多的人选择在线预订酒店宾馆。为了满足这一需求&#xff0c;各大酒店订房APP或是小程序层出不穷&#xff0c;而搭建一个酒店宾馆在线订房小程…

Pico Neo4、Neo3开发手柄的使用交互监听

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR;public class InputEvent : MonoSingleton<InputEvent> {//*************输入设别**************************InputDevice leftHandController;Inp…

颈肩肌筋膜炎做什么检查

颈肩肌筋膜炎症状 颈肩背部广泛疼痛酸胀沉重感、麻木感&#xff0c;僵硬、活动受限&#xff0c;可向后头部及上臂放散。疼痛呈持续性&#xff0c;可因感染、疲劳、受凉、受潮等因素而加重。查体见颈部肌紧张&#xff0c;压痛点常在棘突及棘突旁斜方肌、菱形肌等&#xff0c;压…

掌握CSS Flexbox,打造完美响应式布局,适配各种设备!

&#x1f3ac; 江城开朗的豌豆&#xff1a;个人主页 &#x1f525; 个人专栏 :《 VUE 》 《 javaScript 》 &#x1f4dd; 个人网站 :《 江城开朗的豌豆&#x1fadb; 》 ⛺️ 生活的理想&#xff0c;就是为了理想的生活 ! ​ 目录 ⭐ 专栏简介 &#x1f4d8; 文章引言 基…

汽车网络安全--ECU的安全更新

目前,汽车ECU的软件更新可以总结分成三大类: 工厂刷写模式:工厂大批量刷写或者升级,一般在出厂用; 工程模式:4S店、工厂等专业人员进行的ECU固件更新,通常是动力、转向、车控等; 车主模式:车主根据云端推送信息,通过IVI进行应用软件更新;目前也有趋势通过这种方式刷…

Writing an OS in Rust : Rust Heap Allocation 动态内存分配原理

原文地址 为了保证概念的严谨性&#xff0c;翻译时保留了英文原文。 This post adds support for heap allocation to our kernel. First, it gives an introduction to dynamic memory and shows how the borrow checker prevents common allocation errors. It then implem…

C++中invoke与function的区别

C invoke invoke是C17标准引入的一个函数模板&#xff0c;用来调用可调用对象&#xff08;Callable Object&#xff0c;如函数指针、函数对象、成员函数指针等&#xff09;并返回结果。 invoke提供了统一的调用语法&#xff0c;无论可调用对象的类型是什么&#xff0c;都可以…