vendredi 8 juillet 2016

Parameter being set to null using Google test on C++ while using Type Parameterizing the test

I am using Google test in C++ in Visual Studio. I have two Types, Type1 and Type2 and have a class, MyClass. I would like to save lines of code by type parameterizing my test cases, meaning I would like to write the same test case and have C++ test the different types.

In my code I have made the following declaration:

template <Class T>
MyClass<T> CreateMyClass();


template <>
MyClass<Type1> CreateMyClass(){
  MyClass x;
  x.setSomething(..);
   ...
  return x;
}

template <>
  MyClass<Type2> CreateMyClass(){
  MyClass x;
  x.setSomething(..);
   ...
  return x;
}


template <class T>
class MyUnitTest: public ::testing::Test {
    protected:
    MyUnitTest() : x(CreateCiphertext<T>(){}

  virtual void SetUp() {
}

  virtual void TearDown() {
  // Code here will be called immediately after each test
  // (right before the destructor).
}

  MyClass<T> x;
}

TYPED_TEST(MyUnitTest, test1){
   MyObject<TypeParam> x1(this->x);
   MyObject<TypeParam> x2(this->x);

   EXPECT_EQ(x1,x2);
}

Everything works fine for Type1, but when the second test case is ran with Type2, the x object returned from CreateMyClass is set to null (found through debugging).

On visual studio I get an SEH exception and a Segmentation fault on Linux.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire