测试两个类型列表中的所有组合
Test all the combinations out of two type lists
我正在使用Google测试框架,并且有两个类型列表,我需要运行相同的测试套件。我使用的是宏TYPED_TEST_CASE
但这里的问题是它被迫只与一个类型列表一起使用,而不是两个或更多。
我需要使用这两个类型列表之间的所有组合运行此测试套件。有可能做到吗?如果TYPED_TEST_CASE
宏只接受一个类型列表,是否可以生成包含之前所有组合的列表,将它们插入到列表中,然后使用仅一个列表的宏?
你去吧。它一如既往地需要一些元魔法。为了使它顺利适应GTest,我使用了::testing::Types
,现在您只需将cartesian_product
作为类型参数传递即可进行测试。 编辑:由于::testing::Types
不是真正的可变参数,我不得不做一些帮助和转换:
template<class... Args>
struct Types { };
template<class First, class Second>
struct type_pair {
using first = First;
using second = Second;
};
template<class TypeList, template <class> class Mapper>
struct meta_map {
using type = void;
};
template<template <class> class Mapper, class... Args>
struct meta_map<Types<Args...>, Mapper> {
using type = Types<Mapper<Args>...>;
};
template<class Arg, class TypeList>
struct create_pairs {
template<class T>
using make_pair = type_pair<T, Arg>;
using type = typename meta_map<TypeList, make_pair>::type;
};
template<class List, class... Lists>
struct sum {
using type = void;
};
template<class... Ts>
struct sum<Types<Ts...>> {
using type = Types<Ts...>;
};
template<class... T1, class... T2>
struct sum<Types<T1...>, Types<T2...>> {
using type = typename sum<Types<T1..., T2...>>::type;
};
template<class... T1, class... T2>
struct sum<Types<T1...>, T2...> {
using type = typename sum<Types<T1...>, typename sum<T2...>::type>::type;
};
template<class List, template <class...> class Reducer>
struct meta_reduce {
using type = void;
};
template<class... Args, template <class...> class Reducer>
struct meta_reduce<Types<Args...>, Reducer> {
using type = typename Reducer<Args...>::type;
};
template<class TypeList1, class TypeList2>
struct cartesian_product_helper {
using type = void;
};
template<class TypeList1, class... Args>
struct cartesian_product_helper<TypeList1, Types<Args...>> {
using type = typename meta_reduce<Types<typename create_pairs<Args, TypeList1>::type...>, sum>::type;
};
template<class List1, class List2>
using cartesian_product = typename cartesian_product_helper<List1, List2>::type;
template<class TypeList>
struct to_test_types {
using type = void;
};
template<class... Ts>
struct to_test_types<Types<Ts...>> {
using type = ::testing::Types<Ts...>;
};
template<class TypeList>
using to_test_types_t = typename to_test_types<TypeList>::type;
用法如下所示:
to_test_types_t<cartesian_product<
Types<char, bool, unsigned>,
Types<char, bool>
>>;
现场示例: https://godbolt.org/z/XmyHDT
编辑:在Godbolt上添加了gtest,它似乎可以与此代码正常工作
相关文章:
- 测试两个类型列表中的所有组合
- 动态填充的组合框不显示文本,列表未定义
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 可视化 如何将 CString 值列表添加到 MFC C++ 中的组合框中?
- 在 MFC 组合框中设置文本,而不将其添加到列表中
- C++ 有没有办法以相同的效率组合此列表和映射
- 快速的唯一组合(来自重复项列表),无需查找
- 组合的实现 - 避免初始值设定项列表
- 如何使用超载 运算符组合两个链接列表
- 如何在列表中获取元素的所有组合
- 使用两个列表中的组合生成函数调用
- 从给定的 IPv6:端口列表中搜索 IPv6:端口组合的最快搜索算法是什么 O(1) 时间一致性
- 如何有效地将数字矢量列表组合到一个大数字向量中
- 在列表中对对象进行排序并组合值
- MFC 组合框下拉列表:仅显示一个项目
- 项目列表中的组合
- MFC组合框,灼热字体列表
- 在自定义组合框下拉列表控件上等待超过5秒会导致win32 C++应用程序在Windows7中挂起
- 组合可选宏以形成列表
- 在C++中生成组合列表的最简单方法是什么?