I’ve been starting to use CppUnit library. And everything worked fine, but now, I get stuck with asserting iterators using CPPUNIT_ASSERT_EQUAL. So there is my code:
void TestingClass::test_adjacent_find()
{
// Set up
int a [5] = {1,2,3,3,5};
int b [5] = {1,2,3,4,5};
int c [1] = {1};
std::list<int> lst;
lst.push_back(1);
lst.push_back(1);
lst.push_back(5);
// Check
CPPUNIT_ASSERT_EQUAL(a+2, my_adjacent_find(a , a+5, pred_eq<int>));
CPPUNIT_ASSERT_EQUAL(b+5, my_adjacent_find(b, b+5, pred_eq<int>));
CPPUNIT_ASSERT_EQUAL(c+1, my_adjacent_find(c, c+1, pred_eq<int>));
CPPUNIT_ASSERT_EQUAL(lst.begin(), lst.end()); // problem is here
}
When I’m running this test I’m getting the error below.
/opt/local/include/cppunit/TestAssert.h:49:13:
Invalid operands to binary expression
('OStringStream' (aka 'basic_ostringstream<char>')
and 'const std::_List_iterator<int>')
If I comment the line with iterators, then it compiles without any problems. So what am I doing wrong? And how should I assert equality of two iterators? By the way, I use xcode 4.4.
See
CPPUNIT_ASSERT_EQUALmacro documentation inTestAssert.h:#define CPPUNIT_ASSERT_EQUAL(expected,actual)Requirement for
expectedandactualparameters:The last two requirements (serialization and comparison) can be removed by specializing the
CppUnit::assertion_traits.So the root cause of your problem is that
std::list::iteratorcould not be serialized tostd::strstream. You need to write your ownCppUnit::assertion_traitsspecialization for it as documentation describes or just avoidCPPUNIT_ASSERT_EQUALand useCPPUNIT_ASSERT: