Can't Test Iterator with EXPECT_EQ in VC++ 2008

625 views
Skip to first unread message

David Wallace

unread,
Jul 28, 2011, 7:00:04 PM7/28/11
to Google C++ Testing Framework
Using googletest 1.6.0 with Visual C++ 08, the following test works
fine if I comment out the last line:
TEST_F(ComparePaths, OrderPaths) {
set<combinational_path> paths;
paths.insert(cp_1_2_10);
paths.insert(cp_1_2_11);
paths.insert(cp_1_2_9);
paths.insert(cp_1_3_10);
paths.insert(cp_2_2_10);
paths.insert(cp_3_1_10);
EXPECT_EQ(6, paths.size());
set<combinational_path>::iterator pi = paths.begin();
set<combinational_path>::iterator pi_end = paths.end();

EXPECT_EQ(cp_1_2_9, *(pi++));
EXPECT_EQ(cp_1_2_10, *(pi++));
EXPECT_EQ(cp_1_3_10, *(pi++));
EXPECT_EQ(cp_2_2_10, *(pi++));
EXPECT_EQ(cp_3_1_10, *(pi++));
EXPECT_EQ(cp_1_2_11, *(pi++));
bool at_end = (pi == pi_end);
EXPECT_TRUE(at_end);
// Everything is fine up to this point:
EXPECT_EQ(pi_end, pi);
}
but blows up with a bunch of compile errors if I compile with the last
EXPECT_EQ enabled. The errors are in gtest-printers.h,
DefaultPrintTo. The combinational_path class has operator<,
operator== and the ostream output operator<< defined, but no PrintTo
method.

Here are the error messages:
1>c:\users\dave\my documents\visual studio 2008\projects\paths\coreapp
\googletest\include\gtest\gtest-printers.h(275) : error C2039:
'begin' : is not a member of 'std::_Tree<_Traits>::iterator'
1> with
1> [
1>
_Traits=std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include
\xtree(426) : see declaration of 'std::_Tree<_Traits>::iterator'
1> with
1> [
1>
_Traits=std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(376) : see
reference to function template instantiation 'void
testing::internal::DefaultPrintTo<T>(testing::internal::IsContainer,testing::internal::false_type,const
C &,std::ostream *)' being compiled
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator,
1>
C=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(600) : see
reference to function template instantiation 'void
testing::internal::PrintTo<T>(const T &,std::ostream *)' being
compiled
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(591) : while
compiling class template member function 'void
testing::internal::UniversalPrinter<T>::Print(const T &,std::ostream
*)'
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(697) : see
reference to class template instantiation
'testing::internal::UniversalPrinter<T>' being compiled
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(678) : see
reference to function template instantiation 'void
testing::internal::UniversalPrint<T>(const T &,std::ostream *)' being
compiled
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest-printers.h(790) : see
reference to function template instantiation 'void
testing::internal::UniversalTersePrint<T>(const T &,std::ostream *)'
being compiled
1> with
1> [
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest.h(1301) : see reference to
function template instantiation 'std::string
testing::PrintToString<T1>(const T &)' being compiled
1> with
1> [
1>
T1=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator,
1>
T=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest.h(1326) : see reference to
function template instantiation 'testing::internal::String
testing::internal::FormatForComparisonFailureMessage<T1,T2>(const T1
&,const T2 &)' being compiled
1> with
1> [
1>
T1=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator,
1>
T2=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\googletest\include\gtest\gtest.h(1353) : see reference to
function template instantiation 'testing::AssertionResult
testing::internal::CmpHelperEQ<T1,T2>(const char *,const char *,const
T1 &,const T2 &)' being compiled
1> with
1> [
1>
T1=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator,
1>
T2=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1> c:\users\dave\my documents\visual studio 2008\projects\paths
\coreapp\checker\testlongestpaths\test_longest_paths.cpp(135) : see
reference to function template instantiation 'testing::AssertionResult
testing::internal::EqHelper<lhs_is_null_literal>::Compare<std::_Tree<_Traits>::iterator,std::_Tree<_Traits>::iterator>(const
char *,const char *,const T1 &,const T2 &)' being compiled
1> with
1> [
1> lhs_is_null_literal=false,
1>
_Traits=std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>,
1>
T1=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator,
1>
T2=std::_Tree<std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>>::iterator
1> ]
1>c:\users\dave\my documents\visual studio 2008\projects\paths\coreapp
\googletest\include\gtest\gtest-printers.h(276) : error C2039: 'end' :
is not a member of 'std::_Tree<_Traits>::iterator'
1> with
1> [
1>
_Traits=std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include
\xtree(426) : see declaration of 'std::_Tree<_Traits>::iterator'
1> with
1> [
1>
_Traits=std::_Tset_traits<combinational_path,std::less<combinational_path>,std::allocator<combinational_path>,false>
1> ]

Vlad Losev

unread,
Aug 4, 2011, 7:03:13 AM8/4/11
to David Wallace, Google C++ Testing Framework
Hi David -

The problem is Google Test trying to print iterators you are comparing. In this case it fails to detect the type of the object correctly and tries to treat the iterator as a container. But printing iterators is inherently tricky: given a particular iterator, you never know whether it is dereferenceable or not.

My advice is to refrain from comparing iterators using EXPECT_EQ and friends. Instead, use something like this:

EXPECT_TRUE(it == container.end()) << "it is not beyond the end of the container".
Reply all
Reply to author
Forward
0 new messages