【问题标题】:How to pretty print STL data structures in CppUnit?如何在 CppUnit 中漂亮地打印 STL 数据结构?
【发布时间】:2012-05-24 03:50:06
【问题描述】:

我正在尝试使用以下代码漂亮地打印 STL 数据结构以检查 CppUnit 测试结果

#include <iostream>
#include <utility>
#include <map>
#include <sstream>
template<class S,class T>
std::ostream& operator<<(std::ostream & o, const std::pair<S,T> & p){
      return o << "(" << p.first << ", " << p.second << ")";
}

template<class K, class V>  
std::ostream& operator<<(std::ostream& o, const std::map<K,V> & m){ 
    o << "{ ";
    for(auto it=m.begin();it!=m.end();it++){ 
       o << "{" << it->first <<": " << it->second << "} ";
    }   
    o << " }";
    return o;
}    

但是当我在一些测试中使用这个包含文件时,比如

auto a = std::map<int,int>();
auto b = std::map<int,int>();
CPPUNIT_ASSERT_EQUAL(a,b);

我收到以下错误:

In file included from /usr/include/cppunit/TestCase.h:6:0,
                 from /usr/include/cppunit/TestCaller.h:5,
                 from /usr/include/cppunit/extensions/HelperMacros.h:9,
                 from /home/tcebrian/GIT/compress-sn/test/GraphTest.cpp:1:
/usr/include/cppunit/TestAssert.h: In static member function 'static std::string CppUnit::assertion_traits<T>::toString(const T&) [with T = std::map<int, int>, std::string = std::basic_string<char>]':
/usr/include/cppunit/TestAssert.h:101:5:   instantiated from 'void CppUnit::assertEquals(const T&, const T&, CppUnit::SourceLine, const string&) [with T = std::map<int, int>, std::string = std::basic_string<char>]'
/home/tcebrian/GIT/compress-sn/test/GraphTest.cpp:44:9:   instantiated from here
/usr/include/cppunit/TestAssert.h:49:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
/usr/include/c++/4.6/ostream:581:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::map<int, int>]'
make[3]: *** [CMakeFiles/UnitTester.dir/test/GraphTest.cpp.o] Error 1
make[3]: Leaving directory `/home/tcebrian/GIT/compress-sn/dist/Debug'
make[2]: *** [CMakeFiles/UnitTester.dir/all] Error 2
make[2]: Leaving directory `/home/tcebrian/GIT/compress-sn/dist/Debug'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/tcebrian/GIT/compress-sn/dist/Debug'
make: *** [debug] Error 2

但代码在这个简单的 sn-p 中完全有效:

#include <TestUtils.hpp>
#include <map>
#include <utility>
int main(int argc, const char *argv[])
{
    std::cout << std::pair<int,int>() << std::endl;
    std::cout << std::map<int,int>() << std::endl;
    return 0;
}

CppUnit 做了什么来阻止漂亮地打印数据结构?您如何使用 CppUnit 进行漂亮的打印?

【问题讨论】:

标签: c++ stl iostream cppunit


【解决方案1】:

我不确定你为什么会收到那个特定的错误(它认为他们对 TestAssert.h 所示。像这样的:

namespace CppUnit{
    template<class X, class Y>
    struct assertion_traits<std::pair<X,Y>>{
        static bool equal(const std::pair<X,Y> &a, const std::pair<X,Y> &b){
            return a == b;
        }
        static std::string toString(const std::pair<X,Y> &p){
            std::ostringstream o;
            o << "(" << p.first << ", " << p.second << ")";
            return o.str();
        }
    };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2019-10-08
    • 2017-12-31
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多