00001 // Copyright 2008 Google Inc. 00002 // All Rights Reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: vladl@google.com (Vlad Losev) 00031 00032 // This sample shows how to test common properties of multiple 00033 // implementations of an interface (aka interface tests) using 00034 // value-parameterized tests. Each test in the test case has 00035 // a parameter that is an interface pointer to an implementation 00036 // tested. 00037 00038 // The interface and its implementations are in this header. 00039 #include "prime_tables.h" 00040 00041 #include "gtest/gtest.h" 00042 00043 #if GTEST_HAS_PARAM_TEST 00044 00045 using ::testing::TestWithParam; 00046 using ::testing::Values; 00047 00048 // As a general rule, to prevent a test from affecting the tests that come 00049 // after it, you should create and destroy the tested objects for each test 00050 // instead of reusing them. In this sample we will define a simple factory 00051 // function for PrimeTable objects. We will instantiate objects in test's 00052 // SetUp() method and delete them in TearDown() method. 00053 typedef PrimeTable* CreatePrimeTableFunc(); 00054 00055 PrimeTable* CreateOnTheFlyPrimeTable() { 00056 return new OnTheFlyPrimeTable(); 00057 } 00058 00059 template <size_t max_precalculated> 00060 PrimeTable* CreatePreCalculatedPrimeTable() { 00061 return new PreCalculatedPrimeTable(max_precalculated); 00062 } 00063 00064 // Inside the test body, fixture constructor, SetUp(), and TearDown() you 00065 // can refer to the test parameter by GetParam(). In this case, the test 00066 // parameter is a factory function which we call in fixture's SetUp() to 00067 // create and store an instance of PrimeTable. 00068 class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> { 00069 public: 00070 virtual ~PrimeTableTest() { delete table_; } 00071 virtual void SetUp() { table_ = (*GetParam())(); } 00072 virtual void TearDown() { 00073 delete table_; 00074 table_ = NULL; 00075 } 00076 00077 protected: 00078 PrimeTable* table_; 00079 }; 00080 00081 TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { 00082 EXPECT_FALSE(table_->IsPrime(-5)); 00083 EXPECT_FALSE(table_->IsPrime(0)); 00084 EXPECT_FALSE(table_->IsPrime(1)); 00085 EXPECT_FALSE(table_->IsPrime(4)); 00086 EXPECT_FALSE(table_->IsPrime(6)); 00087 EXPECT_FALSE(table_->IsPrime(100)); 00088 } 00089 00090 TEST_P(PrimeTableTest, ReturnsTrueForPrimes) { 00091 EXPECT_TRUE(table_->IsPrime(2)); 00092 EXPECT_TRUE(table_->IsPrime(3)); 00093 EXPECT_TRUE(table_->IsPrime(5)); 00094 EXPECT_TRUE(table_->IsPrime(7)); 00095 EXPECT_TRUE(table_->IsPrime(11)); 00096 EXPECT_TRUE(table_->IsPrime(131)); 00097 } 00098 00099 TEST_P(PrimeTableTest, CanGetNextPrime) { 00100 EXPECT_EQ(2, table_->GetNextPrime(0)); 00101 EXPECT_EQ(3, table_->GetNextPrime(2)); 00102 EXPECT_EQ(5, table_->GetNextPrime(3)); 00103 EXPECT_EQ(7, table_->GetNextPrime(5)); 00104 EXPECT_EQ(11, table_->GetNextPrime(7)); 00105 EXPECT_EQ(131, table_->GetNextPrime(128)); 00106 } 00107 00108 // In order to run value-parameterized tests, you need to instantiate them, 00109 // or bind them to a list of values which will be used as test parameters. 00110 // You can instantiate them in a different translation module, or even 00111 // instantiate them several times. 00112 // 00113 // Here, we instantiate our tests with a list of two PrimeTable object 00114 // factory functions: 00115 INSTANTIATE_TEST_CASE_P( 00116 OnTheFlyAndPreCalculated, 00117 PrimeTableTest, 00118 Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>)); 00119 00120 #else 00121 00122 // Google Test may not support value-parameterized tests with some 00123 // compilers. If we use conditional compilation to compile out all 00124 // code referring to the gtest_main library, MSVC linker will not link 00125 // that library at all and consequently complain about missing entry 00126 // point defined in that library (fatal error LNK1561: entry point 00127 // must be defined). This dummy test keeps gtest_main linked in. 00128 TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {} 00129 00130 #endif // GTEST_HAS_PARAM_TEST