sample7_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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: vladl@google.com (Vlad Losev)
  31. // This sample shows how to test common properties of multiple
  32. // implementations of an interface (aka interface tests) using
  33. // value-parameterized tests. Each test in the test case has
  34. // a parameter that is an interface pointer to an implementation
  35. // tested.
  36. // The interface and its implementations are in this header.
  37. #include "prime_tables.h"
  38. #include "gtest/gtest.h"
  39. #if GTEST_HAS_PARAM_TEST
  40. using ::testing::TestWithParam;
  41. using ::testing::Values;
  42. // As a general rule, to prevent a test from affecting the tests that come
  43. // after it, you should create and destroy the tested objects for each test
  44. // instead of reusing them. In this sample we will define a simple factory
  45. // function for PrimeTable objects. We will instantiate objects in test's
  46. // SetUp() method and delete them in TearDown() method.
  47. typedef PrimeTable* CreatePrimeTableFunc();
  48. PrimeTable* CreateOnTheFlyPrimeTable() {
  49. return new OnTheFlyPrimeTable();
  50. }
  51. template <size_t max_precalculated>
  52. PrimeTable* CreatePreCalculatedPrimeTable() {
  53. return new PreCalculatedPrimeTable(max_precalculated);
  54. }
  55. // Inside the test body, fixture constructor, SetUp(), and TearDown() you
  56. // can refer to the test parameter by GetParam(). In this case, the test
  57. // parameter is a factory function which we call in fixture's SetUp() to
  58. // create and store an instance of PrimeTable.
  59. class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
  60. public:
  61. virtual ~PrimeTableTest() { delete table_; }
  62. virtual void SetUp() { table_ = (*GetParam())(); }
  63. virtual void TearDown() {
  64. delete table_;
  65. table_ = NULL;
  66. }
  67. protected:
  68. PrimeTable* table_;
  69. };
  70. TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
  71. EXPECT_FALSE(table_->IsPrime(-5));
  72. EXPECT_FALSE(table_->IsPrime(0));
  73. EXPECT_FALSE(table_->IsPrime(1));
  74. EXPECT_FALSE(table_->IsPrime(4));
  75. EXPECT_FALSE(table_->IsPrime(6));
  76. EXPECT_FALSE(table_->IsPrime(100));
  77. }
  78. TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
  79. EXPECT_TRUE(table_->IsPrime(2));
  80. EXPECT_TRUE(table_->IsPrime(3));
  81. EXPECT_TRUE(table_->IsPrime(5));
  82. EXPECT_TRUE(table_->IsPrime(7));
  83. EXPECT_TRUE(table_->IsPrime(11));
  84. EXPECT_TRUE(table_->IsPrime(131));
  85. }
  86. TEST_P(PrimeTableTest, CanGetNextPrime) {
  87. EXPECT_EQ(2, table_->GetNextPrime(0));
  88. EXPECT_EQ(3, table_->GetNextPrime(2));
  89. EXPECT_EQ(5, table_->GetNextPrime(3));
  90. EXPECT_EQ(7, table_->GetNextPrime(5));
  91. EXPECT_EQ(11, table_->GetNextPrime(7));
  92. EXPECT_EQ(131, table_->GetNextPrime(128));
  93. }
  94. // In order to run value-parameterized tests, you need to instantiate them,
  95. // or bind them to a list of values which will be used as test parameters.
  96. // You can instantiate them in a different translation module, or even
  97. // instantiate them several times.
  98. //
  99. // Here, we instantiate our tests with a list of two PrimeTable object
  100. // factory functions:
  101. INSTANTIATE_TEST_CASE_P(
  102. OnTheFlyAndPreCalculated,
  103. PrimeTableTest,
  104. Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>));
  105. #else
  106. // Google Test may not support value-parameterized tests with some
  107. // compilers. If we use conditional compilation to compile out all
  108. // code referring to the gtest_main library, MSVC linker will not link
  109. // that library at all and consequently complain about missing entry
  110. // point defined in that library (fatal error LNK1561: entry point
  111. // must be defined). This dummy test keeps gtest_main linked in.
  112. TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
  113. #endif // GTEST_HAS_PARAM_TEST