blob: ddf2f1c13bbe03481344936b0a72dbb2a0b28424 [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001// 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.
Austin Schuh889ac432018-10-29 22:57:02 -070029
Austin Schuh0cbef622015-09-06 17:34:52 -070030
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"
Austin Schuh889ac432018-10-29 22:57:02 -070038namespace {
Austin Schuh0cbef622015-09-06 17:34:52 -070039// 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
43template <class T>
44PrimeTable* CreatePrimeTable();
45
46template <>
47PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
48 return new OnTheFlyPrimeTable;
49}
50
51template <>
52PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
53 return new PreCalculatedPrimeTable(10000);
54}
55
56// Then we define a test fixture class template.
57template <class T>
58class 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 virtual ~PrimeTableTest() { 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#if GTEST_HAS_TYPED_TEST
77
78using testing::Types;
79
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
83// the tests.
84
85// To write a typed test case, first use
86//
87// TYPED_TEST_CASE(TestCaseName, TypeList);
88//
89// to declare it and specify the type parameters. As with TEST_F,
90// TestCaseName must match the test fixture name.
91
92// The list of types we want to test.
93typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
94
95TYPED_TEST_CASE(PrimeTableTest, Implementations);
96
97// Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
98// similar to TEST_F.
99TYPED_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.
103
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));
113}
114
115TYPED_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));
122}
123
124TYPED_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));
131}
132
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.
136
137#endif // GTEST_HAS_TYPED_TEST
138
139#if GTEST_HAS_TYPED_TEST_P
140
141using testing::Types;
142
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.
149//
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:
155
156// First, define a test fixture class template. Here we just reuse
157// the PrimeTableTest fixture defined earlier:
158
159template <class T>
160class PrimeTableTest2 : public PrimeTableTest<T> {
161};
162
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".
166TYPED_TEST_CASE_P(PrimeTableTest2);
167
168// Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
169// similar to what you do with TEST_F.
170TYPED_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));
177}
178
179TYPED_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));
186}
187
188TYPED_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));
195}
196
197// Type-parameterized tests involve one extra step: you have to
198// enumerate the tests you defined:
199REGISTER_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);
203
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
206// the tests with.
207
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.
214
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.
217typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
218 PrimeTableImplementations;
219INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name
220 PrimeTableTest2, // Test case name
221 PrimeTableImplementations); // Type list
222
223#endif // GTEST_HAS_TYPED_TEST_P
Austin Schuh889ac432018-10-29 22:57:02 -0700224} // namespace