【问题标题】:boost unit test cartesian product of mpl listmpl列表的boost单元测试笛卡尔积
【发布时间】:2021-08-25 21:46:04
【问题描述】:

假设我有两个 mpl 列表,比如说

typedef boost::mpl::list<int, double> inner_types;
typedef boost::mpl::list<std::vector, std::set> outer_types;

我希望能够在 boost 单元测试中迭代这些列表的笛卡尔积并从每个组合构造一个对象..类似于以下内容:

BOOST_TEST_CASE_TEMPLATE(mytest, InnerType, inner_types, OuterType, outer_types) {
    OuterType<InnerType> obj;

    BOOST_CHECK(ns::foo(obj));
}

但在使用 _TEMPLATE 进行 boost 测试用例时,您似乎只能有一个 mpl 列表。有没有办法实现我的意图?

【问题讨论】:

    标签: c++ templates boost


    【解决方案1】:

    您可以使用类似于thisBoost MP11合并两个列表 合并为一个笛卡尔积,如下所示

    #include <boost/mp11.hpp>
    
    template <template <typename...> typename... F>
    using mp_list_q = boost::mp11::mp_list<boost::mp11::mp_quote<F>...>;
    
    using outer_types = mp_list_q<std::vector, std::set>;
    using inner_types = boost::mp11::mp_list<int, double>;
    
    using TypeList = boost::mp11::mp_product<boost::mp11::mp_invoke_q,  
                                             outer_types, 
                                             inner_types>;
    

    在这个例子中TypeList corresponds to

    boost::mp11::mp_list<std::vector<int>, std::vector<double>, std::set<int>, std::set<double>>
    

    这可以用于类型化单元测试,如下所示:

    BOOST_TEST_CASE_TEMPLATE_FUNCTION(foo_test, T) {
      BOOST_TEST(ns::foo(T{}) == true);
    }
    
    boost::unit_test::test_suite* init_unit_test_suite(int, char* []) {
      boost::unit_test::framework::master_test_suite().add(BOOST_TEST_CASE_TEMPLATE(foo_test, TypeList));
      return EXIT_SUCCESS;
    }
    

    Try it here!


    如果由于某种原因您不能使用 Boost MP11,您可以使用可变参数模板为自己编写一个手动解决方案,以创建类似于 the one I have written here 的别名 TypeList

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2013-07-14
      • 2012-03-24
      • 2017-07-15
      • 2012-01-03
      • 2011-07-10
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多