Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 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. |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 29 | |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 30 | |
| 31 | // This sample teaches how to reuse a test fixture in multiple test |
| 32 | // cases by deriving sub-fixtures from it. |
| 33 | // |
| 34 | // When you define a test fixture, you specify the name of the test |
| 35 | // case that will use this fixture. Therefore, a test fixture can |
| 36 | // be used by only one test case. |
| 37 | // |
| 38 | // Sometimes, more than one test cases may want to use the same or |
| 39 | // slightly different test fixtures. For example, you may want to |
| 40 | // make sure that all tests for a GUI library don't leak important |
| 41 | // system resources like fonts and brushes. In Google Test, you do |
| 42 | // this by putting the shared logic in a super (as in "super class") |
| 43 | // test fixture, and then have each test case use a fixture derived |
| 44 | // from this super fixture. |
| 45 | |
| 46 | #include <limits.h> |
| 47 | #include <time.h> |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 48 | #include "gtest/gtest.h" |
| 49 | #include "sample1.h" |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 50 | #include "sample3-inl.h" |
| 51 | namespace { |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 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. |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 66 | void SetUp() override { start_time_ = time(nullptr); } |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 67 | |
| 68 | // TearDown() is invoked immediately after a test finishes. Here we |
| 69 | // check if the test was too slow. |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 70 | void TearDown() override { |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 71 | // Gets the time when the test finishes |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 72 | const time_t end_time = time(nullptr); |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 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 | |
| 85 | // We derive a fixture named IntegerFunctionTest from the QuickTest |
| 86 | // fixture. All tests using this fixture will be automatically |
| 87 | // required to be quick. |
| 88 | class IntegerFunctionTest : public QuickTest { |
| 89 | // We don't need any more logic than already in the QuickTest fixture. |
| 90 | // Therefore the body is empty. |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | // Now we can write tests in the IntegerFunctionTest test case. |
| 95 | |
| 96 | // Tests Factorial() |
| 97 | TEST_F(IntegerFunctionTest, Factorial) { |
| 98 | // Tests factorial of negative numbers. |
| 99 | EXPECT_EQ(1, Factorial(-5)); |
| 100 | EXPECT_EQ(1, Factorial(-1)); |
| 101 | EXPECT_GT(Factorial(-10), 0); |
| 102 | |
| 103 | // Tests factorial of 0. |
| 104 | EXPECT_EQ(1, Factorial(0)); |
| 105 | |
| 106 | // Tests factorial of positive numbers. |
| 107 | EXPECT_EQ(1, Factorial(1)); |
| 108 | EXPECT_EQ(2, Factorial(2)); |
| 109 | EXPECT_EQ(6, Factorial(3)); |
| 110 | EXPECT_EQ(40320, Factorial(8)); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | // Tests IsPrime() |
| 115 | TEST_F(IntegerFunctionTest, IsPrime) { |
| 116 | // Tests negative input. |
| 117 | EXPECT_FALSE(IsPrime(-1)); |
| 118 | EXPECT_FALSE(IsPrime(-2)); |
| 119 | EXPECT_FALSE(IsPrime(INT_MIN)); |
| 120 | |
| 121 | // Tests some trivial cases. |
| 122 | EXPECT_FALSE(IsPrime(0)); |
| 123 | EXPECT_FALSE(IsPrime(1)); |
| 124 | EXPECT_TRUE(IsPrime(2)); |
| 125 | EXPECT_TRUE(IsPrime(3)); |
| 126 | |
| 127 | // Tests positive input. |
| 128 | EXPECT_FALSE(IsPrime(4)); |
| 129 | EXPECT_TRUE(IsPrime(5)); |
| 130 | EXPECT_FALSE(IsPrime(6)); |
| 131 | EXPECT_TRUE(IsPrime(23)); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // The next test case (named "QueueTest") also needs to be quick, so |
| 136 | // we derive another fixture from QuickTest. |
| 137 | // |
| 138 | // The QueueTest test fixture has some logic and shared objects in |
| 139 | // addition to what's in QuickTest already. We define the additional |
| 140 | // stuff inside the body of the test fixture, as usual. |
| 141 | class QueueTest : public QuickTest { |
| 142 | protected: |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 143 | void SetUp() override { |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 144 | // First, we need to set up the super fixture (QuickTest). |
| 145 | QuickTest::SetUp(); |
| 146 | |
| 147 | // Second, some additional setup for this fixture. |
| 148 | q1_.Enqueue(1); |
| 149 | q2_.Enqueue(2); |
| 150 | q2_.Enqueue(3); |
| 151 | } |
| 152 | |
| 153 | // By default, TearDown() inherits the behavior of |
| 154 | // QuickTest::TearDown(). As we have no additional cleaning work |
| 155 | // for QueueTest, we omit it here. |
| 156 | // |
| 157 | // virtual void TearDown() { |
| 158 | // QuickTest::TearDown(); |
| 159 | // } |
| 160 | |
| 161 | Queue<int> q0_; |
| 162 | Queue<int> q1_; |
| 163 | Queue<int> q2_; |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | // Now, let's write tests using the QueueTest fixture. |
| 168 | |
| 169 | // Tests the default constructor. |
| 170 | TEST_F(QueueTest, DefaultConstructor) { |
| 171 | EXPECT_EQ(0u, q0_.Size()); |
| 172 | } |
| 173 | |
| 174 | // Tests Dequeue(). |
| 175 | TEST_F(QueueTest, Dequeue) { |
| 176 | int* n = q0_.Dequeue(); |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 177 | EXPECT_TRUE(n == nullptr); |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 178 | |
| 179 | n = q1_.Dequeue(); |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 180 | EXPECT_TRUE(n != nullptr); |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 181 | EXPECT_EQ(1, *n); |
| 182 | EXPECT_EQ(0u, q1_.Size()); |
| 183 | delete n; |
| 184 | |
| 185 | n = q2_.Dequeue(); |
James Kuszmaul | e2f1529 | 2021-05-10 22:37:32 -0700 | [diff] [blame^] | 186 | EXPECT_TRUE(n != nullptr); |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 187 | EXPECT_EQ(2, *n); |
| 188 | EXPECT_EQ(1u, q2_.Size()); |
| 189 | delete n; |
| 190 | } |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame] | 191 | } // namespace |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 192 | // If necessary, you can derive further test fixtures from a derived |
| 193 | // fixture itself. For example, you can derive another fixture from |
| 194 | // QueueTest. Google Test imposes no limit on how deep the hierarchy |
| 195 | // can be. In practice, however, you probably don't want it to be too |
| 196 | // deep as to be confusing. |