2014-02-21 32 views
2

編譯並運行以下文件後,運行可執行文件時出現上述錯誤。升壓測試設置錯誤:內存訪問衝突

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

}' 

請讓我知道我應該怎麼解決呢? 我得到的錯誤如下: -

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

我已經從最近兩天在努力搞清楚什麼我的問題

回答

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