gtest如何比较嵌套容器

时间:2019-04-19 08:32:54

标签: c++ googletest

我遇到一个关于比较嵌套向量的ut。

在旧代码中,ut代码如下:

EXPECT_THAT(resources[1].cookies, ElementsAre(cookie{0}, cookie{1}, 
    cookie{constant::invalid_cookie}));

cookies是一个向量,该cookie具有一个int id成员,例如下面的伪代码:

struct cookie
{
    int id;
};

在新代码中,我必须在struct cookie中添加另一个向量; 成功。但valgrind检查失败。失败如下:

==32660== Use of uninitialised value of size 8
==32660==    at 0x5943BE1: _itoa_word (_itoa.c:180)
==32660==    by 0x59483AB: vfprintf (vfprintf.c:1636)
==32660==    by 0x596F4CF: vsnprintf (vsnprintf.c:114)
==32660==    by 0x594EFBE: snprintf (snprintf.c:33)
==32660==    by 0x21C5BE: testing::(anonymous namespace)::PrintByteSegmentInObjectTo(unsigned char const*, unsigned long, unsigned long, std::ostream*) (in /home/cilom_C_PL/slaves_workspace/x86_64-0/valgrind-coverage/build/build_cov_mcu/CP_TraceController/Test/ut_stop_call_handlers)
==32660==    by 0x21C647: testing::(anonymous namespace)::PrintBytesInObjectToImpl(unsigned char const*, unsigned long, std::ostream*) (in /home/cilom_C_PL/slaves_workspace/x86_64-0/valgrind-coverage/build/build_cov_mcu/CP_TraceController/Test/ut_stop_call_handlers)
==32660==    by 0x21C6E9: testing::internal2::PrintBytesInObjectTo(unsigned char const*, unsigned long, std::ostream*) (in /home/cilom_C_PL/slaves_workspace/x86_64-0/valgrind-coverage/build/build_cov_mcu/CP_TraceController/Test/ut_stop_call_handlers)
==32660==    by 0x19D881: testing::internal2::TypeWithoutFormatter<tc::master::detail::cookie, (testing::internal2::TypeKind)2>::PrintValue(tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:140)
==32660==    by 0x19D624: std::basic_ostream<char, std::char_traits<char> >& testing::internal2::operator<< <char, std::char_traits<char>, tc::master::detail::cookie>(std::basic_ostream<char, std::char_traits<char> >&, tc::master::detail::cookie const&) (gtest-printers.h:209)
==32660==    by 0x19D128: void testing_internal::DefaultPrintNonContainerTo<tc::master::detail::cookie>(tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:250)
==32660==    by 0x19C082: void testing::internal::DefaultPrintTo<tc::master::detail::cookie>(testing::internal::WrapPrinterType<(testing::internal::DefaultPrinterType)3>, tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:445)
==32660==    by 0x19AA11: void testing::internal::PrintTo<tc::master::detail::cookie>(tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:482)
==32660==    by 0x199698: testing::internal::UniversalPrinter<tc::master::detail::cookie>::Print(tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:724)
==32660==    by 0x1977E9: void testing::internal::UniversalPrint<tc::master::detail::cookie>(tc::master::detail::cookie const&, std::ostream*) (gtest-printers.h:873)
==32660==    by 0x194BE3: void testing::internal::DefaultPrintTo<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > >(testing::internal::WrapPrinterType<(testing::internal::DefaultPrinterType)0>, std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&, std::ostream*) (gtest-printers.h:396)
==32660==    by 0x191357: void testing::internal::PrintTo<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > >(std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&, std::ostream*) (gtest-printers.h:482)
==32660==    by 0x18B8A0: testing::internal::UniversalPrinter<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > >::Print(std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&, std::ostream*) (gtest-printers.h:724)
==32660==    by 0x184771: void testing::internal::UniversalPrint<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > >(std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&, std::ostream*) (gtest-printers.h:873)
==32660==    by 0x17C502: bool testing::internal::MatchPrintAndExplain<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const, std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&>(std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&, testing::Matcher<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&> const&, testing::MatchResultListener*) (gmock-matchers.h:712)
==32660==    by 0x174C9F: testing::AssertionResult testing::internal::PredicateFormatterFromMatcher<testing::internal::ElementsAreMatcher<std::tuple<tc::master::detail::cookie, tc::master::detail::cookie, tc::master::detail::cookie> > >::operator()<std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > >(char const*, std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> > const&) const (gmock-matchers.h:1856)
==32660==  Uninitialised value was created by a heap allocation
==32660==    at 0x4C2B25F: operator new(unsigned long) (vg_replace_malloc.c:334)
==32660==    by 0x192C9D: __gnu_cxx::new_allocator<tc::master::detail::cookie>::allocate(unsigned long, void const*) (new_allocator.h:111)
==32660==    by 0x18E8CE: std::allocator_traits<std::allocator<tc::master::detail::cookie> >::allocate(std::allocator<tc::master::detail::cookie>&, unsigned long) (alloc_traits.h:436)
==32660==    by 0x187849: std::_Vector_base<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> >::_M_allocate(unsigned long) (stl_vector.h:172)
==32660==    by 0x17E33E: std::_Vector_base<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> >::_M_create_storage(unsigned long) (stl_vector.h:187)
==32660==    by 0x176E30: std::_Vector_base<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> >::_Vector_base(unsigned long, std::allocator<tc::master::detail::cookie> const&) (stl_vector.h:138)
==32660==    by 0x16F1AF: std::vector<tc::master::detail::cookie, std::allocator<tc::master::detail::cookie> >::vector(unsigned long, std::allocator<tc::master::detail::cookie> const&) (stl_vector.h:284)
==32660==    by 0x16991B: 

进行以下研究: 从错误打印中可以看出,gtest似乎将cookie的嵌套向量作为一个8大小的指针。

  1. 首先认为ElementsAre是不正确的,可能是由cookie的构造器引起的,我向其中添加了vector.clear(),同样失败了。
  2. 我的一位同事,告诉我另一种方法,代码更改如下:

    std :: vector test2; test2.push_back(cookie(0)); test2.push_back(cookie(constant :: invalid_cookie)); test2.push_back(cookie(1)); EXPECT_THAT(resources [0] .cookies,:: testing :: ContainerEq(test2));

也失败了。

文章中已有代码。

gtest中有任何匹配器可以适用于这种情况。

0 个答案:

没有答案