簡體   English   中英

加速測試設置錯誤:內存訪問沖突

[英]Boost test setup error: memory access violation

編譯並運行以下文件后,在運行可執行文件時出現上述錯誤。

#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上的內存訪問沖突:錯誤地址處沒有映射

我過去兩天一直在努力找出我的問題

代碼中有許多令人不安的事情,並且在發布問題時似乎必須丟失某些格式,否則就沒有機會對其進行編譯。 (例如, }' ?!)

對於初學者,您不應該將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
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM