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