sample7_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 an interface (aka interface tests) using
  31. // value-parameterized tests. Each test in the test case has
  32. // a parameter that is an interface pointer to an implementation
  33. // tested.
  34. // The interface and its implementations are in this header.
  35. #include "prime_tables.h"
  36. #include "gtest/gtest.h"
  37. namespace {
  38. using ::testing::TestWithParam;
  39. using ::testing::Values;
  40. // As a general rule, to prevent a test from affecting the tests that come
  41. // after it, you should create and destroy the tested objects for each test
  42. // instead of reusing them. In this sample we will define a simple factory
  43. // function for PrimeTable objects. We will instantiate objects in test's
  44. // SetUp() method and delete them in TearDown() method.
  45. typedef PrimeTable* CreatePrimeTableFunc();
  46. PrimeTable* CreateOnTheFlyPrimeTable() { return new OnTheFlyPrimeTable(); }
  47. template <size_t max_precalculated>
  48. PrimeTable* CreatePreCalculatedPrimeTable() {
  49. return new PreCalculatedPrimeTable(max_precalculated);
  50. }
  51. // Inside the test body, fixture constructor, SetUp(), and TearDown() you
  52. // can refer to the test parameter by GetParam(). In this case, the test
  53. // parameter is a factory function which we call in fixture's SetUp() to
  54. // create and store an instance of PrimeTable.
  55. class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
  56. public:
  57. ~PrimeTableTestSmpl7() override { delete table_; }
  58. void SetUp() override { table_ = (*GetParam())(); }
  59. void TearDown() override {
  60. delete table_;
  61. table_ = nullptr;
  62. }
  63. protected:
  64. PrimeTable* table_;
  65. };
  66. TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
  67. EXPECT_FALSE(table_->IsPrime(-5));
  68. EXPECT_FALSE(table_->IsPrime(0));
  69. EXPECT_FALSE(table_->IsPrime(1));
  70. EXPECT_FALSE(table_->IsPrime(4));
  71. EXPECT_FALSE(table_->IsPrime(6));
  72. EXPECT_FALSE(table_->IsPrime(100));
  73. }
  74. TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
  75. EXPECT_TRUE(table_->IsPrime(2));
  76. EXPECT_TRUE(table_->IsPrime(3));
  77. EXPECT_TRUE(table_->IsPrime(5));
  78. EXPECT_TRUE(table_->IsPrime(7));
  79. EXPECT_TRUE(table_->IsPrime(11));
  80. EXPECT_TRUE(table_->IsPrime(131));
  81. }
  82. TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
  83. EXPECT_EQ(2, table_->GetNextPrime(0));
  84. EXPECT_EQ(3, table_->GetNextPrime(2));
  85. EXPECT_EQ(5, table_->GetNextPrime(3));
  86. EXPECT_EQ(7, table_->GetNextPrime(5));
  87. EXPECT_EQ(11, table_->GetNextPrime(7));
  88. EXPECT_EQ(131, table_->GetNextPrime(128));
  89. }
  90. // In order to run value-parameterized tests, you need to instantiate them,
  91. // or bind them to a list of values which will be used as test parameters.
  92. // You can instantiate them in a different translation module, or even
  93. // instantiate them several times.
  94. //
  95. // Here, we instantiate our tests with a list of two PrimeTable object
  96. // factory functions:
  97. INSTANTIATE_TEST_SUITE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
  98. Values(&CreateOnTheFlyPrimeTable,
  99. &CreatePreCalculatedPrimeTable<1000>));
  100. } // namespace