sample6_unittest.cc 8.8 KB

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