Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#11936 closed Bugs (duplicate)

BOOST_TEST() misbehaves with boost::optional under C++11

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

Description

The following code shows BOOST_TEST() unexpectedly failing (contrary to BOOST_CHECK_EQUAL() passing) when compiled under C++11.

#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:
        // Single data member
        boost::optional<int> num;

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

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

        // Setter
        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 );
}

I'm compiling and running with:

setenv BOOST_ROOT /opt/boost_1_60_0_gcc_build
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

Change History (3)

comment:1 by Michel Morin, 6 years ago

Component: Documentationtest
Owner: changed from Matias Capeletto to Gennadiy Rozental

comment:2 by Raffi Enficiaud, 6 years ago

Resolution: duplicate
Status: newclosed

Duplicates #12001

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

Thanks for your work that has led this to get fixed.

Note: See TracTickets for help on using tickets.