如何使用gtest测试多个模板参数的c ++模板类? [英] How to test c++ template class with multiple template parameters using gtest?

查看:1497
本文介绍了如何使用gtest测试多个模板参数的c ++模板类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用gtest测试一个模板类。我阅读了有关 TYPED_TEST samples \sample6_unittest.cc )他们引用。该示例中的模板只有一个模板参数。
但是,我的代码有两个模板参数,我该如何测试呢?

I want to test a template class with gtest. I read about TYPED_TESTs in gtest manual and looked at the official example (samples\sample6_unittest.cc) they reference. This template from the example has only one template parameter. But, my code has two template parameters, how can I test it?

我有以下代码:

// two element type
template <typename E, typename F>
class QueueNew
{
public:
    QueueNew() {}
    void Enqueue(const E& element) {}
    E* Dequeue() {}
    F size() const 
    {
        return (F)123;
    }
};

我写了下面的测试代码:

for which I wrote the test code below:

template <class E, class F>
QueueNew<E, F>* CreateQueue();

template <>
QueueNew<int, int>* CreateQueue<int, int>()
{
    return new QueueNew < int, int > ;
}
template <>
QueueNew<char, char>* CreateQueue<char, char>()
{
    return new QueueNew < char, char > ;
}

template <class E, class F>
class QueueTestNew;

template <class E>
class QueueTestNew<E, int> : public testing::Test
{
protected:
    QueueTestNew() : queue(CreateQueue<E, int>()){}
    virtual ~QueueTestNew(){ delete queue; }
    QueueNew<E, int>* const queue;
};

template <class E>
class QueueTestNew<char, E> : public testing::Test
{
protected:
    QueueTestNew() : queue(CreateQueue<char, E>()){}
    virtual ~QueueTestNew(){ delete queue; }
    QueueNew<char, E>* const queue;
};

// The list of types we want to test.
typedef ::testing::Types <char, int> Implementations;

TYPED_TEST_CASE(QueueTestNew, Implementations);

TYPED_TEST(QueueTestNew, DefaultConstructor)
{
    EXPECT_EQ(123u, this->queue->size());
}

但是在构建时,会出现错误:

but when building, I get the error:

error C2976: 'QueueTestNew' : too few template arguments
see declaration of 'QueueTestNew'
...

我认为我的测试模板方法with gtest是错误的,那么我该怎么办呢?

I think my test template method with gtest is wrong, so how should I do this?

推荐答案

一个窍门是让gtest看到一个单一的类型参数,有嵌套类型。为此,您可以定义模板结构,例如:

A trick would be to make gtest see a single type parameter, with nested types. To do this, you can define a templated structure such as:

template <typename A, typename B>
struct TypeDefinitions
{
  typedef typename A MyA;
  typedef typename B MyB;
};

您可以传递给您的测试夹具:

Which you can pass to your typed-test fixture:

template <class T>
class QueueTestNew : public testing::Test
{
protected:
  QueueTestNew() : queue(CreateQueue<typename T::MyA, typename T::MyB>()){}
  virtual ~QueueTestNew(){ delete queue; }
  QueueNew<typename T::MyA, typename T::MyB>* const queue;
};

// The list of types we want to test.
typedef ::testing::Types <TypeDefinitions<char,char>,
                          TypeDefinitions<int,int> > Implementations;

TYPED_TEST_CASE(QueueTestNew, Implementations);

TYPED_TEST(QueueTestNew, DefaultConstructor)
{
  typename TypeParam::MyA someA; // if you need access to the subtypes in the test itself

  EXPECT_EQ(123u, this->queue->size());
}

这篇关于如何使用gtest测试多个模板参数的c ++模板类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆