sample6_unittest.cc (8861B)
1 // Copyright 2008 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 31 // This sample shows how to test common properties of multiple 32 // implementations of the same interface (aka interface tests). 33 34 // The interface and its implementations are in this header. 35 #include "prime_tables.h" 36 37 #include "gtest/gtest.h" 38 namespace { 39 // First, we define some factory functions for creating instances of 40 // the implementations. You may be able to skip this step if all your 41 // implementations can be constructed the same way. 42 43 template <class T> 44 PrimeTable* CreatePrimeTable(); 45 46 template <> 47 PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() { 48 return new OnTheFlyPrimeTable; 49 } 50 51 template <> 52 PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() { 53 return new PreCalculatedPrimeTable(10000); 54 } 55 56 // Then we define a test fixture class template. 57 template <class T> 58 class PrimeTableTest : public testing::Test { 59 protected: 60 // The ctor calls the factory function to create a prime table 61 // implemented by T. 62 PrimeTableTest() : table_(CreatePrimeTable<T>()) {} 63 64 ~PrimeTableTest() override { delete table_; } 65 66 // Note that we test an implementation via the base interface 67 // instead of the actual implementation class. This is important 68 // for keeping the tests close to the real world scenario, where the 69 // implementation is invoked via the base interface. It avoids 70 // got-yas where the implementation class has a method that shadows 71 // a method with the same name (but slightly different argument 72 // types) in the base interface, for example. 73 PrimeTable* const table_; 74 }; 75 76 using testing::Types; 77 78 // Google Test offers two ways for reusing tests for different types. 79 // The first is called "typed tests". You should use it if you 80 // already know *all* the types you are gonna exercise when you write 81 // the tests. 82 83 // To write a typed test case, first use 84 // 85 // TYPED_TEST_SUITE(TestCaseName, TypeList); 86 // 87 // to declare it and specify the type parameters. As with TEST_F, 88 // TestCaseName must match the test fixture name. 89 90 // The list of types we want to test. 91 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations; 92 93 TYPED_TEST_SUITE(PrimeTableTest, Implementations); 94 95 // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test, 96 // similar to TEST_F. 97 TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) { 98 // Inside the test body, you can refer to the type parameter by 99 // TypeParam, and refer to the fixture class by TestFixture. We 100 // don't need them in this example. 101 102 // Since we are in the template world, C++ requires explicitly 103 // writing 'this->' when referring to members of the fixture class. 104 // This is something you have to learn to live with. 105 EXPECT_FALSE(this->table_->IsPrime(-5)); 106 EXPECT_FALSE(this->table_->IsPrime(0)); 107 EXPECT_FALSE(this->table_->IsPrime(1)); 108 EXPECT_FALSE(this->table_->IsPrime(4)); 109 EXPECT_FALSE(this->table_->IsPrime(6)); 110 EXPECT_FALSE(this->table_->IsPrime(100)); 111 } 112 113 TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) { 114 EXPECT_TRUE(this->table_->IsPrime(2)); 115 EXPECT_TRUE(this->table_->IsPrime(3)); 116 EXPECT_TRUE(this->table_->IsPrime(5)); 117 EXPECT_TRUE(this->table_->IsPrime(7)); 118 EXPECT_TRUE(this->table_->IsPrime(11)); 119 EXPECT_TRUE(this->table_->IsPrime(131)); 120 } 121 122 TYPED_TEST(PrimeTableTest, CanGetNextPrime) { 123 EXPECT_EQ(2, this->table_->GetNextPrime(0)); 124 EXPECT_EQ(3, this->table_->GetNextPrime(2)); 125 EXPECT_EQ(5, this->table_->GetNextPrime(3)); 126 EXPECT_EQ(7, this->table_->GetNextPrime(5)); 127 EXPECT_EQ(11, this->table_->GetNextPrime(7)); 128 EXPECT_EQ(131, this->table_->GetNextPrime(128)); 129 } 130 131 // That's it! Google Test will repeat each TYPED_TEST for each type 132 // in the type list specified in TYPED_TEST_SUITE. Sit back and be 133 // happy that you don't have to define them multiple times. 134 135 using testing::Types; 136 137 // Sometimes, however, you don't yet know all the types that you want 138 // to test when you write the tests. For example, if you are the 139 // author of an interface and expect other people to implement it, you 140 // might want to write a set of tests to make sure each implementation 141 // conforms to some basic requirements, but you don't know what 142 // implementations will be written in the future. 143 // 144 // How can you write the tests without committing to the type 145 // parameters? That's what "type-parameterized tests" can do for you. 146 // It is a bit more involved than typed tests, but in return you get a 147 // test pattern that can be reused in many contexts, which is a big 148 // win. Here's how you do it: 149 150 // First, define a test fixture class template. Here we just reuse 151 // the PrimeTableTest fixture defined earlier: 152 153 template <class T> 154 class PrimeTableTest2 : public PrimeTableTest<T> { 155 }; 156 157 // Then, declare the test case. The argument is the name of the test 158 // fixture, and also the name of the test case (as usual). The _P 159 // suffix is for "parameterized" or "pattern". 160 TYPED_TEST_SUITE_P(PrimeTableTest2); 161 162 // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test, 163 // similar to what you do with TEST_F. 164 TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) { 165 EXPECT_FALSE(this->table_->IsPrime(-5)); 166 EXPECT_FALSE(this->table_->IsPrime(0)); 167 EXPECT_FALSE(this->table_->IsPrime(1)); 168 EXPECT_FALSE(this->table_->IsPrime(4)); 169 EXPECT_FALSE(this->table_->IsPrime(6)); 170 EXPECT_FALSE(this->table_->IsPrime(100)); 171 } 172 173 TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) { 174 EXPECT_TRUE(this->table_->IsPrime(2)); 175 EXPECT_TRUE(this->table_->IsPrime(3)); 176 EXPECT_TRUE(this->table_->IsPrime(5)); 177 EXPECT_TRUE(this->table_->IsPrime(7)); 178 EXPECT_TRUE(this->table_->IsPrime(11)); 179 EXPECT_TRUE(this->table_->IsPrime(131)); 180 } 181 182 TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) { 183 EXPECT_EQ(2, this->table_->GetNextPrime(0)); 184 EXPECT_EQ(3, this->table_->GetNextPrime(2)); 185 EXPECT_EQ(5, this->table_->GetNextPrime(3)); 186 EXPECT_EQ(7, this->table_->GetNextPrime(5)); 187 EXPECT_EQ(11, this->table_->GetNextPrime(7)); 188 EXPECT_EQ(131, this->table_->GetNextPrime(128)); 189 } 190 191 // Type-parameterized tests involve one extra step: you have to 192 // enumerate the tests you defined: 193 REGISTER_TYPED_TEST_SUITE_P( 194 PrimeTableTest2, // The first argument is the test case name. 195 // The rest of the arguments are the test names. 196 ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime); 197 198 // At this point the test pattern is done. However, you don't have 199 // any real test yet as you haven't said which types you want to run 200 // the tests with. 201 202 // To turn the abstract test pattern into real tests, you instantiate 203 // it with a list of types. Usually the test pattern will be defined 204 // in a .h file, and anyone can #include and instantiate it. You can 205 // even instantiate it more than once in the same program. To tell 206 // different instances apart, you give each of them a name, which will 207 // become part of the test case name and can be used in test filters. 208 209 // The list of types we want to test. Note that it doesn't have to be 210 // defined at the time we write the TYPED_TEST_P()s. 211 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> 212 PrimeTableImplementations; 213 INSTANTIATE_TYPED_TEST_SUITE_P(OnTheFlyAndPreCalculated, // Instance name 214 PrimeTableTest2, // Test case name 215 PrimeTableImplementations); // Type list 216 217 } // namespace