Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2008 Google Inc. |
michael@0 | 2 | // All Rights Reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: vladl@google.com (Vlad Losev) |
michael@0 | 31 | |
michael@0 | 32 | // This sample shows how to test common properties of multiple |
michael@0 | 33 | // implementations of an interface (aka interface tests) using |
michael@0 | 34 | // value-parameterized tests. Each test in the test case has |
michael@0 | 35 | // a parameter that is an interface pointer to an implementation |
michael@0 | 36 | // tested. |
michael@0 | 37 | |
michael@0 | 38 | // The interface and its implementations are in this header. |
michael@0 | 39 | #include "prime_tables.h" |
michael@0 | 40 | |
michael@0 | 41 | #include "gtest/gtest.h" |
michael@0 | 42 | |
michael@0 | 43 | #if GTEST_HAS_PARAM_TEST |
michael@0 | 44 | |
michael@0 | 45 | using ::testing::TestWithParam; |
michael@0 | 46 | using ::testing::Values; |
michael@0 | 47 | |
michael@0 | 48 | // As a general rule, to prevent a test from affecting the tests that come |
michael@0 | 49 | // after it, you should create and destroy the tested objects for each test |
michael@0 | 50 | // instead of reusing them. In this sample we will define a simple factory |
michael@0 | 51 | // function for PrimeTable objects. We will instantiate objects in test's |
michael@0 | 52 | // SetUp() method and delete them in TearDown() method. |
michael@0 | 53 | typedef PrimeTable* CreatePrimeTableFunc(); |
michael@0 | 54 | |
michael@0 | 55 | PrimeTable* CreateOnTheFlyPrimeTable() { |
michael@0 | 56 | return new OnTheFlyPrimeTable(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | template <size_t max_precalculated> |
michael@0 | 60 | PrimeTable* CreatePreCalculatedPrimeTable() { |
michael@0 | 61 | return new PreCalculatedPrimeTable(max_precalculated); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // Inside the test body, fixture constructor, SetUp(), and TearDown() you |
michael@0 | 65 | // can refer to the test parameter by GetParam(). In this case, the test |
michael@0 | 66 | // parameter is a factory function which we call in fixture's SetUp() to |
michael@0 | 67 | // create and store an instance of PrimeTable. |
michael@0 | 68 | class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> { |
michael@0 | 69 | public: |
michael@0 | 70 | virtual ~PrimeTableTest() { delete table_; } |
michael@0 | 71 | virtual void SetUp() { table_ = (*GetParam())(); } |
michael@0 | 72 | virtual void TearDown() { |
michael@0 | 73 | delete table_; |
michael@0 | 74 | table_ = NULL; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | protected: |
michael@0 | 78 | PrimeTable* table_; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { |
michael@0 | 82 | EXPECT_FALSE(table_->IsPrime(-5)); |
michael@0 | 83 | EXPECT_FALSE(table_->IsPrime(0)); |
michael@0 | 84 | EXPECT_FALSE(table_->IsPrime(1)); |
michael@0 | 85 | EXPECT_FALSE(table_->IsPrime(4)); |
michael@0 | 86 | EXPECT_FALSE(table_->IsPrime(6)); |
michael@0 | 87 | EXPECT_FALSE(table_->IsPrime(100)); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | TEST_P(PrimeTableTest, ReturnsTrueForPrimes) { |
michael@0 | 91 | EXPECT_TRUE(table_->IsPrime(2)); |
michael@0 | 92 | EXPECT_TRUE(table_->IsPrime(3)); |
michael@0 | 93 | EXPECT_TRUE(table_->IsPrime(5)); |
michael@0 | 94 | EXPECT_TRUE(table_->IsPrime(7)); |
michael@0 | 95 | EXPECT_TRUE(table_->IsPrime(11)); |
michael@0 | 96 | EXPECT_TRUE(table_->IsPrime(131)); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | TEST_P(PrimeTableTest, CanGetNextPrime) { |
michael@0 | 100 | EXPECT_EQ(2, table_->GetNextPrime(0)); |
michael@0 | 101 | EXPECT_EQ(3, table_->GetNextPrime(2)); |
michael@0 | 102 | EXPECT_EQ(5, table_->GetNextPrime(3)); |
michael@0 | 103 | EXPECT_EQ(7, table_->GetNextPrime(5)); |
michael@0 | 104 | EXPECT_EQ(11, table_->GetNextPrime(7)); |
michael@0 | 105 | EXPECT_EQ(131, table_->GetNextPrime(128)); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | // In order to run value-parameterized tests, you need to instantiate them, |
michael@0 | 109 | // or bind them to a list of values which will be used as test parameters. |
michael@0 | 110 | // You can instantiate them in a different translation module, or even |
michael@0 | 111 | // instantiate them several times. |
michael@0 | 112 | // |
michael@0 | 113 | // Here, we instantiate our tests with a list of two PrimeTable object |
michael@0 | 114 | // factory functions: |
michael@0 | 115 | INSTANTIATE_TEST_CASE_P( |
michael@0 | 116 | OnTheFlyAndPreCalculated, |
michael@0 | 117 | PrimeTableTest, |
michael@0 | 118 | Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>)); |
michael@0 | 119 | |
michael@0 | 120 | #else |
michael@0 | 121 | |
michael@0 | 122 | // Google Test may not support value-parameterized tests with some |
michael@0 | 123 | // compilers. If we use conditional compilation to compile out all |
michael@0 | 124 | // code referring to the gtest_main library, MSVC linker will not link |
michael@0 | 125 | // that library at all and consequently complain about missing entry |
michael@0 | 126 | // point defined in that library (fatal error LNK1561: entry point |
michael@0 | 127 | // must be defined). This dummy test keeps gtest_main linked in. |
michael@0 | 128 | TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {} |
michael@0 | 129 | |
michael@0 | 130 | #endif // GTEST_HAS_PARAM_TEST |