gtest 学习之四 testing::Test

本文解析了gtest框架的第三个示例程序,详细介绍了测试类的结构及其如何使用SetUp和TearDown方法进行测试前后的准备与清理工作。此外,还深入分析了一个自定义队列类的实现,并展示了如何通过编写测试用例来验证该队列类的功能。

本例是gtest中的第三个demo,有一个测试类,这个测试类是从testing::Test中继承而来,在测试类中有一个SetUp和一个TearDown方法,这两个事件是gtest中提供的事件,通过虚函数可以在继承类中重写这两个方法,这两个事件属于玩转gtest测试框架的事件机制中的TestCase事件。

头文件sample3.h定义:

#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
#define GTEST_SAMPLES_SAMPLE3_INL_H_

#include <stddef.h>


// Queue 是一个单向链表的简单实现.
//
// The element 必须支持拷贝构造.
template <typename E>  // E is the element type
class Queue;

// QueueNode 是组成 Queue的一个节点
// type E and a pointer to the next node.
template <typename E>  // E is the element type
class QueueNode {
    friend class Queue<E>;

public:
    // 获取本节点的 element.
    const E& element() const { return element_; }

    // 获取下一个节点.
    QueueNode* next() { return next_; }
    const QueueNode* next() const { return next_; }

private:
    // 用给定的element创建一个节点,next pointer 被设定为空
    QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}

    // We disable the default assignment operator and copy c'tor.
    const QueueNode& operator = (const QueueNode&);
    QueueNode(const QueueNode&);

    E element_;
    QueueNode* next_;
};

template <typename E>  // E is the element type.
class Queue {
public:

    // 创建一个空队列.
    Queue() : head_(NULL), last_(NULL), size_(0) {}

    // 析构
    ~Queue() { Clear(); }

    // 清理队列.
    void Clear() {
        if (size_ > 0) {
            // 1. 删除所有节点.
            QueueNode<E>* node = head_;
            QueueNode<E>* next = node->next();
            for (; ;) {
                delete node;
                node = next;
                if (node == NULL) break;
                next = node->next();
            }

            // 2. Resets the member variables.
            head_ = last_ = NULL;
            size_ = 0;
        }
    }

    // 获取元素数量.
    size_t Size() const { return size_; }

    // 获取队列第一个元素,当队列为空时返回null.
    QueueNode<E>* Head() { return head_; }
    const QueueNode<E>* Head() const { return head_; }

    // 获取队列最后一个元素,当队列为空时返回null.
    QueueNode<E>* Last() { return last_; }
    const QueueNode<E>* Last() const { return last_; }

    // 在队列的尾部加入一个元素,用element的构造函数创建一个element并存贮在队列的尾部
    void Enqueue(const E& element) {
        QueueNode<E>* new_node = new QueueNode<E>(element);

        if (size_ == 0) {
            head_ = last_ = new_node;
            size_ = 1;
        } else {
            last_->next_ = new_node;
            last_ = new_node;
            size_++;
        }
    }

    // 从头部移除一个元素,并将这个元素返加,队列为空时返Null. 
    E* Dequeue() {
        if (size_ == 0) {
            return NULL;
        }

        const QueueNode<E>* const old_head = head_;
        head_ = head_->next_;
        size_--;
        if (size_ == 0) {
            last_ = NULL;
        }

        E* element = new E(old_head->element());
        delete old_head;

        return element;
    }

    // 在队列的每一个元素上应用function,并返回新队列。原队列不受影响
    template <typename F>
    Queue* Map(F function) const {
        Queue* new_queue = new Queue();
        for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
            new_queue->Enqueue(function(node->element()));
        }

        return new_queue;
    }

private:
    QueueNode<E>* head_;  // 队列头.
    QueueNode<E>* last_;  // 队列尾.
    size_t size_;  // 队列中元素的数量.

    // 禁止队列拷贝及赋值.
    Queue(const Queue&);
    const Queue& operator = (const Queue&);
};

#endif  // GTEST_SAMPLES_SAMPLE3_INL_H_

 

main.cpp:

#include "sample3.h"
#include "gtest/gtest.h"

class QueueTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
    // accessed from sub-classes.

    // 虚函数SetUp将在所有测试用例之前调用,当有变量需要初始化时,应当定义这个函数
    virtual void SetUp() {
        q1_.Enqueue(1);
        q2_.Enqueue(2);
        q2_.Enqueue(3);
    }

    //虚函数 TearDown()将在所有测试用例运行后调用,这个函数用于清理类中的资源,没有资源需要清理时要以不定义
    // virtual void TearDown() {
    // }

    // 用于测试的函数.
    static int Double(int n) {
        return 2*n;
    }

    // 用于测试Queue::Map()的函数.
    void MapTester(const Queue<int> * q) {
        // 创建一个队列,这个队列是原队列中数值的两倍
        const Queue<int> * const new_q = q->Map(Double);

        // 确认新老队列的元素数量相同
        ASSERT_EQ(q->Size(), new_q->Size());

        // 确认两个队列的关系
        for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
            n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
                EXPECT_EQ(2 * n1->element(), n2->element());
        }

        delete new_q;
    }

    // Declares the variables your tests want to use.
    Queue<int> q0_;
    Queue<int> q1_;
    Queue<int> q2_;
};

// When you have a test fixture, you define a test using TEST_F
// instead of TEST.

// 测试默认构造函数
TEST_F(QueueTest, DefaultConstructor) {
    // You can access data in the test fixture here.
    EXPECT_EQ(0u, q0_.Size());
}

// Tests Dequeue().
TEST_F(QueueTest, Dequeue) {
    int * n = q0_.Dequeue();
    EXPECT_TRUE(n == NULL);

    n = q1_.Dequeue();
    ASSERT_TRUE(n != NULL);
    EXPECT_EQ(1, *n);
    EXPECT_EQ(0u, q1_.Size());
    delete n;

    n = q2_.Dequeue();
    ASSERT_TRUE(n != NULL);
    EXPECT_EQ(2, *n);
    EXPECT_EQ(1u, q2_.Size());
    delete n;
}

// Tests the Queue::Map() function.
TEST_F(QueueTest, Map) {
    MapTester(&q0_);
    MapTester(&q1_);
    MapTester(&q2_);
}


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

运行结果:

转载于:https://www.cnblogs.com/fanx/p/4574143.html

[ 60%] Built target ydlidar_sdk [ 68%] Built target etlidar_test [ 76%] Built target tof_test [ 84%] Built target ydlidar_test [ 92%] Built target lidar_c_api_test [ 96%] Building CXX object test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o In file included from /usr/include/gtest/gtest-message.h:57, from /usr/include/gtest/gtest-assertion-result.h:46, from /usr/include/gtest/gtest.h:64, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.h:4, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.cpp:1: /usr/include/gtest/internal/gtest-port.h:279:2: error: #error C++ versions less than C++14 are not supported. 279 | #error C++ versions less than C++14 are not supported. | ^~~~~ /usr/include/gtest/gtest-assertion-result.h: In member function ‘void testing::AssertionResult::AppendMessage(const testing::Message&)’: /usr/include/gtest/gtest-assertion-result.h:207:48: error: ‘make_unique’ is not a member of ‘std’ 207 | if (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^~~~~~~~~~~ /usr/include/gtest/gtest-assertion-result.h:207:48: note: ‘std::make_unique’ is only available from C++14 onwards /usr/include/gtest/gtest-assertion-result.h:207:73: error: expected primary-expression before ‘>’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ /usr/include/gtest/gtest-assertion-result.h:207:75: error: expected primary-expression before ‘)’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ In file included from /usr/include/gtest/gtest-printers.h:122, from /usr/include/gtest/gtest-matchers.h:49, from /usr/include/gtest/internal/gtest-death-test-internal.h:47, from /usr/include/gtest/gtest-death-test.h:43, from /usr/include/gtest/gtest.h:65: /usr/include/gtest/internal/gtest-internal.h: At global scope: /usr/include/gtest/internal/gtest-internal.h:622:58: error: wrong number of template arguments (0, should be 1) 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^ In file included from /usr/include/c++/13/string:49, from /usr/include/c++/13/bits/locale_classes.h:40, from /usr/include/c++/13/bits/ios_base.h:41, from /usr/include/c++/13/iomanip:42, from /usr/include/gtest/gtest.h:54: /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/internal/gtest-internal.h:622:59: error: template argument 3 is invalid 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::AddTestName(const char*, int, const char*, const char*)’: /usr/include/gtest/internal/gtest-internal.h:599:23: error: request for member ‘insert’ in ‘((testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘int’} 599 | registered_tests_.insert( | ^~~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::TestExists(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:605:30: error: request for member ‘count’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 605 | return registered_tests_.count(test_name) > 0; | ^~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘const testing::internal::CodeLocation& testing::internal::TypedTestSuitePState::GetCodeLocation(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:609:40: error: qualified-id in declaration before ‘it’ 609 | RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name); | ^~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: ‘it’ was not declared in this scope; did you mean ‘int’? 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: request for member ‘end’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:611:12: error: ‘it’ was not declared in this scope; did you mean ‘int’? 611 | return it->second; | ^~ | int /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:725:75: error: wrong number of template arguments (0, should be 1) 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:725:76: error: template argument 3 is invalid 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::EqMatcher<Rhs>::EqMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:728:58: error: wrong number of template arguments (0, should be 1) 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:728:59: error: template argument 3 is invalid 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:728:61: error: expected ‘{’ before ‘(’ token 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:734:67: error: wrong number of template arguments (0, should be 1) 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:734:68: error: template argument 3 is invalid 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::NeMatcher<Rhs>::NeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:737:62: error: wrong number of template arguments (0, should be 1) 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:737:63: error: template argument 3 is invalid 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:737:65: error: expected ‘{’ before ‘(’ token 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:742:71: error: wrong number of template arguments (0, should be 1) 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:742:72: error: template argument 3 is invalid 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LtMatcher<Rhs>::LtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:745:54: error: wrong number of template arguments (0, should be 1) 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:745:55: error: template argument 3 is invalid 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:745:57: error: expected ‘{’ before ‘(’ token 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:750:74: error: wrong number of template arguments (0, should be 1) 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:750:75: error: template argument 3 is invalid 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GtMatcher<Rhs>::GtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:753:57: error: wrong number of template arguments (0, should be 1) 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:753:58: error: template argument 3 is invalid 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:753:60: error: expected ‘{’ before ‘(’ token 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:759:65: error: wrong number of template arguments (0, should be 1) 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:759:66: error: template argument 3 is invalid 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LeMatcher<Rhs>::LeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:762:60: error: wrong number of template arguments (0, should be 1) 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:762:61: error: template argument 3 is invalid 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:762:63: error: expected ‘{’ before ‘(’ token 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:768:68: error: wrong number of template arguments (0, should be 1) 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:768:69: error: template argument 3 is invalid 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GeMatcher<Rhs>::GeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:771:63: error: wrong number of template arguments (0, should be 1) 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:771:64: error: template argument 3 is invalid 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:771:66: error: expected ‘{’ before ‘(’ token 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest.h: At global scope: /usr/include/gtest/gtest.h:302:30: error: ‘std::enable_if_t’ has not been declared 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^~~~~~~~~~~ /usr/include/gtest/gtest.h:302:41: error: expected ‘>’ before ‘<’ token 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^ make[2]: *** [test/CMakeFiles/lidar_test.dir/build.make:76:test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:449:test/CMakeFiles/lidar_test.dir/all] 错误 2 make: *** [Makefile:166:all] 错误 2
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值