2013-12-11 74 views
0

我有一個和這個相關的問題:GTest fixture when constructor takes parameters?。我那個問題,我想知道如何設置一個GTest燈具,當受測試的類爲構造函數採用參數。我試圖複製閃電++而不是ARMA的答案,並且我失敗了。 任何線索?GoogleTest Fixture用於blitz ++類,在構造函數中帶參數

測試類:

#include <blitz/array.h> 
#include <vector> 

class TClass { 
    private: 
     std::vector<blitz::Array<double,2> * > mats; 
    public: 
     TClass(std::vector<blitz::Array<double,2> * > m_); 
     blitz::Array<double,2> * GetM(int which){ return(mats.at(which));}; 

}; 

TClass::TClass(std::vector<blitz::Array<double,2> * > m_){ 
    mats = m_; 
} 

測試:

#include <gtest/gtest.h> // Include the google test framework 
#include "TClass.cpp" 


class TClassTest : public ::testing::Test { 
protected: 
    int n; 
    int m; 
    std::vector<blitz::Array<double,2> * > M; 
    virtual void SetUp() { 
     n = 3; 
     m = 2; 
     blitz::Array<double,2> M1(n,m); 
     blitz::Array<double,2> M2(n,m); 
     M.push_back(&M1); 
     M.push_back(&M2); 
     T = new TClass(M); 
    } 

    virtual void TearDown() {delete T;} 

    TClass *T; 
}; 


TEST_F(TClassTest, CanGetM1){ 
    EXPECT_EQ(T->GetM(0), M.at(0)); 
} 

TEST_F(TClassTest, CanSeeN){ 
    EXPECT_EQ(3, n); 
} 

TEST_F(TClassTest, CanSeeM){ 
    EXPECT_EQ(3, (*M.at(0)).extent(blitz::firstDim)); 
} 

int main(int argc, char **argv) { 
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

最後的測試失敗,

TClassTest.cpp:43: Failure 
Value of: (*M.at(0)).extent(1) 
Actual: 32767 
Expected: 3 

即似乎M1未分配?或者它已經超出了範圍?

回答

1

設置完成之前,它已經超出了範圍。你可能想要:

class TClassTest : public ::testing::Test 
{ 
    protected: 
    int n; 
    int m; 
    std::vector<blitz::Array<double,2> * > M; 
    virtual void SetUp() { 
     n = 3; 
     m = 2; 
     M.push_back(new blitz::Array<double,2>(n,m)); 
     M.push_back(new blitz::Array<double,2>(n,m)); 
     T = new TClass(M); 
    } 
    virtual void TearDown() 
    { 
     delete T; 
     delete M[0]; 
     delete M[1]; 
    } 
    TClass *T; 
}; 

另一件事是你不應該包括cpp文件。將它們重命名爲.h.hpp

+0

哇 - 現貨。謝謝! –

相關問題