测试两个类型列表中的所有组合

问题描述 投票:2回答:1

我正在使用Google Test Framework,并且有两个类型的列表,我需要针对它们运行相同的测试套件。我正在使用宏TYPED_TEST_CASE,但是这里的问题是,它被强制仅用于一种类型的列表,而不是两种或多种。

我需要使用这两个类型列表之间的所有组合来运行此测试套件。有可能做到吗?如果TYPED_TEST_CASE宏仅接受一个类型列表,是否可以生成之前包含所有组合的列表,将它们插入列表,然后使用仅一个列表的宏?

c++ googletest
1个回答
0
投票

你去。一如既往的有些不可思议。为了使其顺利地适合GTest类型列表,应该以某种方式将其更改为::testing::Types,这应该非常简单:

template<class First, class Second>
struct type_pair {
    using first = First;
    using second = Second;
};

template<class Arg, class... Args>
struct type_list {
    using next = type_list<Args...>;
    using type = Arg;
};

template<class Arg>
struct type_list<Arg> {
    using next = void;
    using type = Arg;
};

template<class TypeList, template <class> class Mapper>
struct meta_map {
    using type = void;
};

template<template <class> class Mapper, class... Args>
struct meta_map<type_list<Args...>, Mapper> {
    using type = type_list<Mapper<Args>...>;
};

template<class Arg, class TypeList>
struct create_pairs {
    template<class T>
    using make_pair = type_pair<Arg, T>;
    using type = typename meta_map<TypeList, make_pair>::type;
};

template<class List, class... Lists>
struct sum {
    using type = void;
};

template<class... Ts>
struct sum<type_list<Ts...>> {
    using type = type_list<Ts...>;
};

template<class... T1, class... T2>
struct sum<type_list<T1...>, type_list<T2...>> {
    using type = typename sum<type_list<T1..., T2...>>::type;
};

template<class... T1, class... T2>
struct sum<type_list<T1...>, T2...> {
    using type = typename sum<type_list<T1...>, typename sum<T2...>::type>::type;
};

template<class TypeList1, class TypeList2>
struct cartesian_product_helper {
    using type = void;
};

template<class List, template <class...> class Reducer>
struct meta_reduce {
    using type = void;
};

template<class... Args, template <class...> class Reducer>
struct meta_reduce<type_list<Args...>, Reducer> {
    using type = typename Reducer<Args...>::type;
};

template<class TypeList1, class... Args>
struct cartesian_product_helper<TypeList1, type_list<Args...>> {
    using type = typename meta_reduce<type_list<typename create_pairs<Args, TypeList1>::type...>, sum>::type;
};

template<class List1, class List2>
using cartesian_product = typename cartesian_product_helper<List1, List2>::type;

int main(){
    cartesian_product<type_list<char, bool>, type_list<int, char>>::type::first a;
}

并且要将笛卡尔积转换为::testing::Values以完全适合,您可以使用以下代码段:

struct to_types_h {
    using type = void;
};

template<class... Ts>
struct to_types_h<type_list<Ts...>> {
    using type = testing::Types<Ts...>;
};

template<class TypeList>
using to_types = typename to_types_h<TypeList>::type;
© www.soinside.com 2019 - 2024. All rights reserved.