如何将 C++ Google Test ::testing::ValuesIn 与 std::generator lambda 一起使用?

问题描述 投票:0回答:1
INSTANTIATE_TEST_SUITE_P(
    MyTests,
    MyTestFixture,
    ::testing::Combine(
        ::testing::Values(make_tuple(2, 8, vector<size_t>{1, 2, 3, 4, 5, 6, 5}),
                          make_tuple(3, 12, vector<size_t>{5, 7, 9, 13, 11, 6, 6, 3, 3}),
                          // make_tuple(4, 10, vector<size_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
                          ::testing::ValuesIn([]() -> generator<size_t>
                                              {
    co_yield 4;
    co_yield 10;
    co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
);

错误:

FAILED: test/CMakeFiles/DataStructuresAlgorithmsTests.dir/RangeTests.cpp.o 
/usr/bin/g++ -DUNICODE -DX64 -D_CONSOLE -D_DEBUG -D_UNICODE  -g -std=gnu++23 -Winvalid-pch -include /usr/src/DataStructuresAlgorithms/build/test/CMakeFiles/DataStructuresAlgorithmsTests.dir/cmake_pch.hxx -MD -MT test/CMakeFiles/DataStructuresAlgorithmsTests.dir/RangeTests.cpp.o -MF test/CMakeFiles/DataStructuresAlgorithmsTests.dir/RangeTests.cpp.o.d -o test/CMakeFiles/DataStructuresAlgorithmsTests.dir/RangeTests.cpp.o -c /usr/src/DataStructuresAlgorithms/test/RangeTests.cpp
In file included from /usr/include/gtest/gtest.h:67,
                 from /usr/src/DataStructuresAlgorithms/test/pch.h:2,
                 from /usr/src/DataStructuresAlgorithms/build/test/CMakeFiles/DataStructuresAlgorithmsTests.dir/cmake_pch.hxx:5,
                 from <command-line>:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp: In function ‘testing::internal::ParamGenerator<std::tuple<long unsigned int, long unsigned int, std::vector<long unsigned int, std::allocator<long unsigned int> > > > gtest_SumPairsTestsSumPairsTestFixture_EvalGenerator_()’:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:64:70: error: no matching function for call to ‘ValuesIn(std::generator<long unsigned int>)’
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/gtest/gtest-param-test.h:309:58: note: candidate: ‘template<class Container> testing::internal::ParamGenerator<typename Container::value_type> testing::ValuesIn(const Container&)’
  309 | internal::ParamGenerator<typename Container::value_type> ValuesIn(
      |                                                          ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:309:58: note:   template argument deduction/substitution failed:
/usr/include/gtest/gtest-param-test.h: In substitution of ‘template<class Container> testing::internal::ParamGenerator<typename Container::value_type> testing::ValuesIn(const Container&) [with Container = std::generator<long unsigned int>]’:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:52:1:   required from here
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/gtest/gtest-param-test.h:309:58: error: no type named ‘value_type’ in ‘class std::generator<long unsigned int>’
  309 | internal::ParamGenerator<typename Container::value_type> ValuesIn(
      |                                                          ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:297:1: note: candidate: ‘template<class ForwardIterator> testing::internal::ParamGenerator<typename std::iterator_traits<_Iterator>::value_type> testing::ValuesIn(ForwardIterator, ForwardIterator)’
  297 | ValuesIn(ForwardIterator begin, ForwardIterator end) {
      | ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:297:1: note:   candidate expects 2 arguments, 1 provided
/usr/include/gtest/gtest-param-test.h:304:29: note: candidate: ‘template<class T, long unsigned int N> testing::internal::ParamGenerator<T> testing::ValuesIn(const T (&)[N])’
  304 | internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {
      |                             ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:304:29: note:   template argument deduction/substitution failed:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:64:70: note:   mismatched types ‘const T [N]’ and ‘std::generator<long unsigned int>’
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp: In function ‘std::string gtest_SumPairsTestsSumPairsTestFixture_EvalGenerateName_(const testing::TestParamInfo<std::tuple<long unsigned int, long unsigned int, std::vector<long unsigned int, std::allocator<long unsigned int> > > >&)’:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:64:70: error: no matching function for call to ‘ValuesIn(std::generator<long unsigned int>)’
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/gtest/gtest-param-test.h:309:58: note: candidate: ‘template<class Container> testing::internal::ParamGenerator<typename Container::value_type> testing::ValuesIn(const Container&)’
  309 | internal::ParamGenerator<typename Container::value_type> ValuesIn(
      |                                                          ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:309:58: note:   template argument deduction/substitution failed:
/usr/include/gtest/gtest-param-test.h: In substitution of ‘template<class Container> testing::internal::ParamGenerator<typename Container::value_type> testing::ValuesIn(const Container&) [with Container = std::generator<long unsigned int>]’:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:52:1:   required from here
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/include/gtest/gtest-param-test.h:309:58: error: no type named ‘value_type’ in ‘class std::generator<long unsigned int>’
  309 | internal::ParamGenerator<typename Container::value_type> ValuesIn(
      |                                                          ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:297:1: note: candidate: ‘template<class ForwardIterator> testing::internal::ParamGenerator<typename std::iterator_traits<_Iterator>::value_type> testing::ValuesIn(ForwardIterator, ForwardIterator)’
  297 | ValuesIn(ForwardIterator begin, ForwardIterator end) {
      | ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:297:1: note:   candidate expects 2 arguments, 1 provided
/usr/include/gtest/gtest-param-test.h:304:29: note: candidate: ‘template<class T, long unsigned int N> testing::internal::ParamGenerator<T> testing::ValuesIn(const T (&)[N])’
  304 | internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {
      |                             ^~~~~~~~
/usr/include/gtest/gtest-param-test.h:304:29: note:   template argument deduction/substitution failed:
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:64:70: note:   mismatched types ‘const T [N]’ and ‘std::generator<long unsigned int>’
   64 |                                                   ::testing::ValuesIn([]() -> generator<size_t>
      |                                                   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |                                                                                           {
      |                                                                                           ~
   66 |         co_yield 4;
      |         ~~~~~~~~~~~                                                   
   67 |         co_yield 10;
      |         ~~~~~~~~~~~~                                                  
   68 |         co_yield ranges::elements_of(ranges::iota_view(0, 10)); }())))
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
/usr/src/DataStructuresAlgorithms/test/RangeTests.cpp:52:1: error: template argument 1 is invalid
   52 | INSTANTIATE_TEST_SUITE_P(
lambda generator googletest c++23 c++-coroutine
1个回答
0
投票

testing::ValueIn
不接受
std::generator
因为:

  1. 缺乏
    value_type
    成员类型
    std::generator
  2. 缺乏常量的开始/结束。
    std::generator
    有这些函数的非常量版本——这没关系,因为这些函数会修改生成器状态
  3. begin 和 end 必须返回相同的类型——这是不正确的——end() 返回哨兵类型。

但是,更重要的是

::testing::ValuesIn
将尝试将整个输入范围复制到其内部向量 - 所以我们可以转换为向量 -> 所以使用额外的
 | std::ranges::to<std::vector>()

::testing::ValuesIn([]() -> std::generator<size_t>
        {
            co_yield 4;
            co_yield 10;
            co_yield std::ranges::elements_of(std::ranges::iota_view(0, 10)); 
        }() | std::ranges::to<std::vector>());

© www.soinside.com 2019 - 2024. All rights reserved.