BOOST_CHECK fails to compile operator<< for custom types

前端 未结 4 1394
温柔的废话
温柔的废话 2020-12-30 11:43

I wrote this really trivial class so that it\'s clear what my problem is:

class A
{
public:
    int x;
    A(int y) {x=y;}
    bool operator==(const A &o         


        
4条回答
  •  孤独总比滥情好
    2020-12-30 12:37

    Based on John Dibling's answer I was looking for a way to dump integer values in HEX rather than decimal I came up with this approach:

    // test_macros.h in my project
    namespace myproject
    {
    namespace test
    {
    namespace macros
    {
        extern bool hex;
    
        // context manager
        struct use_hex
        {
            use_hex()  { hex = true; }
            ~use_hex() { hex = false; }
        };
    
     }; // namespace
     }; // namespace
     }; // namespace
    
    namespace boost
    {
    namespace test_tools
    {
    
        // boost 1.56+ uses these methods
    
        template<>
        inline                                               
        void                                                 
        print_log_value::                       
        operator()(std::ostream & out, const uint64 & t) 
        {                                                    
            if(myproject::test::macros::hex)                    
                out << ::boost::format("0x%016X") % t;           
            else 
                out << t;                                        
        }                                                    
    
    namespace tt_detail
    {
    
        // Boost < 1.56 uses these methods
    
        template <>
        inline
        std::ostream &
        operator<<(std::ostream & ostr, print_helper_t const & ph )
        {
            if(myproject::test::macros::hex)
                return ostr << ::boost::format("0x%016X") % ph.m_t;
    
            return ostr << ph.m_t;
        }
    
    }; // namespace
    }; // namespace
    }; // namespace
    

    Now in my unit test case, I can turn on/off hex by setting the global static bool value, for example:

    for(uint64 i = 1; i <= 256/64; ++i)
    {
        if(i % 2 == 0) test::macros::hex = true;
        else           test::macros::hex = false;
        BOOST_CHECK_EQUAL(i+1, dst.pop());
    }
    

    And I get the behavior I was looking for:

    test_foobar.cc(106): error in "test_foobar_51": check i+1 == dst.pop() failed [2 != 257]
    test_foobar.cc(106): error in "test_foobar_51": check i+1 == dst.pop() failed [0x0000000000000003 != 0x0000000000000102]
    test_foobar.cc(106): error in "test_foobar_51": check i+1 == dst.pop() failed [4 != 259]
    test_foobar.cc(106): error in "test_foobar_51": check i+1 == dst.pop() failed [0x0000000000000005 != 0x0000000000000104]
    

    Alternatively, I can use the context manager:

    {
        test::macros::use_hex context;
    
        for(uint64 i = 1; i <= 4; ++i)
        {
            BOOST_CHECK_EQUAL(i + 0x200, i + 0x100);
        }
    }
    
    for(uint64 i = 1; i <= 4; ++i)
    {
        BOOST_CHECK_EQUAL(i + 0x200, i + 0x100);
    }
    

    And hex output will only be used in that block:

    test_foobar.cc(94): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [0x0000000000000201 != 0x0000000000000101]
    test_foobar.cc(94): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [0x0000000000000202 != 0x0000000000000102]
    test_foobar.cc(94): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [0x0000000000000203 != 0x0000000000000103]
    test_foobar.cc(94): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [0x0000000000000204 != 0x0000000000000104]
    test_foobar.cc(100): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [513 != 257]
    test_foobar.cc(100): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [514 != 258]
    test_foobar.cc(100): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [515 != 259]
    test_foobar.cc(100): error in "test_foobar_28": check i + 0x200 == i + 0x100 failed [516 != 260]
    

提交回复
热议问题