Boost logo

Boost Users :

From: Ananda Tallur (anand_at_[hidden])
Date: 2006-02-24 11:17:52


Hello,

I would like to submit a strange problem we recently discovered, when
using boost_unit_test.
This problem only occurs : on Mac OS X, when using gcc 4 with any
optimization level other than 0 (-O, -O1, -O2, etc...).
  I have tested with gcc 4.0.1 (build 5250 coming with Apple XCode
2.2.1), and also gcc 4.0.2 delivered by darwinports, and the problem
is the same.
On the other hand, it works perfectly on Linux, with gcc 4.0.2 and
optimizations.

The problem :
it occurs when using BOOST_CLASS_TEST_CASE.
BOOST_CLASS_TEST_CASE executes a test method of a class/struct
instance, so that the class/struct attributes can be used in the
test. With a very simple class, named SampleTest, having only
-> one int attribute "a"
-> a single test function : void sampleTest (), which only displays
value of "a"
-> a test declared in boost test suite : BOOST_CLASS_TEST_CASE ( &
SampleTest::sampleTest, instance), with instance being shared pointer
to a instance of sampleTest, instanciated with a given value of "a"
passed as constructor argument.

It does not work on Mac OS X, with the conditions described above.
Everything happens as if BOOST_CLASS_TEST_CASE was not using the
correct address in memory for accessing the instance attributes data.
Furthermore, the value displayed for a is wrong, but always the same,
it is not random at all, which seems to mean that there is a shift in
the memory adress being accessed.

Below is the complete sample program, for reproducing the problem :

---- file boost_unit_test_gcc4_pb_sample.cpp ---
#include <iostream>
#include <boost/test/unit_test.hpp>
using boost::unit_test_framework::test_suite;
using boost::unit_test_framework::test_case;
using namespace std;

class SampleTest {
public:
    SampleTest (int val) :
       a (val)
    {
    }
    int a;

    void sampleTest ()
    {
       std::cout << "a : " << a << std::endl;
       BOOST_CHECK_EQUAL (a, 12345);
    }
};

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    test_suite* test= BOOST_TEST_SUITE( "Sample test" );
    boost::shared_ptr<SampleTest> instance (new SampleTest (12345));
    test->add (BOOST_CLASS_TEST_CASE ( & SampleTest::sampleTest,
instance));
    return test;
}
---- end-of-file boost_unit_test_gcc4_pb_sample.cpp ---

* Command used for compiling :
BOOST_PATH=<path-to-your-boost-install>
  /usr/bin/g++-4.0 -O2 -I${BOOST_PATH}/include -L${BOOST_PATH}/lib -
lboost_unit_test_framework boost_unit_test_gcc4_pb_sample.cpp -o
boost_unit_test_gcc4_pb_sample

When compiled when the above command, and running the program on Mac
OS X :
export DYLD_LIBRARY_PATH=${BOOST_PATH}/lib
./boost_unit_test_gcc4_pb_sample

The result is the following :

----
Running 1 test case...a : 13691
boost_unit_test_gcc4_pb_sample.cpp(18): error in "  
SampleTest::sampleTest": check a == 12345 failed [13691 != 12345]
*** 1 failure detected in test suite "Sample test"
---
We see that value of "a" is wrong.
When compiled without optimization (-O0), and running, the result is :
----
Running 1 test case...a : 12345
*** No errors detected
----
Everything is right in this case.
Has anyone some experience using Boost unit_test framework on Mac OS  
X, using gcc 4, and already experienced this kind of problems ?
Any help would be greatly appreciated !
Regards,
Anand

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net