michael@0: // Copyright 2008 Google Inc. michael@0: // All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: vladl@google.com (Vlad Losev) michael@0: michael@0: // This sample shows how to test common properties of multiple michael@0: // implementations of an interface (aka interface tests) using michael@0: // value-parameterized tests. Each test in the test case has michael@0: // a parameter that is an interface pointer to an implementation michael@0: // tested. michael@0: michael@0: // The interface and its implementations are in this header. michael@0: #include "prime_tables.h" michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #if GTEST_HAS_PARAM_TEST michael@0: michael@0: using ::testing::TestWithParam; michael@0: using ::testing::Values; michael@0: michael@0: // As a general rule, to prevent a test from affecting the tests that come michael@0: // after it, you should create and destroy the tested objects for each test michael@0: // instead of reusing them. In this sample we will define a simple factory michael@0: // function for PrimeTable objects. We will instantiate objects in test's michael@0: // SetUp() method and delete them in TearDown() method. michael@0: typedef PrimeTable* CreatePrimeTableFunc(); michael@0: michael@0: PrimeTable* CreateOnTheFlyPrimeTable() { michael@0: return new OnTheFlyPrimeTable(); michael@0: } michael@0: michael@0: template michael@0: PrimeTable* CreatePreCalculatedPrimeTable() { michael@0: return new PreCalculatedPrimeTable(max_precalculated); michael@0: } michael@0: michael@0: // Inside the test body, fixture constructor, SetUp(), and TearDown() you michael@0: // can refer to the test parameter by GetParam(). In this case, the test michael@0: // parameter is a factory function which we call in fixture's SetUp() to michael@0: // create and store an instance of PrimeTable. michael@0: class PrimeTableTest : public TestWithParam { michael@0: public: michael@0: virtual ~PrimeTableTest() { delete table_; } michael@0: virtual void SetUp() { table_ = (*GetParam())(); } michael@0: virtual void TearDown() { michael@0: delete table_; michael@0: table_ = NULL; michael@0: } michael@0: michael@0: protected: michael@0: PrimeTable* table_; michael@0: }; michael@0: michael@0: TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { michael@0: EXPECT_FALSE(table_->IsPrime(-5)); michael@0: EXPECT_FALSE(table_->IsPrime(0)); michael@0: EXPECT_FALSE(table_->IsPrime(1)); michael@0: EXPECT_FALSE(table_->IsPrime(4)); michael@0: EXPECT_FALSE(table_->IsPrime(6)); michael@0: EXPECT_FALSE(table_->IsPrime(100)); michael@0: } michael@0: michael@0: TEST_P(PrimeTableTest, ReturnsTrueForPrimes) { michael@0: EXPECT_TRUE(table_->IsPrime(2)); michael@0: EXPECT_TRUE(table_->IsPrime(3)); michael@0: EXPECT_TRUE(table_->IsPrime(5)); michael@0: EXPECT_TRUE(table_->IsPrime(7)); michael@0: EXPECT_TRUE(table_->IsPrime(11)); michael@0: EXPECT_TRUE(table_->IsPrime(131)); michael@0: } michael@0: michael@0: TEST_P(PrimeTableTest, CanGetNextPrime) { michael@0: EXPECT_EQ(2, table_->GetNextPrime(0)); michael@0: EXPECT_EQ(3, table_->GetNextPrime(2)); michael@0: EXPECT_EQ(5, table_->GetNextPrime(3)); michael@0: EXPECT_EQ(7, table_->GetNextPrime(5)); michael@0: EXPECT_EQ(11, table_->GetNextPrime(7)); michael@0: EXPECT_EQ(131, table_->GetNextPrime(128)); michael@0: } michael@0: michael@0: // In order to run value-parameterized tests, you need to instantiate them, michael@0: // or bind them to a list of values which will be used as test parameters. michael@0: // You can instantiate them in a different translation module, or even michael@0: // instantiate them several times. michael@0: // michael@0: // Here, we instantiate our tests with a list of two PrimeTable object michael@0: // factory functions: michael@0: INSTANTIATE_TEST_CASE_P( michael@0: OnTheFlyAndPreCalculated, michael@0: PrimeTableTest, michael@0: Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>)); michael@0: michael@0: #else michael@0: michael@0: // Google Test may not support value-parameterized tests with some michael@0: // compilers. If we use conditional compilation to compile out all michael@0: // code referring to the gtest_main library, MSVC linker will not link michael@0: // that library at all and consequently complain about missing entry michael@0: // point defined in that library (fatal error LNK1561: entry point michael@0: // must be defined). This dummy test keeps gtest_main linked in. michael@0: TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {} michael@0: michael@0: #endif // GTEST_HAS_PARAM_TEST