Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.8 KiB

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