Question

I have a simple example class. It has one data member, which is a std::vector of pointers to armadillo matrices. the constructor takes such a vector as the only argument. here's file TClass.cpp:

#include <armadillo>
#include <vector>

class TClass {
    private: 
        std::vector<arma::mat * > mats;
    public:
        TClass(std::vector<arma::mat * > m_);
        arma::mat * GetM( int which ){ return( mats.at(which) );};

};

TClass::TClass(std::vector<arma::mat * > m_){
    mats = m_;
}

I want to construct a GTest fixture to test member function GetM. Here is what I have done:

#include <gtest/gtest.h> 
#include "TClass.cpp"


class TClassTest : public ::testing::Test {
 protected:
    int n;
    int m;
    std::vector<arma::mat * > M;

  virtual void SetUp() {
      n = 3;
      m = 2;
      arma::mat M1 =  arma::randu<arma::mat>(n,m);
      arma::mat M2 =  arma::randu<arma::mat>(n,m);
      M.push_back( &M1);
      M.push_back( &M2);
  }

  // virtual void TearDown() {}

  // initiate a TClass object
  TClass T(M);
};

// my test
TEST_F(TClassTest, CanGetM1){

    EXPECT_EQ( T.GetM(0), M.at(0) );

}

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

I compile this with g++ TClassTest.cpp -o tclass -larmadillo. It tells me that TClassTest.cpp:24: error: ‘M’ is not a type. I dont' understand why I cannot construct the TClass object in the fixture definition?

Was it helpful?

Solution

The object T cannot be initialized in the declaration of class TClassTest. Have you been writing Java lately? ;-)

To initialize it, you can do something like this:

class TClassTest : public ::testing::Test {
  // ... (rest of code is fine as is)
  virtual void SetUp() {
    // ...
    T = new TClass(M);
  }

  virtual void TearDown() { delete T; }

  TClass *T;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top