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 code relying on some global flag variables. 00033 // Combine() helps with generating all possible combinations of such flags, 00034 // and each test is given one combination as a parameter. 00035 00036 // Use class definitions to test from this header. 00037 #include "prime_tables.h" 00038 00039 #include "gtest/gtest.h" 00040 00041 #if GTEST_HAS_COMBINE 00042 00043 // Suppose we want to introduce a new, improved implementation of PrimeTable 00044 // which combines speed of PrecalcPrimeTable and versatility of 00045 // OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both 00046 // PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more 00047 // appropriate under the circumstances. But in low memory conditions, it can be 00048 // told to instantiate without PrecalcPrimeTable instance at all and use only 00049 // OnTheFlyPrimeTable. 00050 class HybridPrimeTable : public PrimeTable { 00051 public: 00052 HybridPrimeTable(bool force_on_the_fly, int max_precalculated) 00053 : on_the_fly_impl_(new OnTheFlyPrimeTable), 00054 precalc_impl_(force_on_the_fly ? NULL : 00055 new PreCalculatedPrimeTable(max_precalculated)), 00056 max_precalculated_(max_precalculated) {} 00057 virtual ~HybridPrimeTable() { 00058 delete on_the_fly_impl_; 00059 delete precalc_impl_; 00060 } 00061 00062 virtual bool IsPrime(int n) const { 00063 if (precalc_impl_ != NULL && n < max_precalculated_) 00064 return precalc_impl_->IsPrime(n); 00065 else 00066 return on_the_fly_impl_->IsPrime(n); 00067 } 00068 00069 virtual int GetNextPrime(int p) const { 00070 int next_prime = -1; 00071 if (precalc_impl_ != NULL && p < max_precalculated_) 00072 next_prime = precalc_impl_->GetNextPrime(p); 00073 00074 return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p); 00075 } 00076 00077 private: 00078 OnTheFlyPrimeTable* on_the_fly_impl_; 00079 PreCalculatedPrimeTable* precalc_impl_; 00080 int max_precalculated_; 00081 }; 00082 00083 using ::testing::TestWithParam; 00084 using ::testing::Bool; 00085 using ::testing::Values; 00086 using ::testing::Combine; 00087 00088 // To test all code paths for HybridPrimeTable we must test it with numbers 00089 // both within and outside PreCalculatedPrimeTable's capacity and also with 00090 // PreCalculatedPrimeTable disabled. We do this by defining fixture which will 00091 // accept different combinations of parameters for instantiating a 00092 // HybridPrimeTable instance. 00093 class PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > { 00094 protected: 00095 virtual void SetUp() { 00096 // This can be written as 00097 // 00098 // bool force_on_the_fly; 00099 // int max_precalculated; 00100 // tie(force_on_the_fly, max_precalculated) = GetParam(); 00101 // 00102 // once the Google C++ Style Guide allows use of ::std::tr1::tie. 00103 // 00104 bool force_on_the_fly = ::testing::get<0>(GetParam()); 00105 int max_precalculated = ::testing::get<1>(GetParam()); 00106 table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated); 00107 } 00108 virtual void TearDown() { 00109 delete table_; 00110 table_ = NULL; 00111 } 00112 HybridPrimeTable* table_; 00113 }; 00114 00115 TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { 00116 // Inside the test body, you can refer to the test parameter by GetParam(). 00117 // In this case, the test parameter is a PrimeTable interface pointer which 00118 // we can use directly. 00119 // Please note that you can also save it in the fixture's SetUp() method 00120 // or constructor and use saved copy in the tests. 00121 00122 EXPECT_FALSE(table_->IsPrime(-5)); 00123 EXPECT_FALSE(table_->IsPrime(0)); 00124 EXPECT_FALSE(table_->IsPrime(1)); 00125 EXPECT_FALSE(table_->IsPrime(4)); 00126 EXPECT_FALSE(table_->IsPrime(6)); 00127 EXPECT_FALSE(table_->IsPrime(100)); 00128 } 00129 00130 TEST_P(PrimeTableTest, ReturnsTrueForPrimes) { 00131 EXPECT_TRUE(table_->IsPrime(2)); 00132 EXPECT_TRUE(table_->IsPrime(3)); 00133 EXPECT_TRUE(table_->IsPrime(5)); 00134 EXPECT_TRUE(table_->IsPrime(7)); 00135 EXPECT_TRUE(table_->IsPrime(11)); 00136 EXPECT_TRUE(table_->IsPrime(131)); 00137 } 00138 00139 TEST_P(PrimeTableTest, CanGetNextPrime) { 00140 EXPECT_EQ(2, table_->GetNextPrime(0)); 00141 EXPECT_EQ(3, table_->GetNextPrime(2)); 00142 EXPECT_EQ(5, table_->GetNextPrime(3)); 00143 EXPECT_EQ(7, table_->GetNextPrime(5)); 00144 EXPECT_EQ(11, table_->GetNextPrime(7)); 00145 EXPECT_EQ(131, table_->GetNextPrime(128)); 00146 } 00147 00148 // In order to run value-parameterized tests, you need to instantiate them, 00149 // or bind them to a list of values which will be used as test parameters. 00150 // You can instantiate them in a different translation module, or even 00151 // instantiate them several times. 00152 // 00153 // Here, we instantiate our tests with a list of parameters. We must combine 00154 // all variations of the boolean flag suppressing PrecalcPrimeTable and some 00155 // meaningful values for tests. We choose a small value (1), and a value that 00156 // will put some of the tested numbers beyond the capability of the 00157 // PrecalcPrimeTable instance and some inside it (10). Combine will produce all 00158 // possible combinations. 00159 INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters, 00160 PrimeTableTest, 00161 Combine(Bool(), Values(1, 10))); 00162 00163 #else 00164 00165 // Google Test may not support Combine() with some compilers. If we 00166 // use conditional compilation to compile out all code referring to 00167 // the gtest_main library, MSVC linker will not link that library at 00168 // all and consequently complain about missing entry point defined in 00169 // that library (fatal error LNK1561: entry point must be 00170 // defined). This dummy test keeps gtest_main linked in. 00171 TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {} 00172 00173 #endif // GTEST_HAS_COMBINE