sample6_unittest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. ~PrimeTableTest() override { 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. using testing::Types;
  66. // Google Test offers two ways for reusing tests for different types.
  67. // The first is called "typed tests". You should use it if you
  68. // already know *all* the types you are gonna exercise when you write
  69. // the tests.
  70. // To write a typed test case, first use
  71. //
  72. // TYPED_TEST_SUITE(TestCaseName, TypeList);
  73. //
  74. // to declare it and specify the type parameters. As with TEST_F,
  75. // TestCaseName must match the test fixture name.
  76. // The list of types we want to test.
  77. typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
  78. TYPED_TEST_SUITE(PrimeTableTest, Implementations);
  79. // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
  80. // similar to TEST_F.
  81. TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
  82. // Inside the test body, you can refer to the type parameter by
  83. // TypeParam, and refer to the fixture class by TestFixture. We
  84. // don't need them in this example.
  85. // Since we are in the template world, C++ requires explicitly
  86. // writing 'this->' when referring to members of the fixture class.
  87. // This is something you have to learn to live with.
  88. EXPECT_FALSE(this->table_->IsPrime(-5));
  89. EXPECT_FALSE(this->table_->IsPrime(0));
  90. EXPECT_FALSE(this->table_->IsPrime(1));
  91. EXPECT_FALSE(this->table_->IsPrime(4));
  92. EXPECT_FALSE(this->table_->IsPrime(6));
  93. EXPECT_FALSE(this->table_->IsPrime(100));
  94. }
  95. TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
  96. EXPECT_TRUE(this->table_->IsPrime(2));
  97. EXPECT_TRUE(this->table_->IsPrime(3));
  98. EXPECT_TRUE(this->table_->IsPrime(5));
  99. EXPECT_TRUE(this->table_->IsPrime(7));
  100. EXPECT_TRUE(this->table_->IsPrime(11));
  101. EXPECT_TRUE(this->table_->IsPrime(131));
  102. }
  103. TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
  104. EXPECT_EQ(2, this->table_->GetNextPrime(0));
  105. EXPECT_EQ(3, this->table_->GetNextPrime(2));
  106. EXPECT_EQ(5, this->table_->GetNextPrime(3));
  107. EXPECT_EQ(7, this->table_->GetNextPrime(5));
  108. EXPECT_EQ(11, this->table_->GetNextPrime(7));
  109. EXPECT_EQ(131, this->table_->GetNextPrime(128));
  110. }
  111. // That's it! Google Test will repeat each TYPED_TEST for each type
  112. // in the type list specified in TYPED_TEST_SUITE. Sit back and be
  113. // happy that you don't have to define them multiple times.
  114. using testing::Types;
  115. // Sometimes, however, you don't yet know all the types that you want
  116. // to test when you write the tests. For example, if you are the
  117. // author of an interface and expect other people to implement it, you
  118. // might want to write a set of tests to make sure each implementation
  119. // conforms to some basic requirements, but you don't know what
  120. // implementations will be written in the future.
  121. //
  122. // How can you write the tests without committing to the type
  123. // parameters? That's what "type-parameterized tests" can do for you.
  124. // It is a bit more involved than typed tests, but in return you get a
  125. // test pattern that can be reused in many contexts, which is a big
  126. // win. Here's how you do it:
  127. // First, define a test fixture class template. Here we just reuse
  128. // the PrimeTableTest fixture defined earlier:
  129. template <class T>
  130. class PrimeTableTest2 : public PrimeTableTest<T> {
  131. };
  132. // Then, declare the test case. The argument is the name of the test
  133. // fixture, and also the name of the test case (as usual). The _P
  134. // suffix is for "parameterized" or "pattern".
  135. TYPED_TEST_SUITE_P(PrimeTableTest2);
  136. // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
  137. // similar to what you do with TEST_F.
  138. TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
  139. EXPECT_FALSE(this->table_->IsPrime(-5));
  140. EXPECT_FALSE(this->table_->IsPrime(0));
  141. EXPECT_FALSE(this->table_->IsPrime(1));
  142. EXPECT_FALSE(this->table_->IsPrime(4));
  143. EXPECT_FALSE(this->table_->IsPrime(6));
  144. EXPECT_FALSE(this->table_->IsPrime(100));
  145. }
  146. TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
  147. EXPECT_TRUE(this->table_->IsPrime(2));
  148. EXPECT_TRUE(this->table_->IsPrime(3));
  149. EXPECT_TRUE(this->table_->IsPrime(5));
  150. EXPECT_TRUE(this->table_->IsPrime(7));
  151. EXPECT_TRUE(this->table_->IsPrime(11));
  152. EXPECT_TRUE(this->table_->IsPrime(131));
  153. }
  154. TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
  155. EXPECT_EQ(2, this->table_->GetNextPrime(0));
  156. EXPECT_EQ(3, this->table_->GetNextPrime(2));
  157. EXPECT_EQ(5, this->table_->GetNextPrime(3));
  158. EXPECT_EQ(7, this->table_->GetNextPrime(5));
  159. EXPECT_EQ(11, this->table_->GetNextPrime(7));
  160. EXPECT_EQ(131, this->table_->GetNextPrime(128));
  161. }
  162. // Type-parameterized tests involve one extra step: you have to
  163. // enumerate the tests you defined:
  164. REGISTER_TYPED_TEST_SUITE_P(
  165. PrimeTableTest2, // The first argument is the test case name.
  166. // The rest of the arguments are the test names.
  167. ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
  168. // At this point the test pattern is done. However, you don't have
  169. // any real test yet as you haven't said which types you want to run
  170. // the tests with.
  171. // To turn the abstract test pattern into real tests, you instantiate
  172. // it with a list of types. Usually the test pattern will be defined
  173. // in a .h file, and anyone can #include and instantiate it. You can
  174. // even instantiate it more than once in the same program. To tell
  175. // different instances apart, you give each of them a name, which will
  176. // become part of the test case name and can be used in test filters.
  177. // The list of types we want to test. Note that it doesn't have to be
  178. // defined at the time we write the TYPED_TEST_P()s.
  179. typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
  180. PrimeTableImplementations;
  181. INSTANTIATE_TYPED_TEST_SUITE_P(OnTheFlyAndPreCalculated, // Instance name
  182. PrimeTableTest2, // Test case name
  183. PrimeTableImplementations); // Type list
  184. } // namespace