Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

C-string comparison

In the general case, pointers are compared using their value. However when type of the the pointers are char* or wchar_t*, BOOST_TEST promotes them as null terminated char arrays and string comparison is used instead.

Example: BOOST_TEST string comparison

Code

#define BOOST_TEST_MODULE boost_test_strings
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_CASE( test_pointers )
{
  float a(0.5f), b(0.5f);
  const float* pa = &a, *pb = &b;
  BOOST_TEST(a == b);
  BOOST_TEST(pa == pb);
}

BOOST_AUTO_TEST_CASE( test_strings )
{
  const char* a = "test1";
  const char* b = "test2";
  const char* c = "test1";
  BOOST_TEST(a == b);
  BOOST_TEST(a == c);
}

Output

> ./boost_test_strings --log_level=all
Running 2 test cases...
Entering test module "boost_test_strings"
test.cpp(15): Entering test case "test_pointers"
test.cpp(19): info: check a == b has passed
test.cpp(20): error: in "test_pointers": check pa == pb has failed [0x7fff51cdcc04 != 0x7fff51cdcc00]
test.cpp(15): Leaving test case "test_pointers"; testing time: 306us
test.cpp(23): Entering test case "test_strings"
test.cpp(28): error: in "test_strings": check a == b has failed [test1 != test2]
test.cpp(29): info: check a == c has passed
test.cpp(23): Leaving test case "test_strings"; testing time: 198us
Leaving test module "boost_test_strings"; testing time: 702us

*** 2 failures are detected in the test module "boost_test_strings"

PrevUpHomeNext