①GTest学习笔记2017-04-27

1、测试程序块的执行时间

需要包含头文件如下

#include <limits.h>
#include <time.h>

设置TestCase事件,即  

SetUp()和TearDown()
如下为测试程序块是否在5秒以内完成
 1 #include <limits.h>
 2 #include <time.h>
 3 //#include "sample3-inl.h"
 4 #include "gtest/gtest.h"
 5 //#include "sample1.h"
 6 
 7 // In this sample, we want to ensure that every test finishes within
 8 // ~5 seconds.  If a test takes longer to run, we consider it a
 9 // failure.
10 //
11 // We put the code for timing a test in a test fixture called
12 // "QuickTest".  QuickTest is intended to be the super fixture that
13 // other fixtures derive from, therefore there is no test case with
14 // the name "QuickTest".  This is OK.
15 //
16 // Later, we will derive multiple test fixtures from QuickTest.
17 class QuickTest : public testing::Test {
18  protected:
19   // Remember that SetUp() is run immediately before a test starts.
20   // This is a good place to record the start time.
21   virtual void SetUp() {
22     start_time_ = time(NULL);
23   }
24 
25   // TearDown() is invoked immediately after a test finishes.  Here we
26   // check if the test was too slow.
27   virtual void TearDown() {
28     // Gets the time when the test finishes
29     const time_t end_time = time(NULL);
30 
31     // Asserts that the test took no more than ~5 seconds.  Did you
32     // know that you can use assertions in SetUp() and TearDown() as
33     // well?
34     EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
35   }
36 
37   // The UTC time (in seconds) when the test starts
38   time_t start_time_;
39 };

1.1  新建测试类,继承类QuickTest,用于测试非类内函数的测试,新建类为空,如下

1 // We derive a fixture named IntegerFunctionTest from the QuickTest
2 // fixture.  All tests using this fixture will be automatically
3 // required to be quick.
4 class IntegerFunctionTest : public QuickTest {
5   // We don't need any more logic than already in the QuickTest fixture.
6   // Therefore the body is empty.
7 };

然后,编写测试案例,如下所示,

 1 // Now we can write tests in the IntegerFunctionTest test case.
 2 
 3 // Tests Factorial()
 4 TEST_F(IntegerFunctionTest, Factorial) {
 5   // Tests factorial of negative numbers.
 6   EXPECT_EQ(1, Factorial(-5));
 7   EXPECT_EQ(1, Factorial(-1));
 8   EXPECT_GT(Factorial(-10), 0);
 9 
10   // Tests factorial of 0.
11   EXPECT_EQ(1, Factorial(0));
12 
13   // Tests factorial of positive numbers.
14   EXPECT_EQ(1, Factorial(1));
15   EXPECT_EQ(2, Factorial(2));
16   EXPECT_EQ(6, Factorial(3));
17   EXPECT_EQ(40320, Factorial(8));
18 }

1.2 新建类继承类QuickTest,用于测试类和类内函数,新建类包含部分测试类和函数的初始化,如下所示

 1 // The next test case (named "QueueTest") also needs to be quick, so
 2 // we derive another fixture from QuickTest.
 3 //
 4 // The QueueTest test fixture has some logic and shared objects in
 5 // addition to what's in QuickTest already.  We define the additional
 6 // stuff inside the body of the test fixture, as usual.
 7 class QueueTest : public QuickTest {
 8  protected:
 9   virtual void SetUp() {
10     // First, we need to set up the super fixture (QuickTest).
11     QuickTest::SetUp();
12 
13     // Second, some additional setup for this fixture.
14     q1_.Enqueue(1);
15     q2_.Enqueue(2);
16     q2_.Enqueue(3);
17   }
18 
19   // By default, TearDown() inherits the behavior of
20   // QuickTest::TearDown().  As we have no additional cleaning work
21   // for QueueTest, we omit it here.
22   //
23   // virtual void TearDown() {
24   //   QuickTest::TearDown();
25   // }
26 
27   Queue<int> q0_;
28   Queue<int> q1_;
29   Queue<int> q2_;
30 };

然后,编写测试案例,如下所示,

 1 // Now, let's write tests using the QueueTest fixture.
 2 
 3 // Tests the default constructor.
 4 TEST_F(QueueTest, DefaultConstructor) {
 5   EXPECT_EQ(0u, q0_.Size());
 6 }
 7 
 8 // Tests Dequeue().
 9 TEST_F(QueueTest, Dequeue) {
10   int* n = q0_.Dequeue();
11   EXPECT_TRUE(n == NULL);
12 
13   n = q1_.Dequeue();
14   EXPECT_TRUE(n != NULL);
15   EXPECT_EQ(1, *n);
16   EXPECT_EQ(0u, q1_.Size());
17   delete n;
18 
19   n = q2_.Dequeue();
20   EXPECT_TRUE(n != NULL);
21   EXPECT_EQ(2, *n);
22   EXPECT_EQ(1u, q2_.Size());
23   delete n;
24 }

2、完整测试工程如下所示

2.1 测试文件sample1.cpp

 1 // Copyright 2005, Google Inc.
 2 // All rights reserved.
 3 //
 4 // Redistribution and use in source and binary forms, with or without
 5 // modification, are permitted provided that the following conditions are
 6 // met:
 7 //
 8 //     * Redistributions of source code must retain the above copyright
 9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // A sample program demonstrating using Google C++ testing framework.
31 //
32 // Author: wan@google.com (Zhanyong Wan)
33 
34 #include "sample1.h"
35 
36 // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
37 int Factorial(int n) {
38   int result = 1;
39   for (int i = 1; i <= n; i++) {
40     result *= i;
41   }
42 
43   return result;
44 }
45 
46 // Returns true iff n is a prime number.
47 bool IsPrime(int n) {
48   // Trivial case 1: small numbers
49   if (n <= 1) return false;
50 
51   // Trivial case 2: even numbers
52   if (n % 2 == 0) return n == 2;
53 
54   // Now, we have that n is odd and n >= 3.
55 
56   // Try to divide n by every odd number i, starting from 3
57   for (int i = 3; ; i += 2) {
58     // We only have to try i up to the squre root of n
59     if (i > n/i) break;
60 
61     // Now, we have i <= n/i < n.
62     // If n is divisible by i, n is not prime.
63     if (n % i == 0) return false;
64   }
65 
66   // n has no integer factor in the range (1, n), and thus is prime.
67   return true;
68 }

2.2 测试文件sample1.h

 1 // Copyright 2005, Google Inc.
 2 // All rights reserved.
 3 //
 4 // Redistribution and use in source and binary forms, with or without
 5 // modification, are permitted provided that the following conditions are
 6 // met:
 7 //
 8 //     * Redistributions of source code must retain the above copyright
 9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // A sample program demonstrating using Google C++ testing framework.
31 //
32 // Author: wan@google.com (Zhanyong Wan)
33 
34 #ifndef GTEST_SAMPLES_SAMPLE1_H_
35 #define GTEST_SAMPLES_SAMPLE1_H_
36 
37 // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
38 int Factorial(int n);
39 
40 // Returns true iff n is a prime number.
41 bool IsPrime(int n);
42 
43 #endif  // GTEST_SAMPLES_SAMPLE1_H_

2.3 测试文件sample3-inl.h

  1 // Copyright 2005, Google Inc.
  2 // All rights reserved.
  3 //
  4 // Redistribution and use in source and binary forms, with or without
  5 // modification, are permitted provided that the following conditions are
  6 // met:
  7 //
  8 //     * Redistributions of source code must retain the above copyright
  9 // notice, this list of conditions and the following disclaimer.
 10 //     * Redistributions in binary form must reproduce the above
 11 // copyright notice, this list of conditions and the following disclaimer
 12 // in the documentation and/or other materials provided with the
 13 // distribution.
 14 //     * Neither the name of Google Inc. nor the names of its
 15 // contributors may be used to endorse or promote products derived from
 16 // this software without specific prior written permission.
 17 //
 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 
 30 // A sample program demonstrating using Google C++ testing framework.
 31 //
 32 // Author: wan@google.com (Zhanyong Wan)
 33 
 34 #ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
 35 #define GTEST_SAMPLES_SAMPLE3_INL_H_
 36 
 37 #include <stddef.h>
 38 
 39 
 40 // Queue is a simple queue implemented as a singled-linked list.
 41 //
 42 // The element type must support copy constructor.
 43 template <typename E>  // E is the element type
 44 class Queue;
 45 
 46 // QueueNode is a node in a Queue, which consists of an element of
 47 // type E and a pointer to the next node.
 48 template <typename E>  // E is the element type
 49 class QueueNode {
 50   friend class Queue<E>;
 51 
 52  public:
 53   // Gets the element in this node.
 54   const E& element() const { return element_; }
 55 
 56   // Gets the next node in the queue.
 57   QueueNode* next() { return next_; }
 58   const QueueNode* next() const { return next_; }
 59 
 60  private:
 61   // Creates a node with a given element value.  The next pointer is
 62   // set to NULL.
 63   explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
 64 
 65   // We disable the default assignment operator and copy c'tor.
 66   const QueueNode& operator = (const QueueNode&);
 67   QueueNode(const QueueNode&);
 68 
 69   E element_;
 70   QueueNode* next_;
 71 };
 72 
 73 template <typename E>  // E is the element type.
 74 class Queue {
 75  public:
 76   // Creates an empty queue.
 77   Queue() : head_(NULL), last_(NULL), size_(0) {}
 78 
 79   // D'tor.  Clears the queue.
 80   ~Queue() { Clear(); }
 81 
 82   // Clears the queue.
 83   void Clear() {
 84     if (size_ > 0) {
 85       // 1. Deletes every node.
 86       QueueNode<E>* node = head_;
 87       QueueNode<E>* next = node->next();
 88       for (; ;) {
 89         delete node;
 90         node = next;
 91         if (node == NULL) break;
 92         next = node->next();
 93       }
 94 
 95       // 2. Resets the member variables.
 96       head_ = last_ = NULL;
 97       size_ = 0;
 98     }
 99   }
100 
101   // Gets the number of elements.
102   size_t Size() const { return size_; }
103 
104   // Gets the first element of the queue, or NULL if the queue is empty.
105   QueueNode<E>* Head() { return head_; }
106   const QueueNode<E>* Head() const { return head_; }
107 
108   // Gets the last element of the queue, or NULL if the queue is empty.
109   QueueNode<E>* Last() { return last_; }
110   const QueueNode<E>* Last() const { return last_; }
111 
112   // Adds an element to the end of the queue.  A copy of the element is
113   // created using the copy constructor, and then stored in the queue.
114   // Changes made to the element in the queue doesn't affect the source
115   // object, and vice versa.
116   void Enqueue(const E& element) {
117     QueueNode<E>* new_node = new QueueNode<E>(element);
118 
119     if (size_ == 0) {
120       head_ = last_ = new_node;
121       size_ = 1;
122     } else {
123       last_->next_ = new_node;
124       last_ = new_node;
125       size_++;
126     }
127   }
128 
129   // Removes the head of the queue and returns it.  Returns NULL if
130   // the queue is empty.
131   E* Dequeue() {
132     if (size_ == 0) {
133       return NULL;
134     }
135 
136     const QueueNode<E>* const old_head = head_;
137     head_ = head_->next_;
138     size_--;
139     if (size_ == 0) {
140       last_ = NULL;
141     }
142 
143     E* element = new E(old_head->element());
144     delete old_head;
145 
146     return element;
147   }
148 
149   // Applies a function/functor on each element of the queue, and
150   // returns the result in a new queue.  The original queue is not
151   // affected.
152   template <typename F>
153   Queue* Map(F function) const {
154     Queue* new_queue = new Queue();
155     for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
156       new_queue->Enqueue(function(node->element()));
157     }
158 
159     return new_queue;
160   }
161 
162  private:
163   QueueNode<E>* head_;  // The first node of the queue.
164   QueueNode<E>* last_;  // The last node of the queue.
165   size_t size_;  // The number of elements in the queue.
166 
167   // We disallow copying a queue.
168   Queue(const Queue&);
169   const Queue& operator = (const Queue&);
170 };
171 
172 #endif  // GTEST_SAMPLES_SAMPLE3_INL_H_

2.4测试文件 sample_unittest.cpp

  1 // Copyright 2005, Google Inc.
  2 // All rights reserved.
  3 //
  4 // Redistribution and use in source and binary forms, with or without
  5 // modification, are permitted provided that the following conditions are
  6 // met:
  7 //
  8 //     * Redistributions of source code must retain the above copyright
  9 // notice, this list of conditions and the following disclaimer.
 10 //     * Redistributions in binary form must reproduce the above
 11 // copyright notice, this list of conditions and the following disclaimer
 12 // in the documentation and/or other materials provided with the
 13 // distribution.
 14 //     * Neither the name of Google Inc. nor the names of its
 15 // contributors may be used to endorse or promote products derived from
 16 // this software without specific prior written permission.
 17 //
 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 //
 30 // Author: wan@google.com (Zhanyong Wan)
 31 
 32 // This sample teaches how to reuse a test fixture in multiple test
 33 // cases by deriving sub-fixtures from it.
 34 //
 35 // When you define a test fixture, you specify the name of the test
 36 // case that will use this fixture.  Therefore, a test fixture can
 37 // be used by only one test case.
 38 //
 39 // Sometimes, more than one test cases may want to use the same or
 40 // slightly different test fixtures.  For example, you may want to
 41 // make sure that all tests for a GUI library don't leak important
 42 // system resources like fonts and brushes.  In Google Test, you do
 43 // this by putting the shared logic in a super (as in "super class")
 44 // test fixture, and then have each test case use a fixture derived
 45 // from this super fixture.
 46 
 47 #include <limits.h>
 48 #include <time.h>
 49 #include "sample3-inl.h"
 50 #include "gtest/gtest.h"
 51 #include "sample1.h"
 52 
 53 // In this sample, we want to ensure that every test finishes within
 54 // ~5 seconds.  If a test takes longer to run, we consider it a
 55 // failure.
 56 //
 57 // We put the code for timing a test in a test fixture called
 58 // "QuickTest".  QuickTest is intended to be the super fixture that
 59 // other fixtures derive from, therefore there is no test case with
 60 // the name "QuickTest".  This is OK.
 61 //
 62 // Later, we will derive multiple test fixtures from QuickTest.
 63 class QuickTest : public testing::Test {
 64  protected:
 65   // Remember that SetUp() is run immediately before a test starts.
 66   // This is a good place to record the start time.
 67   virtual void SetUp() {
 68     start_time_ = time(NULL);
 69   }
 70 
 71   // TearDown() is invoked immediately after a test finishes.  Here we
 72   // check if the test was too slow.
 73   virtual void TearDown() {
 74     // Gets the time when the test finishes
 75     const time_t end_time = time(NULL);
 76 
 77     // Asserts that the test took no more than ~5 seconds.  Did you
 78     // know that you can use assertions in SetUp() and TearDown() as
 79     // well?
 80     EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
 81   }
 82 
 83   // The UTC time (in seconds) when the test starts
 84   time_t start_time_;
 85 };
 86 
 87 
 88 // We derive a fixture named IntegerFunctionTest from the QuickTest
 89 // fixture.  All tests using this fixture will be automatically
 90 // required to be quick.
 91 class IntegerFunctionTest : public QuickTest {
 92   // We don't need any more logic than already in the QuickTest fixture.
 93   // Therefore the body is empty.
 94 };
 95 
 96 
 97 // Now we can write tests in the IntegerFunctionTest test case.
 98 
 99 // Tests Factorial()
100 TEST_F(IntegerFunctionTest, Factorial) {
101   // Tests factorial of negative numbers.
102   EXPECT_EQ(1, Factorial(-5));
103   EXPECT_EQ(1, Factorial(-1));
104   EXPECT_GT(Factorial(-10), 0);
105 
106   // Tests factorial of 0.
107   EXPECT_EQ(1, Factorial(0));
108 
109   // Tests factorial of positive numbers.
110   EXPECT_EQ(1, Factorial(1));
111   EXPECT_EQ(2, Factorial(2));
112   EXPECT_EQ(6, Factorial(3));
113   EXPECT_EQ(40320, Factorial(8));
114 }
115 
116 
117 // Tests IsPrime()
118 TEST_F(IntegerFunctionTest, IsPrime) {
119   // Tests negative input.
120   EXPECT_FALSE(IsPrime(-1));
121   EXPECT_FALSE(IsPrime(-2));
122   EXPECT_FALSE(IsPrime(INT_MIN));
123 
124   // Tests some trivial cases.
125   EXPECT_FALSE(IsPrime(0));
126   EXPECT_FALSE(IsPrime(1));
127   EXPECT_TRUE(IsPrime(2));
128   EXPECT_TRUE(IsPrime(3));
129 
130   // Tests positive input.
131   EXPECT_FALSE(IsPrime(4));
132   EXPECT_TRUE(IsPrime(5));
133   EXPECT_FALSE(IsPrime(6));
134   EXPECT_TRUE(IsPrime(23));
135 }
136 
137 
138 // The next test case (named "QueueTest") also needs to be quick, so
139 // we derive another fixture from QuickTest.
140 //
141 // The QueueTest test fixture has some logic and shared objects in
142 // addition to what's in QuickTest already.  We define the additional
143 // stuff inside the body of the test fixture, as usual.
144 class QueueTest : public QuickTest {
145  protected:
146   virtual void SetUp() {
147     // First, we need to set up the super fixture (QuickTest).
148     QuickTest::SetUp();
149 
150     // Second, some additional setup for this fixture.
151     q1_.Enqueue(1);
152     q2_.Enqueue(2);
153     q2_.Enqueue(3);
154   }
155 
156   // By default, TearDown() inherits the behavior of
157   // QuickTest::TearDown().  As we have no additional cleaning work
158   // for QueueTest, we omit it here.
159   //
160   // virtual void TearDown() {
161   //   QuickTest::TearDown();
162   // }
163 
164   Queue<int> q0_;
165   Queue<int> q1_;
166   Queue<int> q2_;
167 };
168 
169 
170 // Now, let's write tests using the QueueTest fixture.
171 
172 // Tests the default constructor.
173 TEST_F(QueueTest, DefaultConstructor) {
174   EXPECT_EQ(0u, q0_.Size());
175 }
176 
177 // Tests Dequeue().
178 TEST_F(QueueTest, Dequeue) {
179   int* n = q0_.Dequeue();
180   EXPECT_TRUE(n == NULL);
181 
182   n = q1_.Dequeue();
183   EXPECT_TRUE(n != NULL);
184   EXPECT_EQ(1, *n);
185   EXPECT_EQ(0u, q1_.Size());
186   delete n;
187 
188   n = q2_.Dequeue();
189   EXPECT_TRUE(n != NULL);
190   EXPECT_EQ(2, *n);
191   EXPECT_EQ(1u, q2_.Size());
192   delete n;
193 }
194 
195 // If necessary, you can derive further test fixtures from a derived
196 // fixture itself.  For example, you can derive another fixture from
197 // QueueTest.  Google Test imposes no limit on how deep the hierarchy
198 // can be.  In practice, however, you probably don't want it to be too
199 // deep as to be confusing.

 

posted @ 2017-04-27 10:23  半醉人间帅  阅读(213)  评论(0编辑  收藏  举报