本文介绍了适用于blitz ++类的GoogleTest Fixture,在构造函数中带有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此相关的问题:构造函数使用时的GTest固定装置参数?.我想知道的这个问题是,当被测类为构造函数使用参数时,如何设置GTest固定装置.我试图复制blitz ++而不是arma的答案,但失败了.有任何线索吗?

I have got a question that is related to this one: GTest fixture when constructor takes parameters?. I that question I wanted to know how to set up a GTest fixture when the tested class takes a parameter for the constructor. I tried to replicate the answer for blitz++ instead of arma, and I fail.Any clues?

测试类:

#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?还是超出范围?

i.e. it seems that M1 is not allocated? Or it's gone out of scope?

推荐答案

在SetUp完成之前,它已超出范围.您可能想要:

It's gone out of scope, just before SetUp is finished. You probably want:

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.

Another thing is that you are not supposed to include cpp files. Rename them to .h or .hpp.

这篇关于适用于blitz ++类的GoogleTest Fixture,在构造函数中带有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:22