Boost测试设置错误:内存访问冲突

时间:2014-02-21 12:11:22

标签: c++ boost boost-test

在编译并运行以下文件后运行可执行文件时出现上述错误。

#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;
}

}'

请让我知道我该如何解决这个问题? 我收到的错误如下: -

  

测试设置错误:地址处的内存访问冲突:0x00000021:故障地址没有映射

过去2天我一直在努力弄清楚我的问题是什么

1 个答案:

答案 0 :(得分:1)

代码中有许多令人不安的东西,在发布问题时似乎必须丢失一些格式,否则它就没有机会编译。 (例如,}’?!)

对于初学者,您不应将init_unit_test_suite(int, char**)放在索引命名空间中,然后定义BOOST_TEST_MAIN没有意义 - 您最终会得到多个定义上述init_unit_test_suite(int, char**)方法。

在您的情况下,套件应该只是在主测试套件中注册,不需要从方法返回指向它的指针。

这是一个可以为您的目的使用扩展的最小示例。它遵循您的结构,但省略了不相关的细节:

#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
*/