Boost test setup error: memory access violation

2

I am getting the above error while running the executable after compiling and running the following file.

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>
#include "index/DatabaseGroup.hpp"

using namespace boost::unit_test;

namespace indexing {

class ForwardDBTest {

    /// A pointer to the database group object.
    DatabaseGroup& databaseGroup;

    std::string databaseName;
public:

    ~ForwardDBTest() {
    }
    ;

    ForwardDBTest(DatabaseGroup& databaseGroup_, std::string dbName) :
            databaseGroup(databaseGroup_), databaseName(dbName) {
    }

    void boostTestCreateDB() {
        databaseGroup.createDatabase(databaseName, databaseName);
    }

};

class testSuites: public test_suite {
public:
    testSuites() :
            test_suite("test_suite") {
        std::string db_location = "home/girijag/ripe/ripe_db";
        std::cout << "hello" << std::endl;
        int concurrency = 0;
        std::string db_cache_policy = "AllMem";
        boost::shared_ptr<DatabaseGroup> db = boost::shared_ptr<DatabaseGroup>(
                new DatabaseGroup(db_location, concurrency, db_cache_policy));
        std::string dbName = "DB1";
        boost::shared_ptr<ForwardDBTest> instance(
                new ForwardDBTest(*db, dbName));
        test_case* boostTestCreateDB_test_case = BOOST_CLASS_TEST_CASE(
                &ForwardDBTest::boostTestCreateDB, instance);
        add(boostTestCreateDB_test_case);
    }

    ~testSuites() {
    }
    ;

};

test_suite* init_unit_test_suite(int argc, char** argv) {

    test_suite* suite(BOOST_TEST_SUITE("Master Suite"));
    suite->add(new testSuites());
    return suite;
}

}'

Please let me know how should i resolve this? i am getting errors as below:-

Test setup error: memory access violation at address: 0x00000021: no mapping at fault address

I have been struggling from past 2 days to figure out whats my issue

c++
boost
boost-test
asked on Stack Overflow Feb 21, 2014 by user2772739 • edited Feb 25, 2014 by mockinterface

1 Answer

1

There are a number of disturbing things in the code, and some formatting seems have to been lost when posting the question, otherwise there is no chance it compiles. (For example, }’ ?!)

For starters, you shouldn’t place init_unit_test_suite(int, char**) in the indexing namespace, and subsequently there is no point in defining BOOST_TEST_MAIN - you will end up with multiple definition of the said init_unit_test_suite(int, char**) method.

In your case, the suite should be simply registered in the master test suite, there is no need to return a pointer to it from the method.

Here’s a minimal example that you can work with an extend for your purpose. It follows your structure, but omits non-relevant details:

#include <boost/test/included/unit_test.hpp>
#include <iostream>

using namespace boost::unit_test;

namespace indexing {

class ForwardDBTest { 
public:
    void boostTestCreateDB() { std::cout << __FUNCTION__ << std::endl; }
};

class TestSuite : public test_suite {
public:
    TestSuite() : test_suite("test_suite") {
        boost::shared_ptr<ForwardDBTest> instance(new ForwardDBTest);
        add(BOOST_CLASS_TEST_CASE(&ForwardDBTest::boostTestCreateDB, instance));
    }
};

} // namespace indexing

test_suite* init_unit_test_suite(int, char**) {
    framework::master_test_suite().add(new indexing::TestSuite);
    return 0;
}
/* Output:
Running 1 test case...
boostTestCreateDB

*** No errors detected
*/
answered on Stack Overflow Feb 25, 2014 by mockinterface

User contributions licensed under CC BY-SA 3.0