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 common properties of multiple
3313481Sgiacomo.travaglini@arm.com// implementations of an interface (aka interface tests) using
3413481Sgiacomo.travaglini@arm.com// value-parameterized tests. Each test in the test case has
3513481Sgiacomo.travaglini@arm.com// a parameter that is an interface pointer to an implementation
3613481Sgiacomo.travaglini@arm.com// tested.
3713481Sgiacomo.travaglini@arm.com
3813481Sgiacomo.travaglini@arm.com// The interface and its implementations are in this header.
3913481Sgiacomo.travaglini@arm.com#include "prime_tables.h"
4013481Sgiacomo.travaglini@arm.com
4113481Sgiacomo.travaglini@arm.com#include "gtest/gtest.h"
4213481Sgiacomo.travaglini@arm.com
4313481Sgiacomo.travaglini@arm.com#if GTEST_HAS_PARAM_TEST
4413481Sgiacomo.travaglini@arm.com
4513481Sgiacomo.travaglini@arm.comusing ::testing::TestWithParam;
4613481Sgiacomo.travaglini@arm.comusing ::testing::Values;
4713481Sgiacomo.travaglini@arm.com
4813481Sgiacomo.travaglini@arm.com// As a general rule, to prevent a test from affecting the tests that come
4913481Sgiacomo.travaglini@arm.com// after it, you should create and destroy the tested objects for each test
5013481Sgiacomo.travaglini@arm.com// instead of reusing them.  In this sample we will define a simple factory
5113481Sgiacomo.travaglini@arm.com// function for PrimeTable objects.  We will instantiate objects in test's
5213481Sgiacomo.travaglini@arm.com// SetUp() method and delete them in TearDown() method.
5313481Sgiacomo.travaglini@arm.comtypedef PrimeTable* CreatePrimeTableFunc();
5413481Sgiacomo.travaglini@arm.com
5513481Sgiacomo.travaglini@arm.comPrimeTable* CreateOnTheFlyPrimeTable() {
5613481Sgiacomo.travaglini@arm.com  return new OnTheFlyPrimeTable();
5713481Sgiacomo.travaglini@arm.com}
5813481Sgiacomo.travaglini@arm.com
5913481Sgiacomo.travaglini@arm.comtemplate <size_t max_precalculated>
6013481Sgiacomo.travaglini@arm.comPrimeTable* CreatePreCalculatedPrimeTable() {
6113481Sgiacomo.travaglini@arm.com  return new PreCalculatedPrimeTable(max_precalculated);
6213481Sgiacomo.travaglini@arm.com}
6313481Sgiacomo.travaglini@arm.com
6413481Sgiacomo.travaglini@arm.com// Inside the test body, fixture constructor, SetUp(), and TearDown() you
6513481Sgiacomo.travaglini@arm.com// can refer to the test parameter by GetParam().  In this case, the test
6613481Sgiacomo.travaglini@arm.com// parameter is a factory function which we call in fixture's SetUp() to
6713481Sgiacomo.travaglini@arm.com// create and store an instance of PrimeTable.
6813481Sgiacomo.travaglini@arm.comclass PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
6913481Sgiacomo.travaglini@arm.com public:
7013481Sgiacomo.travaglini@arm.com  virtual ~PrimeTableTest() { delete table_; }
7113481Sgiacomo.travaglini@arm.com  virtual void SetUp() { table_ = (*GetParam())(); }
7213481Sgiacomo.travaglini@arm.com  virtual void TearDown() {
7313481Sgiacomo.travaglini@arm.com    delete table_;
7413481Sgiacomo.travaglini@arm.com    table_ = NULL;
7513481Sgiacomo.travaglini@arm.com  }
7613481Sgiacomo.travaglini@arm.com
7713481Sgiacomo.travaglini@arm.com protected:
7813481Sgiacomo.travaglini@arm.com  PrimeTable* table_;
7913481Sgiacomo.travaglini@arm.com};
8013481Sgiacomo.travaglini@arm.com
8113481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
8213481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(-5));
8313481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(0));
8413481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(1));
8513481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(4));
8613481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(6));
8713481Sgiacomo.travaglini@arm.com  EXPECT_FALSE(table_->IsPrime(100));
8813481Sgiacomo.travaglini@arm.com}
8913481Sgiacomo.travaglini@arm.com
9013481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
9113481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(2));
9213481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(3));
9313481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(5));
9413481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(7));
9513481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(11));
9613481Sgiacomo.travaglini@arm.com  EXPECT_TRUE(table_->IsPrime(131));
9713481Sgiacomo.travaglini@arm.com}
9813481Sgiacomo.travaglini@arm.com
9913481Sgiacomo.travaglini@arm.comTEST_P(PrimeTableTest, CanGetNextPrime) {
10013481Sgiacomo.travaglini@arm.com  EXPECT_EQ(2, table_->GetNextPrime(0));
10113481Sgiacomo.travaglini@arm.com  EXPECT_EQ(3, table_->GetNextPrime(2));
10213481Sgiacomo.travaglini@arm.com  EXPECT_EQ(5, table_->GetNextPrime(3));
10313481Sgiacomo.travaglini@arm.com  EXPECT_EQ(7, table_->GetNextPrime(5));
10413481Sgiacomo.travaglini@arm.com  EXPECT_EQ(11, table_->GetNextPrime(7));
10513481Sgiacomo.travaglini@arm.com  EXPECT_EQ(131, table_->GetNextPrime(128));
10613481Sgiacomo.travaglini@arm.com}
10713481Sgiacomo.travaglini@arm.com
10813481Sgiacomo.travaglini@arm.com// In order to run value-parameterized tests, you need to instantiate them,
10913481Sgiacomo.travaglini@arm.com// or bind them to a list of values which will be used as test parameters.
11013481Sgiacomo.travaglini@arm.com// You can instantiate them in a different translation module, or even
11113481Sgiacomo.travaglini@arm.com// instantiate them several times.
11213481Sgiacomo.travaglini@arm.com//
11313481Sgiacomo.travaglini@arm.com// Here, we instantiate our tests with a list of two PrimeTable object
11413481Sgiacomo.travaglini@arm.com// factory functions:
11513481Sgiacomo.travaglini@arm.comINSTANTIATE_TEST_CASE_P(
11613481Sgiacomo.travaglini@arm.com    OnTheFlyAndPreCalculated,
11713481Sgiacomo.travaglini@arm.com    PrimeTableTest,
11813481Sgiacomo.travaglini@arm.com    Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>));
11913481Sgiacomo.travaglini@arm.com
12013481Sgiacomo.travaglini@arm.com#else
12113481Sgiacomo.travaglini@arm.com
12213481Sgiacomo.travaglini@arm.com// Google Test may not support value-parameterized tests with some
12313481Sgiacomo.travaglini@arm.com// compilers. If we use conditional compilation to compile out all
12413481Sgiacomo.travaglini@arm.com// code referring to the gtest_main library, MSVC linker will not link
12513481Sgiacomo.travaglini@arm.com// that library at all and consequently complain about missing entry
12613481Sgiacomo.travaglini@arm.com// point defined in that library (fatal error LNK1561: entry point
12713481Sgiacomo.travaglini@arm.com// must be defined). This dummy test keeps gtest_main linked in.
12813481Sgiacomo.travaglini@arm.comTEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
12913481Sgiacomo.travaglini@arm.com
13013481Sgiacomo.travaglini@arm.com#endif  // GTEST_HAS_PARAM_TEST
131