Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

BOOST_level_EQUAL

BOOST_EQUAL(lhs, rhs) compares the two arguments with operator==. The diagnostic message is constructed with operator<< on the two arguments.

[Important] Important

If you get a strange compile error when using BOOST_EQUAL, check that the appropriate operator== and operator<< are defined for the arguments. If you define these operators in your test code, keep in mind argument-dependent lookup of types based on enclosing namespaces and the namespaces opened by test suites. The need to define operator<< can be eliminated by using BOOST_TEST_DONT_PRINT_LOG_VALUE.

Examples

BOOST_AUTO_TEST_CASE(example_equal)
{
    // integral types
    unsigned const u = 1u;
    int const i = -1;
    char const A = 'A';
    // standard library types providing operator== and operator<<
    std::string const s = "scooby";

    BOOST_REQUIRE_EQUAL(1u, u);
    BOOST_REQUIRE_EQUAL(-1, i);
    BOOST_REQUIRE_EQUAL('A', A);
    BOOST_REQUIRE_EQUAL("scooby", s);
}

// custom data structure
struct symbol
{
    char const* const name;
    int value;
};

// custom comparison operator
static bool operator==(symbol const& lhs, symbol const& rhs)
{
    return lhs.value == rhs.value
        && std::string(lhs.name) == std::string(rhs.name);
}

// custom stream insertion operator
static std::ostream& operator<<(std::ostream& stream, symbol const& value)
{
    return stream << value.name << ": " << value.value;
}

BOOST_AUTO_TEST_CASE(example_equal_custom_compare)
{
    symbol const s1 = { "var" , 1 };
    symbol const s2 = { "var", 1 };
    // If the compiler doesn't collapse multiple constants, then:
    // s1.name != s2.name;

    BOOST_REQUIRE_EQUAL(s1, s2);
}


PrevUpHomeNext