如何在 Google Test 中使用不同模板测试多个模板类的相同行为?

2024-01-12

我正在练习 C++ 17 中的排序算法,并按如下方式实现了我的单元测试(以下编译和所有测试都是绿色的):

template <typename T>
class SortingmethodTest : public ::testing::Test
{
protected:   
    T sortingmethod;

    static constexpr int amount_test_data[7] = {0, 4, 8, 10, 256, 1000, 1234};
};

using sortingmethods = ::testing::Types<STLSort<int>,
                                         InsertionSort<int>,
                                         ShellSort<int>,
                                         MergeSort<int>,
                                         OptimizedMergeSort<int>,
                                         QuickSort<int>>;

TYPED_TEST_SUITE(SortingmethodTest, sortingmethods);

TYPED_TEST(SortingmethodTest, sort)
{
    for (const auto& amount : this->amount_test_data)
    {
        Sortvector<int> test(amount);
        test.vul_random_zonder_dubbels(); // Fills the vector

        this->sortingmethod(test); // operator() of the sortmethod used (STLSort, InsertionSort, ...) sorts the vector

        ASSERT_TRUE(test.is_range());
        ASSERT_TRUE(test.is_gesorteerd());
        ASSERT_TRUE(std::is_sorted(test.begin(), test.end()));
    }
}

TYPED_TEST(SortingmethodTest, sort_reverse)
{
    // ...
}

TYPED_TEST(SortingmethodTest, sort_already_sorted)
{
    // ...
}

TYPED_TEST(SortingmethodTest, sort_empty)
{
    // ...
}

我想对除整数之外的其他类型重复相同的测试,例如

STLSort<int>,
InsertionSort<int>,
ShellSort<int>,
MergeSort<int>,
OptimizedMergeSort<int>,
QuickSort<int>

STLSort<double>,
InsertionSort<double>,
ShellSort<double>,
MergeSort<double>,
OptimizedMergeSort<double>,
QuickSort<double>

STLSort<CustomType>,
InsertionSort<CustomType>,
ShellSort<CustomType>,
MergeSort<CustomType>,
OptimizedMergeSort<CustomType>,
QuickSort<CustomType>

...

我怎样才能在 C++ 中使用 google test 尽可能干净地并尽可能多地重用呢?我在类型测试和类型参数化测试的丛林中迷失了方向 [1]:我什么时候应该使用其中之一?

亲切的问候,

Marten

[1] https://github.com/google/googletest/blob/master/docs/advanced.md#type-parameterized-tests https://github.com/google/googletest/blob/master/docs/advanced.md#type-parameterized-tests


令人沮丧的是,到目前为止 googletest API 并没有为我们提供更多的信息 利用现代C++使测试代码简洁,特别是对于 模板。但直到 v1.8.x(当前版本系列位于 截至目前)googletest 已致力于 C++98 兼容性,这就是主要原因。 即将发布的 1.9.x 将继续兼容 C++11,我们希望 以获得更强大的 API。

尽管如此,现在可以编写相当简洁和直接的 googletest 代码来 做你想要的事情:也就是说,对一致模板进行单元测试,以获取仅一个的不同值 模板参数。

有不止一种方法可以做到这一点。这是其中之一的一个有效示例, 使用类型参数化测试 https://github.com/google/googletest/blob/master/docs/advanced.md#type-parameterized-tests.

我们将有一组三个模板

template<typename T> struct (AA|BB|CC) {...};

每个都提供(至少)接口:

Name::Name(T const & u);
Name::operator int() const;
Name Name::operator+(Name const & u) const;
Name & Name::operator+=(Name const & u);
Name Name::operator-(Name const & u) const;
Name & Name::operator-=(Name const & u);

for Name = (AA|BB|CC)。我们想 对每个接口进行单元测试(AA|BB|CC),每个实例化为 六种类型中的每一种:

char, int, float, AA<char>, BB<int>, CC<float>

因此需要测试 18 个实例:

AA<char>, AA<int>, AA<float>, AA<AA<char>>, AA<BB<int>>, AA<CC<float>>
BB<char>, BB<int>, BB<float>, BB<AA<char>>, BB<BB<int>>, BB<CC<float>>
CC<char>, CC<int>, CC<float>, CC<AA<char>>, CC<BB<int>>, CC<CC<float>>

为了简单起见,我们将只实现两个通用测试。对于物体a, b and c任何实例化测试类型:

  • After a = b + c; b += c, then a == b.
  • Given b != c, after a = b - c; c -= b, then a != c.

(至少,只要操作不溢出或不溢出,这些属性就应该保持 失去精度,我会避免这种情况)。

所以我们预计会看到 36 项测试。

对于这个插图我不在乎什么AA, BB and CC除了它们的通用接口之外, 所以我只是从一个模型中以相同的方式导出它们,如下所示:

一些_类型.h

#pragma once

#include <type_traits>

namespace detail {
    template<typename T>
    struct bottom_type {
        using type = T;
    };

    template<template<typename ...> class C, typename ...Ts>
    struct bottom_type<C<Ts...>> {
        using type = typename C<Ts...>::type;
    };
}

template<typename T>
using bottom_t = typename detail::bottom_type<T>::type;

template<
    typename T,
    typename Enable = std::enable_if_t<std::is_arithmetic_v<bottom_t<T>>>
>
struct model
{
    using type = bottom_t<T>;

    model() = default;
    model(model const &) = default;
    model(T const & t)
    : _t{t}{}

    operator type() const { return _t; }

    auto operator+(model const & u) const {
        return _t + u;
    }

    auto & operator+=(model const & u) {
        _t += u;
        return *this;
    }

    auto operator-(model const & u ) const {
        return _t - u;
    }

    auto & operator-=(model const & u ) {
        _t -= u;
        return *this;
    }

protected:
    type _t = 0;
};

template<typename T> struct AA : model<T>{ using model<T>::model; };
template<typename T> struct BB : model<T>{ using model<T>::model; };
template<typename T> struct CC : model<T>{ using model<T>::model; };

现在这是我的谷歌测试代码:

main.cpp

#include <array>
#include <algorithm>
#include <random>
#include <type_traits>
#include <limits>
#include <gtest/gtest.h>
#include "some_types.h"

template<typename T>
struct fixture : public ::testing::Test
{
protected:

    template<typename U>
    static auto const & test_data() {
        using type = bottom_t<U>;
        static std::array<type,1000> data;
        static bool called;
        if (!called) {
            std::default_random_engine gen;
            auto low = std::numeric_limits<type>::min() / 2;
            auto high = std::numeric_limits<type>::max() / 2;
            auto dist = [&low,&high](){
                if constexpr (std::is_floating_point_v<type>) {
                    return std::uniform_real_distribution<type>(low,high);
                } else {
                    return std::uniform_int_distribution<type>(low,high);
                }
            }();
            std::generate(
                data.begin(),data.end(),[&dist,&gen](){ return dist(gen); });
            called = true;
        }
        return data;
    }
};


template<template<typename> class C, typename ...Ts>
using test_types = ::testing::Types<C<Ts>...>;

using AA_test_types = test_types<AA,char,int,float,AA<char>,BB<int>,CC<float>>;
using BB_test_types = test_types<BB,char,int,float,AA<char>,BB<int>,CC<float>>;
using CC_test_types = test_types<CC,char,int,float,AA<char>,BB<int>,CC<float>>;

TYPED_TEST_SUITE_P(fixture);

TYPED_TEST_P(fixture, addition)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri)
    {
        TypeParam lhs{*fi}, rhs{*ri};
        auto sum = lhs + rhs;
        lhs += rhs;
        ASSERT_EQ(lhs,sum);
    }
}

TYPED_TEST_P(fixture, subtraction)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri) {
        TypeParam lhs{*fi}, rhs{*ri};
        if (lhs != rhs) {
            auto diff = lhs - rhs;
            rhs -= lhs;
            ASSERT_NE(rhs,diff);
        }
    }
}

REGISTER_TYPED_TEST_SUITE_P(fixture,addition,subtraction);
INSTANTIATE_TYPED_TEST_SUITE_P(AA_tests, fixture, AA_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(BB_tests, fixture, BB_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(CC_tests, fixture, CC_test_types);

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

让我们看看感兴趣的点:-

template<template<typename> class C, typename ...Ts>
using test_types = ::testing::Types<C<Ts>...>;

在这里,我正在制作test_types的模板别名::testing::Types<SomeType...>列表 在哪里SomeType将是正在测试的模板之一的实例。正如它 发生了,我的模板AA, BB, CC(像你的一样)都是以下形式:

template<typename T> class;

所以我想要test_types成为一个:

::testing::Types<C<Ts>...>

然后我定义了 3 个具体类型别名:

using AA_test_types = test_types<AA,char,int,float,AA<char>,BB<int>,CC<float>>;
using BB_test_types = test_types<BB,char,int,float,AA<char>,BB<int>,CC<float>>;
using CC_test_types = test_types<CC,char,int,float,AA<char>,BB<int>,CC<float>>;

分别相当于:

::testing::Types<AA<char>, AA<int>, AA<float>, AA<AA<char>>, AA<BB<int>>, AA<CC<float>>>;
::testing::Types<BB<char>, BB<int>, BB<float>, BB<AA<char>>, BB<BB<int>>, BB<CC<float>>>;
::testing::Types<CC<char>, CC<int>, CC<float>, CC<AA<char>>, CC<BB<int>>, CC<CC<float>>>;

然后我使用模板夹具定义类型参数化测试套件fixture.

TYPED_TEST_SUITE_P(fixture);

然后我定义两个类型参数化测试模式。

TYPED_TEST_P(fixture, addition)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri)
    {
        TypeParam lhs{*fi}, rhs{*ri};
        auto sum = lhs + rhs;
        lhs += rhs;
        ASSERT_EQ(lhs,sum);
    }
}

TYPED_TEST_P(fixture, subtraction)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri) {
        TypeParam lhs{*fi}, rhs{*ri};
        if (lhs != rhs) {
            auto diff = lhs - rhs;
            rhs -= lhs;
            ASSERT_NE(rhs,diff);
        }
    }
}

然后我在每次实例化时注册这两个模式以进行实例化 的fixture:

REGISTER_TYPED_TEST_SUITE_P(fixture,addition,subtraction);

然后我创建了 3 个实例,名为(AA|BB|CC)_tests of fixture为了 测试类型列表(AA|BB|CC)_test_types分别:

INSTANTIATE_TYPED_TEST_SUITE_P(AA_tests, fixture, AA_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(BB_tests, fixture, BB_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(CC_tests, fixture, CC_test_types);

就是这样。编译并链接:

$ g++ -std=c++17 -Wall -Wextra -pedantic -o gtester main.cpp -lgtest -pthread

Run:

./gtester
[==========] Running 36 tests from 18 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from AA_tests/fixture/0, where TypeParam = AA<char>
[ RUN      ] AA_tests/fixture/0.addition
[       OK ] AA_tests/fixture/0.addition (0 ms)
[ RUN      ] AA_tests/fixture/0.subtraction
[       OK ] AA_tests/fixture/0.subtraction (1 ms)
[----------] 2 tests from AA_tests/fixture/0 (1 ms total)

[----------] 2 tests from AA_tests/fixture/1, where TypeParam = AA<int>
[ RUN      ] AA_tests/fixture/1.addition
[       OK ] AA_tests/fixture/1.addition (0 ms)
[ RUN      ] AA_tests/fixture/1.subtraction
[       OK ] AA_tests/fixture/1.subtraction (0 ms)
[----------] 2 tests from AA_tests/fixture/1 (0 ms total)
...
...
...
[----------] 2 tests from CC_tests/fixture/4, where TypeParam = CC<BB<int> >
[ RUN      ] CC_tests/fixture/4.addition
[       OK ] CC_tests/fixture/4.addition (0 ms)
[ RUN      ] CC_tests/fixture/4.subtraction
[       OK ] CC_tests/fixture/4.subtraction (0 ms)
[----------] 2 tests from CC_tests/fixture/4 (0 ms total)

[----------] 2 tests from CC_tests/fixture/5, where TypeParam = CC<CC<float> >
[ RUN      ] CC_tests/fixture/5.addition
[       OK ] CC_tests/fixture/5.addition (0 ms)
[ RUN      ] CC_tests/fixture/5.subtraction
[       OK ] CC_tests/fixture/5.subtraction (0 ms)
[----------] 2 tests from CC_tests/fixture/5 (0 ms total)

[----------] Global test environment tear-down
[==========] 36 tests from 18 test suites ran. (4 ms total)
[  PASSED  ] 36 tests.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Google Test 中使用不同模板测试多个模板类的相同行为? 的相关文章

随机推荐

  • SQL繁琐添加数组作为参数

    我正在运行这个 SQL 查询tedious js使用参数 var query select from table name where id in ids request new sql Request query function err
  • Laravel Pluck 但结合名字+姓氏进行选择

    在 Laravel Vue 项目中使用 select2 并需要返回以下格式的 JSON id 0 text enhancement id 1 text bug 在 Laravel 中 我知道我可以使用 pluck 来创建列表数据 例如对于客
  • Google 地图信息窗口关闭按钮被隐藏

    我在我的网站中使用了带有多个标记和多个信息窗口的 Google 地图 不知何故 关闭按钮 小x 隐藏在信息窗口内 但如果在应显示的位置单击则可以使用 如何解决这个问题 修复了它 显然 bootstrap css 与图像冲突 必须使用这几行额
  • 如何区分 Google Fit Api 中手动添加的步数和传感器记录的步数

    我在我的项目中使用 Google Fit Api 来获取用户的每日步数 但问题是 用户可以通过添加活动来手动输入步骤 当我检索每日步数时 Google Fit Api 还会返回手动添加的步数 有什么方法可以区分手动添加的步骤和传感器记录的步
  • 适用于 Linux 的 C++ 对象序列化

    我正在编写一个需要通过网络发送和接收数据的程序 我从来没有处理过对象序列化 我读到了一些关于 Boost 和 Google Protocol Buffers 的建议 在 Linux 中使用哪个最好 如果您知道其他人 我将感谢您的帮助 Tha
  • Redis、StackExchange、与 MGET 配合使用,一次获取多个密钥

    有没有办法将 MGET 与 StackExchange Redis C 一起使用 我需要一种方法可以在一次通话中重奏多个按键 可以使用下一个方法 Task
  • 是什么让Python中的东西变得可迭代

    是什么让 Python 中的东西变得可迭代 IE 可以循环它for 我可以用Python创建一个可迭代的类吗 如果是这样 怎么办 要使类可迭代 请编写 iter 返回迭代器的方法 class MyList object def init s
  • CUDA - 如果我选择太多块怎么办?

    我仍然对这些未知大小的矩阵感到生气 每个维度可能在 10 20 000 之间变化 我正在查看 CUDA sdk 并想知道 如果我选择的块数量太高怎么办 就像 X 和 Y 维度上 9999 x 9999 块的网格一样 如果我的硬件具有无法容纳
  • git checkout my_branch 与 git checkout origin/my_branch

    我当时在branch1当我结帐时branch2像这样 两个分支都存在 git checkout origin branch2 然后我得到了一个分离头错误 You are in detached HEAD state You can look
  • 根据给定的 OpenApi/Swagger 规范验证请求负载和响应

    我们有一个 HTTP API 它是用Python 的拥抱框架 http www hug rest 但这并不是一成不变的 将来可能会被其他东西取代 在它旁边 我们手动写了一个OpenApi Swagger 2 0 规范文件 https git
  • 如何设置log4j属性文件?

    我有一个使用 log4j 的 Eclipse Java 项目 我无法将log4j配置文件设置为通过文件路径访问 我必须在 jar 中导出并运行该项目 这是我的尝试 public class Wita static Logger logger
  • ROR-使用回形针逐步生成多个图像

    我已经使用了 教程 here http sleekd com rails adding multiple images to a rails model with paperclip 但由于某种原因它不起作用 任何人都可以给我一个使用其他表
  • 如何每天自动从 git 存储库更新我的服务器文件

    我是这些服务器相关工作的菜鸟 我正在本地系统中编写一些 PHP 代码 并定期更新 github 中的存储库 每次我想测试我的应用程序时 我都会通过 FTP 将本地系统中的所有文件复制到我的服务器上 然后进行测试 现在我想知道是否有一种方法可
  • 服务和组件属性之间的 Angular2 数据绑定

    我需要对服务和组件属性之间的绑定以及 Angular2 中的数据绑定进行一些澄清 假设我有一个服务 单例 和一个组件 export class Service name Luke object id 1 getName return thi
  • 使用python提取一个句子

    如果该句子中存在特定单词 我想提取确切的句子 谁能告诉我如何用 python 做到这一点 我使用了 concordance 但它只打印单词匹配的行 快速提醒一下 断句实际上是一件非常复杂的事情 句号规则也有例外 例如 先生 或 博士 还有各
  • AttributeError:“KMeans”对象没有属性“inertia_”

    from sklearn cluster import KMeans import numpy import pandas as pd from pandas import read csv boston read csv desktop
  • 计算两个点阵列之间的成对角度矩阵

    我有两个点向量 x and y 成形 n p and m p 分别 举个例子 x np array 0 0 16341 0 98656 0 05937 0 25205 0 96589 0 05937 0 25205 0 96589 0 11
  • 在函数 (group_by) 中使用 dplyr 时出现问题

    我想使用 dplyr 进行一些数据操作 背景 我有一个调查权重和一堆变量 主要是李克特项目 我想对有和没有调查权重的每个类别的频率和百分比进行求和 举个例子 让我们只使用性别变量的频率 结果应该是这样的 gender freq freq w
  • 哪个 .NET 库具有写时复制集合?

    我正在搜索要在 C 程序中使用的 NET 写时复制集合 例如列表 字典等 哪些集合具有该属性 包括参考FSharp Core 然后你就可以访问多种不可变的集合 Set List Map etc 这些位于Microsoft FSharp Co
  • 如何在 Google Test 中使用不同模板测试多个模板类的相同行为?

    我正在练习 C 17 中的排序算法 并按如下方式实现了我的单元测试 以下编译和所有测试都是绿色的 template