Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2008 Google Inc. |
michael@0 | 2 | // All Rights Reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | |
michael@0 | 32 | // This sample shows how to test common properties of multiple |
michael@0 | 33 | // implementations of the same interface (aka interface tests). |
michael@0 | 34 | |
michael@0 | 35 | // The interface and its implementations are in this header. |
michael@0 | 36 | #include "prime_tables.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "gtest/gtest.h" |
michael@0 | 39 | |
michael@0 | 40 | // First, we define some factory functions for creating instances of |
michael@0 | 41 | // the implementations. You may be able to skip this step if all your |
michael@0 | 42 | // implementations can be constructed the same way. |
michael@0 | 43 | |
michael@0 | 44 | template <class T> |
michael@0 | 45 | PrimeTable* CreatePrimeTable(); |
michael@0 | 46 | |
michael@0 | 47 | template <> |
michael@0 | 48 | PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() { |
michael@0 | 49 | return new OnTheFlyPrimeTable; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | template <> |
michael@0 | 53 | PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() { |
michael@0 | 54 | return new PreCalculatedPrimeTable(10000); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // Then we define a test fixture class template. |
michael@0 | 58 | template <class T> |
michael@0 | 59 | class PrimeTableTest : public testing::Test { |
michael@0 | 60 | protected: |
michael@0 | 61 | // The ctor calls the factory function to create a prime table |
michael@0 | 62 | // implemented by T. |
michael@0 | 63 | PrimeTableTest() : table_(CreatePrimeTable<T>()) {} |
michael@0 | 64 | |
michael@0 | 65 | virtual ~PrimeTableTest() { delete table_; } |
michael@0 | 66 | |
michael@0 | 67 | // Note that we test an implementation via the base interface |
michael@0 | 68 | // instead of the actual implementation class. This is important |
michael@0 | 69 | // for keeping the tests close to the real world scenario, where the |
michael@0 | 70 | // implementation is invoked via the base interface. It avoids |
michael@0 | 71 | // got-yas where the implementation class has a method that shadows |
michael@0 | 72 | // a method with the same name (but slightly different argument |
michael@0 | 73 | // types) in the base interface, for example. |
michael@0 | 74 | PrimeTable* const table_; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | #if GTEST_HAS_TYPED_TEST |
michael@0 | 78 | |
michael@0 | 79 | using testing::Types; |
michael@0 | 80 | |
michael@0 | 81 | // Google Test offers two ways for reusing tests for different types. |
michael@0 | 82 | // The first is called "typed tests". You should use it if you |
michael@0 | 83 | // already know *all* the types you are gonna exercise when you write |
michael@0 | 84 | // the tests. |
michael@0 | 85 | |
michael@0 | 86 | // To write a typed test case, first use |
michael@0 | 87 | // |
michael@0 | 88 | // TYPED_TEST_CASE(TestCaseName, TypeList); |
michael@0 | 89 | // |
michael@0 | 90 | // to declare it and specify the type parameters. As with TEST_F, |
michael@0 | 91 | // TestCaseName must match the test fixture name. |
michael@0 | 92 | |
michael@0 | 93 | // The list of types we want to test. |
michael@0 | 94 | typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations; |
michael@0 | 95 | |
michael@0 | 96 | TYPED_TEST_CASE(PrimeTableTest, Implementations); |
michael@0 | 97 | |
michael@0 | 98 | // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test, |
michael@0 | 99 | // similar to TEST_F. |
michael@0 | 100 | TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) { |
michael@0 | 101 | // Inside the test body, you can refer to the type parameter by |
michael@0 | 102 | // TypeParam, and refer to the fixture class by TestFixture. We |
michael@0 | 103 | // don't need them in this example. |
michael@0 | 104 | |
michael@0 | 105 | // Since we are in the template world, C++ requires explicitly |
michael@0 | 106 | // writing 'this->' when referring to members of the fixture class. |
michael@0 | 107 | // This is something you have to learn to live with. |
michael@0 | 108 | EXPECT_FALSE(this->table_->IsPrime(-5)); |
michael@0 | 109 | EXPECT_FALSE(this->table_->IsPrime(0)); |
michael@0 | 110 | EXPECT_FALSE(this->table_->IsPrime(1)); |
michael@0 | 111 | EXPECT_FALSE(this->table_->IsPrime(4)); |
michael@0 | 112 | EXPECT_FALSE(this->table_->IsPrime(6)); |
michael@0 | 113 | EXPECT_FALSE(this->table_->IsPrime(100)); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) { |
michael@0 | 117 | EXPECT_TRUE(this->table_->IsPrime(2)); |
michael@0 | 118 | EXPECT_TRUE(this->table_->IsPrime(3)); |
michael@0 | 119 | EXPECT_TRUE(this->table_->IsPrime(5)); |
michael@0 | 120 | EXPECT_TRUE(this->table_->IsPrime(7)); |
michael@0 | 121 | EXPECT_TRUE(this->table_->IsPrime(11)); |
michael@0 | 122 | EXPECT_TRUE(this->table_->IsPrime(131)); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | TYPED_TEST(PrimeTableTest, CanGetNextPrime) { |
michael@0 | 126 | EXPECT_EQ(2, this->table_->GetNextPrime(0)); |
michael@0 | 127 | EXPECT_EQ(3, this->table_->GetNextPrime(2)); |
michael@0 | 128 | EXPECT_EQ(5, this->table_->GetNextPrime(3)); |
michael@0 | 129 | EXPECT_EQ(7, this->table_->GetNextPrime(5)); |
michael@0 | 130 | EXPECT_EQ(11, this->table_->GetNextPrime(7)); |
michael@0 | 131 | EXPECT_EQ(131, this->table_->GetNextPrime(128)); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // That's it! Google Test will repeat each TYPED_TEST for each type |
michael@0 | 135 | // in the type list specified in TYPED_TEST_CASE. Sit back and be |
michael@0 | 136 | // happy that you don't have to define them multiple times. |
michael@0 | 137 | |
michael@0 | 138 | #endif // GTEST_HAS_TYPED_TEST |
michael@0 | 139 | |
michael@0 | 140 | #if GTEST_HAS_TYPED_TEST_P |
michael@0 | 141 | |
michael@0 | 142 | using testing::Types; |
michael@0 | 143 | |
michael@0 | 144 | // Sometimes, however, you don't yet know all the types that you want |
michael@0 | 145 | // to test when you write the tests. For example, if you are the |
michael@0 | 146 | // author of an interface and expect other people to implement it, you |
michael@0 | 147 | // might want to write a set of tests to make sure each implementation |
michael@0 | 148 | // conforms to some basic requirements, but you don't know what |
michael@0 | 149 | // implementations will be written in the future. |
michael@0 | 150 | // |
michael@0 | 151 | // How can you write the tests without committing to the type |
michael@0 | 152 | // parameters? That's what "type-parameterized tests" can do for you. |
michael@0 | 153 | // It is a bit more involved than typed tests, but in return you get a |
michael@0 | 154 | // test pattern that can be reused in many contexts, which is a big |
michael@0 | 155 | // win. Here's how you do it: |
michael@0 | 156 | |
michael@0 | 157 | // First, define a test fixture class template. Here we just reuse |
michael@0 | 158 | // the PrimeTableTest fixture defined earlier: |
michael@0 | 159 | |
michael@0 | 160 | template <class T> |
michael@0 | 161 | class PrimeTableTest2 : public PrimeTableTest<T> { |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | // Then, declare the test case. The argument is the name of the test |
michael@0 | 165 | // fixture, and also the name of the test case (as usual). The _P |
michael@0 | 166 | // suffix is for "parameterized" or "pattern". |
michael@0 | 167 | TYPED_TEST_CASE_P(PrimeTableTest2); |
michael@0 | 168 | |
michael@0 | 169 | // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test, |
michael@0 | 170 | // similar to what you do with TEST_F. |
michael@0 | 171 | TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) { |
michael@0 | 172 | EXPECT_FALSE(this->table_->IsPrime(-5)); |
michael@0 | 173 | EXPECT_FALSE(this->table_->IsPrime(0)); |
michael@0 | 174 | EXPECT_FALSE(this->table_->IsPrime(1)); |
michael@0 | 175 | EXPECT_FALSE(this->table_->IsPrime(4)); |
michael@0 | 176 | EXPECT_FALSE(this->table_->IsPrime(6)); |
michael@0 | 177 | EXPECT_FALSE(this->table_->IsPrime(100)); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) { |
michael@0 | 181 | EXPECT_TRUE(this->table_->IsPrime(2)); |
michael@0 | 182 | EXPECT_TRUE(this->table_->IsPrime(3)); |
michael@0 | 183 | EXPECT_TRUE(this->table_->IsPrime(5)); |
michael@0 | 184 | EXPECT_TRUE(this->table_->IsPrime(7)); |
michael@0 | 185 | EXPECT_TRUE(this->table_->IsPrime(11)); |
michael@0 | 186 | EXPECT_TRUE(this->table_->IsPrime(131)); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) { |
michael@0 | 190 | EXPECT_EQ(2, this->table_->GetNextPrime(0)); |
michael@0 | 191 | EXPECT_EQ(3, this->table_->GetNextPrime(2)); |
michael@0 | 192 | EXPECT_EQ(5, this->table_->GetNextPrime(3)); |
michael@0 | 193 | EXPECT_EQ(7, this->table_->GetNextPrime(5)); |
michael@0 | 194 | EXPECT_EQ(11, this->table_->GetNextPrime(7)); |
michael@0 | 195 | EXPECT_EQ(131, this->table_->GetNextPrime(128)); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | // Type-parameterized tests involve one extra step: you have to |
michael@0 | 199 | // enumerate the tests you defined: |
michael@0 | 200 | REGISTER_TYPED_TEST_CASE_P( |
michael@0 | 201 | PrimeTableTest2, // The first argument is the test case name. |
michael@0 | 202 | // The rest of the arguments are the test names. |
michael@0 | 203 | ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime); |
michael@0 | 204 | |
michael@0 | 205 | // At this point the test pattern is done. However, you don't have |
michael@0 | 206 | // any real test yet as you haven't said which types you want to run |
michael@0 | 207 | // the tests with. |
michael@0 | 208 | |
michael@0 | 209 | // To turn the abstract test pattern into real tests, you instantiate |
michael@0 | 210 | // it with a list of types. Usually the test pattern will be defined |
michael@0 | 211 | // in a .h file, and anyone can #include and instantiate it. You can |
michael@0 | 212 | // even instantiate it more than once in the same program. To tell |
michael@0 | 213 | // different instances apart, you give each of them a name, which will |
michael@0 | 214 | // become part of the test case name and can be used in test filters. |
michael@0 | 215 | |
michael@0 | 216 | // The list of types we want to test. Note that it doesn't have to be |
michael@0 | 217 | // defined at the time we write the TYPED_TEST_P()s. |
michael@0 | 218 | typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> |
michael@0 | 219 | PrimeTableImplementations; |
michael@0 | 220 | INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name |
michael@0 | 221 | PrimeTableTest2, // Test case name |
michael@0 | 222 | PrimeTableImplementations); // Type list |
michael@0 | 223 | |
michael@0 | 224 | #endif // GTEST_HAS_TYPED_TEST_P |