113481Sgiacomo.travaglini@arm.com// Copyright 2008 Google Inc. 213481Sgiacomo.travaglini@arm.com// All Rights Reserved. 313481Sgiacomo.travaglini@arm.com// 413481Sgiacomo.travaglini@arm.com// Redistribution and use in source and binary forms, with or without 513481Sgiacomo.travaglini@arm.com// modification, are permitted provided that the following conditions are 613481Sgiacomo.travaglini@arm.com// met: 713481Sgiacomo.travaglini@arm.com// 813481Sgiacomo.travaglini@arm.com// * Redistributions of source code must retain the above copyright 913481Sgiacomo.travaglini@arm.com// notice, this list of conditions and the following disclaimer. 1013481Sgiacomo.travaglini@arm.com// * Redistributions in binary form must reproduce the above 1113481Sgiacomo.travaglini@arm.com// copyright notice, this list of conditions and the following disclaimer 1213481Sgiacomo.travaglini@arm.com// in the documentation and/or other materials provided with the 1313481Sgiacomo.travaglini@arm.com// distribution. 1413481Sgiacomo.travaglini@arm.com// * Neither the name of Google Inc. nor the names of its 1513481Sgiacomo.travaglini@arm.com// contributors may be used to endorse or promote products derived from 1613481Sgiacomo.travaglini@arm.com// this software without specific prior written permission. 1713481Sgiacomo.travaglini@arm.com// 1813481Sgiacomo.travaglini@arm.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1913481Sgiacomo.travaglini@arm.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2013481Sgiacomo.travaglini@arm.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2113481Sgiacomo.travaglini@arm.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2213481Sgiacomo.travaglini@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2313481Sgiacomo.travaglini@arm.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2413481Sgiacomo.travaglini@arm.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2513481Sgiacomo.travaglini@arm.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2613481Sgiacomo.travaglini@arm.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2713481Sgiacomo.travaglini@arm.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2813481Sgiacomo.travaglini@arm.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2913481Sgiacomo.travaglini@arm.com// 3013481Sgiacomo.travaglini@arm.com// Author: vladl@google.com (Vlad Losev) 3113481Sgiacomo.travaglini@arm.com 3213481Sgiacomo.travaglini@arm.com// This sample shows how to test code relying on some global flag variables. 3313481Sgiacomo.travaglini@arm.com// Combine() helps with generating all possible combinations of such flags, 3413481Sgiacomo.travaglini@arm.com// and each test is given one combination as a parameter. 3513481Sgiacomo.travaglini@arm.com 3613481Sgiacomo.travaglini@arm.com// Use class definitions to test from this header. 3713481Sgiacomo.travaglini@arm.com#include "prime_tables.h" 3813481Sgiacomo.travaglini@arm.com 3913481Sgiacomo.travaglini@arm.com#include "gtest/gtest.h" 4013481Sgiacomo.travaglini@arm.com 4113481Sgiacomo.travaglini@arm.com#if GTEST_HAS_COMBINE 4213481Sgiacomo.travaglini@arm.com 4313481Sgiacomo.travaglini@arm.com// Suppose we want to introduce a new, improved implementation of PrimeTable 4413481Sgiacomo.travaglini@arm.com// which combines speed of PrecalcPrimeTable and versatility of 4513481Sgiacomo.travaglini@arm.com// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both 4613481Sgiacomo.travaglini@arm.com// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more 4713481Sgiacomo.travaglini@arm.com// appropriate under the circumstances. But in low memory conditions, it can be 4813481Sgiacomo.travaglini@arm.com// told to instantiate without PrecalcPrimeTable instance at all and use only 4913481Sgiacomo.travaglini@arm.com// OnTheFlyPrimeTable. 5013481Sgiacomo.travaglini@arm.comclass HybridPrimeTable : public PrimeTable { 5113481Sgiacomo.travaglini@arm.com public: 5213481Sgiacomo.travaglini@arm.com HybridPrimeTable(bool force_on_the_fly, int max_precalculated) 5313481Sgiacomo.travaglini@arm.com : on_the_fly_impl_(new OnTheFlyPrimeTable), 5413481Sgiacomo.travaglini@arm.com precalc_impl_(force_on_the_fly ? NULL : 5513481Sgiacomo.travaglini@arm.com new PreCalculatedPrimeTable(max_precalculated)), 5613481Sgiacomo.travaglini@arm.com max_precalculated_(max_precalculated) {} 5713481Sgiacomo.travaglini@arm.com virtual ~HybridPrimeTable() { 5813481Sgiacomo.travaglini@arm.com delete on_the_fly_impl_; 5913481Sgiacomo.travaglini@arm.com delete precalc_impl_; 6013481Sgiacomo.travaglini@arm.com } 6113481Sgiacomo.travaglini@arm.com 6213481Sgiacomo.travaglini@arm.com virtual bool IsPrime(int n) const { 6313481Sgiacomo.travaglini@arm.com if (precalc_impl_ != NULL && n < max_precalculated_) 6413481Sgiacomo.travaglini@arm.com return precalc_impl_->IsPrime(n); 6513481Sgiacomo.travaglini@arm.com else 6613481Sgiacomo.travaglini@arm.com return on_the_fly_impl_->IsPrime(n); 6713481Sgiacomo.travaglini@arm.com } 6813481Sgiacomo.travaglini@arm.com 6913481Sgiacomo.travaglini@arm.com virtual int GetNextPrime(int p) const { 7013481Sgiacomo.travaglini@arm.com int next_prime = -1; 7113481Sgiacomo.travaglini@arm.com if (precalc_impl_ != NULL && p < max_precalculated_) 7213481Sgiacomo.travaglini@arm.com next_prime = precalc_impl_->GetNextPrime(p); 7313481Sgiacomo.travaglini@arm.com 7413481Sgiacomo.travaglini@arm.com return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p); 7513481Sgiacomo.travaglini@arm.com } 7613481Sgiacomo.travaglini@arm.com 7713481Sgiacomo.travaglini@arm.com private: 7813481Sgiacomo.travaglini@arm.com OnTheFlyPrimeTable* on_the_fly_impl_; 7913481Sgiacomo.travaglini@arm.com PreCalculatedPrimeTable* precalc_impl_; 8013481Sgiacomo.travaglini@arm.com int max_precalculated_; 8113481Sgiacomo.travaglini@arm.com}; 8213481Sgiacomo.travaglini@arm.com 8313481Sgiacomo.travaglini@arm.comusing ::testing::TestWithParam; 8413481Sgiacomo.travaglini@arm.comusing ::testing::Bool; 8513481Sgiacomo.travaglini@arm.comusing ::testing::Values; 8613481Sgiacomo.travaglini@arm.comusing ::testing::Combine; 8713481Sgiacomo.travaglini@arm.com 8813481Sgiacomo.travaglini@arm.com// To test all code paths for HybridPrimeTable we must test it with numbers 8913481Sgiacomo.travaglini@arm.com// both within and outside PreCalculatedPrimeTable's capacity and also with 9013481Sgiacomo.travaglini@arm.com// PreCalculatedPrimeTable disabled. We do this by defining fixture which will 9113481Sgiacomo.travaglini@arm.com// accept different combinations of parameters for instantiating a 9213481Sgiacomo.travaglini@arm.com// HybridPrimeTable instance. 9313481Sgiacomo.travaglini@arm.comclass PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > { 9413481Sgiacomo.travaglini@arm.com protected: 9513481Sgiacomo.travaglini@arm.com virtual void SetUp() { 9613481Sgiacomo.travaglini@arm.com // This can be written as 9713481Sgiacomo.travaglini@arm.com // 9813481Sgiacomo.travaglini@arm.com // bool force_on_the_fly; 9913481Sgiacomo.travaglini@arm.com // int max_precalculated; 10013481Sgiacomo.travaglini@arm.com // tie(force_on_the_fly, max_precalculated) = GetParam(); 10113481Sgiacomo.travaglini@arm.com // 10213481Sgiacomo.travaglini@arm.com // once the Google C++ Style Guide allows use of ::std::tr1::tie. 10313481Sgiacomo.travaglini@arm.com // 10413481Sgiacomo.travaglini@arm.com bool force_on_the_fly = ::testing::get<0>(GetParam()); 10513481Sgiacomo.travaglini@arm.com int max_precalculated = ::testing::get<1>(GetParam()); 10613481Sgiacomo.travaglini@arm.com table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated); 10713481Sgiacomo.travaglini@arm.com } 10813481Sgiacomo.travaglini@arm.com virtual void TearDown() { 10913481Sgiacomo.travaglini@arm.com delete table_; 11013481Sgiacomo.travaglini@arm.com table_ = NULL; 11113481Sgiacomo.travaglini@arm.com } 11213481Sgiacomo.travaglini@arm.com HybridPrimeTable* table_; 11313481Sgiacomo.travaglini@arm.com}; 11413481Sgiacomo.travaglini@arm.com 11513481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { 11613481Sgiacomo.travaglini@arm.com // Inside the test body, you can refer to the test parameter by GetParam(). 11713481Sgiacomo.travaglini@arm.com // In this case, the test parameter is a PrimeTable interface pointer which 11813481Sgiacomo.travaglini@arm.com // we can use directly. 11913481Sgiacomo.travaglini@arm.com // Please note that you can also save it in the fixture's SetUp() method 12013481Sgiacomo.travaglini@arm.com // or constructor and use saved copy in the tests. 12113481Sgiacomo.travaglini@arm.com 12213481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(-5)); 12313481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(0)); 12413481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(1)); 12513481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(4)); 12613481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(6)); 12713481Sgiacomo.travaglini@arm.com EXPECT_FALSE(table_->IsPrime(100)); 12813481Sgiacomo.travaglini@arm.com} 12913481Sgiacomo.travaglini@arm.com 13013481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, ReturnsTrueForPrimes) { 13113481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(2)); 13213481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(3)); 13313481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(5)); 13413481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(7)); 13513481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(11)); 13613481Sgiacomo.travaglini@arm.com EXPECT_TRUE(table_->IsPrime(131)); 13713481Sgiacomo.travaglini@arm.com} 13813481Sgiacomo.travaglini@arm.com 13913481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, CanGetNextPrime) { 14013481Sgiacomo.travaglini@arm.com EXPECT_EQ(2, table_->GetNextPrime(0)); 14113481Sgiacomo.travaglini@arm.com EXPECT_EQ(3, table_->GetNextPrime(2)); 14213481Sgiacomo.travaglini@arm.com EXPECT_EQ(5, table_->GetNextPrime(3)); 14313481Sgiacomo.travaglini@arm.com EXPECT_EQ(7, table_->GetNextPrime(5)); 14413481Sgiacomo.travaglini@arm.com EXPECT_EQ(11, table_->GetNextPrime(7)); 14513481Sgiacomo.travaglini@arm.com EXPECT_EQ(131, table_->GetNextPrime(128)); 14613481Sgiacomo.travaglini@arm.com} 14713481Sgiacomo.travaglini@arm.com 14813481Sgiacomo.travaglini@arm.com// In order to run value-parameterized tests, you need to instantiate them, 14913481Sgiacomo.travaglini@arm.com// or bind them to a list of values which will be used as test parameters. 15013481Sgiacomo.travaglini@arm.com// You can instantiate them in a different translation module, or even 15113481Sgiacomo.travaglini@arm.com// instantiate them several times. 15213481Sgiacomo.travaglini@arm.com// 15313481Sgiacomo.travaglini@arm.com// Here, we instantiate our tests with a list of parameters. We must combine 15413481Sgiacomo.travaglini@arm.com// all variations of the boolean flag suppressing PrecalcPrimeTable and some 15513481Sgiacomo.travaglini@arm.com// meaningful values for tests. We choose a small value (1), and a value that 15613481Sgiacomo.travaglini@arm.com// will put some of the tested numbers beyond the capability of the 15713481Sgiacomo.travaglini@arm.com// PrecalcPrimeTable instance and some inside it (10). Combine will produce all 15813481Sgiacomo.travaglini@arm.com// possible combinations. 15913481Sgiacomo.travaglini@arm.comINSTANTIATE_TEST_CASE_P(MeaningfulTestParameters, 16013481Sgiacomo.travaglini@arm.com PrimeTableTest, 16113481Sgiacomo.travaglini@arm.com Combine(Bool(), Values(1, 10))); 16213481Sgiacomo.travaglini@arm.com 16313481Sgiacomo.travaglini@arm.com#else 16413481Sgiacomo.travaglini@arm.com 16513481Sgiacomo.travaglini@arm.com// Google Test may not support Combine() with some compilers. If we 16613481Sgiacomo.travaglini@arm.com// use conditional compilation to compile out all code referring to 16713481Sgiacomo.travaglini@arm.com// the gtest_main library, MSVC linker will not link that library at 16813481Sgiacomo.travaglini@arm.com// all and consequently complain about missing entry point defined in 16913481Sgiacomo.travaglini@arm.com// that library (fatal error LNK1561: entry point must be 17013481Sgiacomo.travaglini@arm.com// defined). This dummy test keeps gtest_main linked in. 17113481Sgiacomo.travaglini@arm.comTEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {} 17213481Sgiacomo.travaglini@arm.com 17313481Sgiacomo.travaglini@arm.com#endif // GTEST_HAS_COMBINE 174