Opened 7 years ago

Closed 6 years ago

#12001 closed Bugs (obsolete)

Under C++11, BOOST_TEST() wrong on boost::optional + named-parameter-idiom example

Reported by: Tony Lewis <tonyelewis@…> Owned by: Gennadiy Rozental
Milestone: To Be Determined Component: test
Version: Boost 1.60.0 Severity: Problem
Keywords: test, optional, BOOST_TEST, BOOST_CHECK_EQUAL, c++11, named parameter idiom Cc:

Description

The example below shows the BOOST_TEST() assertions failing where they should pass, as the equivalent BOOST_CHECK_EQUAL() assertions do. It looks as though BOOST_TEST() is preventing the named-parameter-idiom setter from changing the optional value from boost::none.

Importantly, this code works if compiled without -std=c++11.

Compile commands:

g++ -std=c++11 -isystem $BOOST_ROOT/include test_test_bug.cpp -L$BOOST_ROOT/lib -lboost_unit_test_framework-mt
setenv LD_LIBRARY_PATH $BOOST_ROOT/lib
./a.out -l all

Code:

#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif
#include <boost/test/unit_test.hpp>

#include <boost/optional/optional.hpp>
#include <boost/optional/optional_io.hpp>

#include <iostream>

class my_class {
private:
        boost::optional<int> num;

public:
        my_class(const boost::optional<int> &arg_num) : num ( arg_num ) {}

        const boost::optional<int> & get_num() const {
                return num;
        }

        const my_class & set_num(const boost::optional<int> &arg_num) {
                num = arg_num;
                return *this;
        }
};

// Simple stream insertion operator
std::ostream & operator<<(std::ostream &os, const my_class &my_obj) {
        os << "my_class[ " << my_obj.get_num() << " ]";
        return os;
}

// Simple equality predicate operator
bool operator==(const my_class &my_obj_a, const my_class &my_obj_b) {
        return ( my_obj_a.get_num() == my_obj_b.get_num() );
}

BOOST_AUTO_TEST_CASE(my_test) {
        const my_class correct_answer( 5 );

        // These two work as expected
        BOOST_CHECK_EQUAL( my_class( boost::none ).set_num( 5 ),   correct_answer                       );
        BOOST_CHECK_EQUAL( correct_answer,                         my_class( boost::none ).set_num( 5 ) );

        // These two fail, evaluating the non-trivial side as "my_class[ -- ]"
        BOOST_TEST       ( my_class( boost::none ).set_num( 5 ) == correct_answer                       );
        BOOST_TEST       ( correct_answer                       == my_class( boost::none ).set_num( 5 ) );
}

bool init_function() { return true; }
int main( int argc, char* argv[] ) {
    return ::boost::unit_test::unit_test_main( &init_function, argc, argv );
}

Change History (3)

comment:1 by Raffi Enficiaud, 6 years ago

Hi,

I cannot reproduce on my setup (OSX, Xcode 6, C++11). I am running on boost 1.61, same example, the 2 BOOST_TEST assertions pass.

Last edited 6 years ago by Raffi Enficiaud (previous) (diff)

comment:2 by Tony Lewis <tonyelewis@…>, 6 years ago

Yes - I've just run the same test on 1.60.0 and 1.61.0 and it showed that the problem has new now been fixed in 1.61.0.

Thanks very much. Please feel free to close this ticket as fixed.

comment:3 by Raffi Enficiaud, 6 years ago

Resolution: obsolete
Status: newclosed

Thanks for the follow up. Closing the ticket now as "obsolete", although it is not clear where the fix comes from.

Note: See TracTickets for help on using tickets.