Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample5_unittest.cc
Go to the documentation of this file.
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 // This sample teaches how to reuse a test fixture in multiple test
31 // cases by deriving sub-fixtures from it.
32 //
33 // When you define a test fixture, you specify the name of the test
34 // case that will use this fixture. Therefore, a test fixture can
35 // be used by only one test case.
36 //
37 // Sometimes, more than one test cases may want to use the same or
38 // slightly different test fixtures. For example, you may want to
39 // make sure that all tests for a GUI library don't leak important
40 // system resources like fonts and brushes. In Google Test, you do
41 // this by putting the shared logic in a super (as in "super class")
42 // test fixture, and then have each test case use a fixture derived
43 // from this super fixture.
44 
45 #include <limits.h>
46 #include <time.h>
47 
48 #include "sample1.h"
49 #include "sample3-inl.h"
50 #include "gtest/gtest.h"
51 namespace {
52 // In this sample, we want to ensure that every test finishes within
53 // ~5 seconds. If a test takes longer to run, we consider it a
54 // failure.
55 //
56 // We put the code for timing a test in a test fixture called
57 // "QuickTest". QuickTest is intended to be the super fixture that
58 // other fixtures derive from, therefore there is no test case with
59 // the name "QuickTest". This is OK.
60 //
61 // Later, we will derive multiple test fixtures from QuickTest.
62 class QuickTest : public testing::Test {
63  protected:
64  // Remember that SetUp() is run immediately before a test starts.
65  // This is a good place to record the start time.
66  void SetUp() override { start_time_ = time(nullptr); }
67 
68  // TearDown() is invoked immediately after a test finishes. Here we
69  // check if the test was too slow.
70  void TearDown() override {
71  // Gets the time when the test finishes
72  const time_t end_time = time(nullptr);
73 
74  // Asserts that the test took no more than ~5 seconds. Did you
75  // know that you can use assertions in SetUp() and TearDown() as
76  // well?
77  EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
78  }
79 
80  // The UTC time (in seconds) when the test starts
81  time_t start_time_;
82 };
83 
84 // We derive a fixture named IntegerFunctionTest from the QuickTest
85 // fixture. All tests using this fixture will be automatically
86 // required to be quick.
87 class IntegerFunctionTest : public QuickTest {
88  // We don't need any more logic than already in the QuickTest fixture.
89  // Therefore the body is empty.
90 };
91 
92 // Now we can write tests in the IntegerFunctionTest test case.
93 
94 // Tests Factorial()
95 TEST_F(IntegerFunctionTest, Factorial) {
96  // Tests factorial of negative numbers.
97  EXPECT_EQ(1, Factorial(-5));
98  EXPECT_EQ(1, Factorial(-1));
99  EXPECT_GT(Factorial(-10), 0);
100 
101  // Tests factorial of 0.
102  EXPECT_EQ(1, Factorial(0));
103 
104  // Tests factorial of positive numbers.
105  EXPECT_EQ(1, Factorial(1));
106  EXPECT_EQ(2, Factorial(2));
107  EXPECT_EQ(6, Factorial(3));
108  EXPECT_EQ(40320, Factorial(8));
109 }
110 
111 // Tests IsPrime()
112 TEST_F(IntegerFunctionTest, IsPrime) {
113  // Tests negative input.
114  EXPECT_FALSE(IsPrime(-1));
115  EXPECT_FALSE(IsPrime(-2));
116  EXPECT_FALSE(IsPrime(INT_MIN));
117 
118  // Tests some trivial cases.
119  EXPECT_FALSE(IsPrime(0));
120  EXPECT_FALSE(IsPrime(1));
121  EXPECT_TRUE(IsPrime(2));
122  EXPECT_TRUE(IsPrime(3));
123 
124  // Tests positive input.
125  EXPECT_FALSE(IsPrime(4));
126  EXPECT_TRUE(IsPrime(5));
127  EXPECT_FALSE(IsPrime(6));
128  EXPECT_TRUE(IsPrime(23));
129 }
130 
131 // The next test case (named "QueueTest") also needs to be quick, so
132 // we derive another fixture from QuickTest.
133 //
134 // The QueueTest test fixture has some logic and shared objects in
135 // addition to what's in QuickTest already. We define the additional
136 // stuff inside the body of the test fixture, as usual.
137 class QueueTest : public QuickTest {
138  protected:
139  void SetUp() override {
140  // First, we need to set up the super fixture (QuickTest).
141  QuickTest::SetUp();
142 
143  // Second, some additional setup for this fixture.
144  q1_.Enqueue(1);
145  q2_.Enqueue(2);
146  q2_.Enqueue(3);
147  }
148 
149  // By default, TearDown() inherits the behavior of
150  // QuickTest::TearDown(). As we have no additional cleaning work
151  // for QueueTest, we omit it here.
152  //
153  // virtual void TearDown() {
154  // QuickTest::TearDown();
155  // }
156 
157  Queue<int> q0_;
158  Queue<int> q1_;
159  Queue<int> q2_;
160 };
161 
162 // Now, let's write tests using the QueueTest fixture.
163 
164 // Tests the default constructor.
165 TEST_F(QueueTest, DefaultConstructor) { EXPECT_EQ(0u, q0_.Size()); }
166 
167 // Tests Dequeue().
168 TEST_F(QueueTest, Dequeue) {
169  int* n = q0_.Dequeue();
170  EXPECT_TRUE(n == nullptr);
171 
172  n = q1_.Dequeue();
173  EXPECT_TRUE(n != nullptr);
174  EXPECT_EQ(1, *n);
175  EXPECT_EQ(0u, q1_.Size());
176  delete n;
177 
178  n = q2_.Dequeue();
179  EXPECT_TRUE(n != nullptr);
180  EXPECT_EQ(2, *n);
181  EXPECT_EQ(1u, q2_.Size());
182  delete n;
183 }
184 } // namespace
185 // If necessary, you can derive further test fixtures from a derived
186 // fixture itself. For example, you can derive another fixture from
187 // QueueTest. Google Test imposes no limit on how deep the hierarchy
188 // can be. In practice, however, you probably don't want it to be too
189 // deep as to be confusing.
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2224
virtual void SetUp()
Definition: gtest.cc:2512
#define EXPECT_GT(val1, val2)
Definition: gtest.h:1894
bool IsPrime(int n)
Definition: sample1.cc:45
virtual void TearDown()
Definition: gtest.cc:2517
int Factorial(int n)
Definition: sample1.cc:35
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define EXPECT_TRUE(condition)
Definition: gtest.h:1823
#define EXPECT_FALSE(condition)
Definition: gtest.h:1827
int n