gtest基础使用09:Google Test自带示例六:Interface

本文详细介绍了如何使用GoogleTest在C++中针对具有多种实现的接口进行单元测试,涉及接口定义、接口实现、单元测试用例编写以及在VS2019环境下可能遇到的问题。通过实例展示typed tests和type-parameterized tests的区别,以及如何编写和执行针对OnTheFlyPrimeTable和PreCalculatedPrimeTable的测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、环境信息

  1. Visual Studio 2019
  2. Windows 10
  3. 特别注意:如果你在VS 2019下使用其集成的Google Test运行sample06,示例中的宏TYPED_TEST_SUITE需要更正为 TYPED_TEST_CASE,否则无法编译通过。经过分析,很可能是 VS 2019集成的gtest版本过低导致
  4. 前导知识:C++ 类、继承、模板(泛型编程)

二、Google Test Sample06

1. 示例概述

1.1 头文件PrimeTable.h定义了接口 PrimeTable,该接口可以用于 ==bool IsPrime(int n) ==判定一个数是否为素数 、int GetNextPrime(int p) 返回比当前数大的最小素数
1.2 PrimeTable.h虽然定义了接口,但未具体实现,它的具体实施例有两个 OnTheFlyPrimeTable、PreCalculatedPrimeTable ,采用了不同算法实现了接口功能
1.3 sample06就是用来演示如何测试 拥有多个实施例的接口
1.4 PrimeTable.h具体代码及注释如下,该代码难度比较高,在正式阅读之前需要掌握C++的类、继承、模板相关知识
1.5 PrimeTable.h对应的单元测试文件是 sample06UnitTest.cpp

#pragma once //#pragma once?

// This provides interface PrimeTable that determines whether a number is a prime and determines a next prime number.
// 本头文件提供了一个接口 interface 素数表 PrimeTable,用于判定一个数是否是素数 及 返回下一个素数 
// This interface is used in Google Test samples demonstrating use of parameterized tests.
// 该接口用于gtest参数测试示例

#ifndef GTEST_SAMPLES_PRIME_TABLES_H_ //#ifndef #define #endif
#define GTEST_SAMPLES_PRIME_TABLES_H_

#include <iostream>
using namespace std;

// The prime table interface. //class PrimeTable定义了接口,未具体实现
class PrimeTable 
{
   
    public:
        virtual ~PrimeTable() {
   } //析构函数
    
        // Returns true if and only if n is a prime number. //bool IsPrime(int n);
        virtual bool IsPrime(int n) const = 0; //virtual? const = 0?  

        // Returns the smallest prime number greater than p; or returns -1 if the next prime is beyond the capacity of the table.
        // GetNextPrime( ) 返回比p大的最小素数;若返回值超过素数表的范围,返回-1
        virtual int GetNextPrime(int p) const = 0;
};

// Implementation #1 calculates the primes on-the-fly. //接口的具体实施例一:直接计算素数
class OnTheFlyPrimeTable : public PrimeTable 
{
    
    //OnTheFlyPrimeTable继承自类PrimeTable,具体实现了类PrimeTable的成员函数 IsPrime( )和 GetNextPrime( ),
    public:
        bool IsPrime(int n) const override //const override?
        {
   
            if (n <= 1) return false; 
            for (int i = 2; i * i <= n; i++)
                if ((n % i) == 0) return false; // n is divisible by an integer other than 1 and itself.
            return true;
        }

        int GetNextPrime(int p) const override 
        {
   
            if (p < 0) return -1;

            for (int n = p + 1;; n++) 
            {
   
                if (IsPrime(n)) return n;
            }
        }
};

// Implementation #2 pre-calculates the primes and stores the result in an array.//接口的具体实施例二:引入数组,对素数进行标记
class PreCalculatedPrimeTable : public PrimeTable 
{
   
    private:
        const int is_prime_size_; //is_prime_size_ 定义了素数表中的最大数
        bool* const is_prime_; //定义布尔型数组 is_prime_

    public:

        // 'max' specifies the maximum number the prime table holds. // max定义了素数表的最大数      
        explicit PreCalculatedPrimeTable(int max) : is_prime_size_(max + 1), is_prime_(new bool[max + 1])  
        {
      //构造函数:is_prime_size_ = max+1 ; 定义了数组的大小 is_prime_[max + 1]  //explicit?
            CalculatePrimesUpTo(max); //声明了函数 CalculatePrimesUpTo( ) 并在下方的 private 中实现
        }

        ~PreCalculatedPrimeTable() override {
    delete[] is_prime_; } //析构函数,释放数组is_prime_占用的内存空间. delete和new对应

        bool IsPrime(int n) const override 
        {
    //在PrimeTable中声明了接口,在实施例中进行具体实现
            return 0 <= n && n < is_prime_size_ && is_prime_[n]; 
        } // is_prime_[n]为素数返回true; 0 <= n 处理了负数的情况; n < is_prime_size_ 限定了范围

        int GetNextPrime(int p) const override //返回比p大的最小素数,若超出范围,返回-1
        {
   
            for (int n = p + 1; n < is_prime_size_; n++)
            {
   
                if (is_prime_[n]) return n;
            }

            return -1;
        }

    private:
        void CalculatePrimesUpTo(int max)
        {
      
            //布尔型数组 is_prime_[max+1], is_prime_size_ 数组元素个数
            fill(is_prime_, is_prime_ + is_prime_size_, true); // ::std::fill()函数,将is_prime_数组中的所有元素均填充为true
        
            is_prime_[0] = is_prime_[1] 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值