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