断言迭代器在 CppUnit 中相等
Asserting iterators equality in CppUnit
我已经开始使用 CppUnit 库了。一切正常,但现在,我陷入了使用 CPPUNIT_ASSERT_EQUAL
断言迭代器的困境。所以有我的代码:
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
}
当我运行此测试时,我收到以下错误。
/opt/local/include/cppunit/TestAssert.h:49:13:
Invalid operands to binary expression
('OStringStream' (aka 'basic_ostringstream<char>')
and 'const std::_List_iterator<int>')
如果我用迭代器注释该行,那么它可以毫无问题地编译。那我做错了什么呢?我应该如何断言两个迭代器的相等性?顺便说一下,我使用 xcode 4.4。
请参阅TestAssert.h
中的CPPUNIT_ASSERT_EQUAL
宏文档:
#define CPPUNIT_ASSERT_EQUAL(expected,actual)
对expected
和actual
参数的要求:
- 它们完全属于同一类型
- 它们可以使用运算符序列化为 std::strstream <<
- 可以使用运算符 == 来比较它们。
最后两个要求(序列化和比较)可以通过专门CppUnit::assertion_traits
来删除。
因此,问题的根本原因是std::list::iterator
无法序列化为std::strstream
。您需要按照文档描述为其编写自己的CppUnit::assertion_traits
专用化,或者只是避免CPPUNIT_ASSERT_EQUAL
并使用CPPUNIT_ASSERT
:
CPPUNIT_ASSERT(lst.begin() == lst.end());
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 断言迭代器在 CppUnit 中相等