1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // This sample shows how to test common properties of multiple
32 // implementations of the same interface (aka interface tests).
34 // The interface and its implementations are in this header.
35 #include "prime_tables.h"
37 #include "gtest/gtest.h"
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.
44 PrimeTable* CreatePrimeTable();
47 PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
48 return new OnTheFlyPrimeTable;
52 PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
53 return new PreCalculatedPrimeTable(10000);
56 // Then we define a test fixture class template.
58 class PrimeTableTest : public testing::Test {
60 // The ctor calls the factory function to create a prime table
62 PrimeTableTest() : table_(CreatePrimeTable<T>()) {}
64 virtual ~PrimeTableTest() { delete table_; }
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_;
76 #if GTEST_HAS_TYPED_TEST
80 // Google Test offers two ways for reusing tests for different types.
81 // The first is called "typed tests". You should use it if you
82 // already know *all* the types you are gonna exercise when you write
85 // To write a typed test case, first use
87 // TYPED_TEST_CASE(TestCaseName, TypeList);
89 // to declare it and specify the type parameters. As with TEST_F,
90 // TestCaseName must match the test fixture name.
92 // The list of types we want to test.
93 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
95 TYPED_TEST_CASE(PrimeTableTest, Implementations);
97 // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
99 TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
100 // Inside the test body, you can refer to the type parameter by
101 // TypeParam, and refer to the fixture class by TestFixture. We
102 // don't need them in this example.
104 // Since we are in the template world, C++ requires explicitly
105 // writing 'this->' when referring to members of the fixture class.
106 // This is something you have to learn to live with.
107 EXPECT_FALSE(this->table_->IsPrime(-5));
108 EXPECT_FALSE(this->table_->IsPrime(0));
109 EXPECT_FALSE(this->table_->IsPrime(1));
110 EXPECT_FALSE(this->table_->IsPrime(4));
111 EXPECT_FALSE(this->table_->IsPrime(6));
112 EXPECT_FALSE(this->table_->IsPrime(100));
115 TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
116 EXPECT_TRUE(this->table_->IsPrime(2));
117 EXPECT_TRUE(this->table_->IsPrime(3));
118 EXPECT_TRUE(this->table_->IsPrime(5));
119 EXPECT_TRUE(this->table_->IsPrime(7));
120 EXPECT_TRUE(this->table_->IsPrime(11));
121 EXPECT_TRUE(this->table_->IsPrime(131));
124 TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
125 EXPECT_EQ(2, this->table_->GetNextPrime(0));
126 EXPECT_EQ(3, this->table_->GetNextPrime(2));
127 EXPECT_EQ(5, this->table_->GetNextPrime(3));
128 EXPECT_EQ(7, this->table_->GetNextPrime(5));
129 EXPECT_EQ(11, this->table_->GetNextPrime(7));
130 EXPECT_EQ(131, this->table_->GetNextPrime(128));
133 // That's it! Google Test will repeat each TYPED_TEST for each type
134 // in the type list specified in TYPED_TEST_CASE. Sit back and be
135 // happy that you don't have to define them multiple times.
137 #endif // GTEST_HAS_TYPED_TEST
139 #if GTEST_HAS_TYPED_TEST_P
141 using testing::Types;
143 // Sometimes, however, you don't yet know all the types that you want
144 // to test when you write the tests. For example, if you are the
145 // author of an interface and expect other people to implement it, you
146 // might want to write a set of tests to make sure each implementation
147 // conforms to some basic requirements, but you don't know what
148 // implementations will be written in the future.
150 // How can you write the tests without committing to the type
151 // parameters? That's what "type-parameterized tests" can do for you.
152 // It is a bit more involved than typed tests, but in return you get a
153 // test pattern that can be reused in many contexts, which is a big
154 // win. Here's how you do it:
156 // First, define a test fixture class template. Here we just reuse
157 // the PrimeTableTest fixture defined earlier:
160 class PrimeTableTest2 : public PrimeTableTest<T> {
163 // Then, declare the test case. The argument is the name of the test
164 // fixture, and also the name of the test case (as usual). The _P
165 // suffix is for "parameterized" or "pattern".
166 TYPED_TEST_CASE_P(PrimeTableTest2);
168 // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
169 // similar to what you do with TEST_F.
170 TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
171 EXPECT_FALSE(this->table_->IsPrime(-5));
172 EXPECT_FALSE(this->table_->IsPrime(0));
173 EXPECT_FALSE(this->table_->IsPrime(1));
174 EXPECT_FALSE(this->table_->IsPrime(4));
175 EXPECT_FALSE(this->table_->IsPrime(6));
176 EXPECT_FALSE(this->table_->IsPrime(100));
179 TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
180 EXPECT_TRUE(this->table_->IsPrime(2));
181 EXPECT_TRUE(this->table_->IsPrime(3));
182 EXPECT_TRUE(this->table_->IsPrime(5));
183 EXPECT_TRUE(this->table_->IsPrime(7));
184 EXPECT_TRUE(this->table_->IsPrime(11));
185 EXPECT_TRUE(this->table_->IsPrime(131));
188 TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
189 EXPECT_EQ(2, this->table_->GetNextPrime(0));
190 EXPECT_EQ(3, this->table_->GetNextPrime(2));
191 EXPECT_EQ(5, this->table_->GetNextPrime(3));
192 EXPECT_EQ(7, this->table_->GetNextPrime(5));
193 EXPECT_EQ(11, this->table_->GetNextPrime(7));
194 EXPECT_EQ(131, this->table_->GetNextPrime(128));
197 // Type-parameterized tests involve one extra step: you have to
198 // enumerate the tests you defined:
199 REGISTER_TYPED_TEST_CASE_P(
200 PrimeTableTest2, // The first argument is the test case name.
201 // The rest of the arguments are the test names.
202 ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
204 // At this point the test pattern is done. However, you don't have
205 // any real test yet as you haven't said which types you want to run
208 // To turn the abstract test pattern into real tests, you instantiate
209 // it with a list of types. Usually the test pattern will be defined
210 // in a .h file, and anyone can #include and instantiate it. You can
211 // even instantiate it more than once in the same program. To tell
212 // different instances apart, you give each of them a name, which will
213 // become part of the test case name and can be used in test filters.
215 // The list of types we want to test. Note that it doesn't have to be
216 // defined at the time we write the TYPED_TEST_P()s.
217 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
218 PrimeTableImplementations;
219 INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name
220 PrimeTableTest2, // Test case name
221 PrimeTableImplementations); // Type list
223 #endif // GTEST_HAS_TYPED_TEST_P