我正在 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)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我想对整数以外的其他类型重复相同的测试,例如
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>
...
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 C++ 中使用谷歌测试尽可能干净地并尽可能多地重用?我迷失在类型化测试和类型参数化测试的丛林中 [1]:我什么时候应该使用其中一种?
亲切的问候,
貂
[1] 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 代码来完成您想要的事情:也就是说,对仅一个模板参数的不同值的全等模板进行单元测试。
有不止一种方法可以做到这一点。这是其中之一的一个有效示例,使用类型参数化测试。
我们将有一组三个模板
template<typename T> struct (AA|BB|CC) {...};
Run Code Online (Sandbox Code Playgroud)
每个都提供(至少)接口:
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);
Run Code Online (Sandbox Code Playgroud)
对于Name= (AA|BB|CC)。我们想要对这个接口进行单元测试,对于每个(AA|BB|CC),每个都为六种类型中的每一种进行实例化:
char, int, float, AA<char>, BB<int>, CC<float>
Run Code Online (Sandbox Code Playgroud)
因此需要测试 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>>
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我们将只实现两个通用测试。对于对象
a以及任何实例化测试类型b:c
a = b + c; b += ca == bb != c,之后a = b - c; c -= b,然后a != c。(至少,只要操作不溢出或丢失精度,这些属性就应该保持不变,我将避免这种情况)。
所以我们预计会看到 36 项测试。
对于这个插图,除了它们的公共接口之外,我不关心AA、BB和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; };
Run Code Online (Sandbox Code Playgroud)
现在这是我的谷歌测试代码:
主程序
#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();
}
Run Code Online (Sandbox Code Playgroud)
让我们来看看感兴趣的点:-
template<template<typename> class C, typename ...Ts>
using test_types = ::testing::Types<C<Ts>...>;
Run Code Online (Sandbox Code Playgroud)
在这里,我正在test_types为列表创建一个模板别名::testing::Types<SomeType...>,其中SomeType将是正在测试的模板之一的实例化。碰巧,我的模板AA, BB, CC(像你的一样)都是以下形式:
template<typename T> class;
Run Code Online (Sandbox Code Playgroud)
所以我想test_types成为:
::testing::Types<C<Ts>...>
Run Code Online (Sandbox Code Playgroud)
然后我定义了 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>>;
Run Code Online (Sandbox Code Playgroud)
分别相当于:
::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>>>;
Run Code Online (Sandbox Code Playgroud)
然后我使用 template-fixture 定义类型参数化测试套件fixture。
TYPED_TEST_SUITE_P(fixture);
Run Code Online (Sandbox Code Playgroud)
然后我定义两个类型参数化测试模式。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我注册这两个模式以在每个实例化时进行实例化fixture:
REGISTER_TYPED_TEST_SUITE_P(fixture,addition,subtraction);
Run Code Online (Sandbox Code Playgroud)
然后我创建 3 个实例,分别(AA|BB|CC)_tests调用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);
Run Code Online (Sandbox Code Playgroud)
就是这样。编译并链接:
$ g++ -std=c++17 -Wall -Wextra -pedantic -o gtester main.cpp -lgtest -pthread
Run Code Online (Sandbox Code Playgroud)
跑步:
./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.
Run Code Online (Sandbox Code Playgroud)