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 code relying on some global flag variables. michael@0: // Combine() helps with generating all possible combinations of such flags, michael@0: // and each test is given one combination as a parameter. michael@0: michael@0: // Use class definitions to test from this header. michael@0: #include "prime_tables.h" michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #if GTEST_HAS_COMBINE michael@0: michael@0: // Suppose we want to introduce a new, improved implementation of PrimeTable michael@0: // which combines speed of PrecalcPrimeTable and versatility of michael@0: // OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both michael@0: // PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more michael@0: // appropriate under the circumstances. But in low memory conditions, it can be michael@0: // told to instantiate without PrecalcPrimeTable instance at all and use only michael@0: // OnTheFlyPrimeTable. michael@0: class HybridPrimeTable : public PrimeTable { michael@0: public: michael@0: HybridPrimeTable(bool force_on_the_fly, int max_precalculated) michael@0: : on_the_fly_impl_(new OnTheFlyPrimeTable), michael@0: precalc_impl_(force_on_the_fly ? NULL : michael@0: new PreCalculatedPrimeTable(max_precalculated)), michael@0: max_precalculated_(max_precalculated) {} michael@0: virtual ~HybridPrimeTable() { michael@0: delete on_the_fly_impl_; michael@0: delete precalc_impl_; michael@0: } michael@0: michael@0: virtual bool IsPrime(int n) const { michael@0: if (precalc_impl_ != NULL && n < max_precalculated_) michael@0: return precalc_impl_->IsPrime(n); michael@0: else michael@0: return on_the_fly_impl_->IsPrime(n); michael@0: } michael@0: michael@0: virtual int GetNextPrime(int p) const { michael@0: int next_prime = -1; michael@0: if (precalc_impl_ != NULL && p < max_precalculated_) michael@0: next_prime = precalc_impl_->GetNextPrime(p); michael@0: michael@0: return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p); michael@0: } michael@0: michael@0: private: michael@0: OnTheFlyPrimeTable* on_the_fly_impl_; michael@0: PreCalculatedPrimeTable* precalc_impl_; michael@0: int max_precalculated_; michael@0: }; michael@0: michael@0: using ::testing::TestWithParam; michael@0: using ::testing::Bool; michael@0: using ::testing::Values; michael@0: using ::testing::Combine; michael@0: michael@0: // To test all code paths for HybridPrimeTable we must test it with numbers michael@0: // both within and outside PreCalculatedPrimeTable's capacity and also with michael@0: // PreCalculatedPrimeTable disabled. We do this by defining fixture which will michael@0: // accept different combinations of parameters for instantiating a michael@0: // HybridPrimeTable instance. michael@0: class PrimeTableTest : public TestWithParam< ::std::tr1::tuple > { michael@0: protected: michael@0: virtual void SetUp() { michael@0: // This can be written as michael@0: // michael@0: // bool force_on_the_fly; michael@0: // int max_precalculated; michael@0: // tie(force_on_the_fly, max_precalculated) = GetParam(); michael@0: // michael@0: // once the Google C++ Style Guide allows use of ::std::tr1::tie. michael@0: // michael@0: bool force_on_the_fly = ::std::tr1::get<0>(GetParam()); michael@0: int max_precalculated = ::std::tr1::get<1>(GetParam()); michael@0: table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated); michael@0: } michael@0: virtual void TearDown() { michael@0: delete table_; michael@0: table_ = NULL; michael@0: } michael@0: HybridPrimeTable* table_; michael@0: }; michael@0: michael@0: TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { michael@0: // Inside the test body, you can refer to the test parameter by GetParam(). michael@0: // In this case, the test parameter is a PrimeTable interface pointer which michael@0: // we can use directly. michael@0: // Please note that you can also save it in the fixture's SetUp() method michael@0: // or constructor and use saved copy in the tests. michael@0: 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 parameters. We must combine michael@0: // all variations of the boolean flag suppressing PrecalcPrimeTable and some michael@0: // meaningful values for tests. We choose a small value (1), and a value that michael@0: // will put some of the tested numbers beyond the capability of the michael@0: // PrecalcPrimeTable instance and some inside it (10). Combine will produce all michael@0: // possible combinations. michael@0: INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters, michael@0: PrimeTableTest, michael@0: Combine(Bool(), Values(1, 10))); michael@0: michael@0: #else michael@0: michael@0: // Google Test may not support Combine() with some compilers. If we michael@0: // use conditional compilation to compile out all code referring to michael@0: // the gtest_main library, MSVC linker will not link that library at michael@0: // all and consequently complain about missing entry point defined in michael@0: // that library (fatal error LNK1561: entry point must be michael@0: // defined). This dummy test keeps gtest_main linked in. michael@0: TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {} michael@0: michael@0: #endif // GTEST_HAS_COMBINE