1
// Copyright 2008 Google Inc.
2
// All Rights Reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// Author: vladl@google.com (Vlad Losev)
32
// This sample shows how to test common properties of multiple
33
// implementations of an interface (aka interface tests) using
34
// value-parameterized tests. Each test in the test case has
35
// a parameter that is an interface pointer to an implementation
38
// The interface and its implementations are in this header.
39
#include "prime_tables.h"
41
#include <gtest/gtest.h>
43
#if GTEST_HAS_PARAM_TEST
45
using ::testing::TestWithParam;
46
using ::testing::Values;
48
// As a general rule, tested objects should not be reused between tests.
49
// Also, their constructors and destructors of tested objects can have
50
// side effects. Thus you should create and destroy them for each test.
51
// In this sample we will define a simple factory function for PrimeTable
52
// objects. We will instantiate objects in test's SetUp() method and
53
// delete them in TearDown() method.
54
typedef PrimeTable* CreatePrimeTableFunc();
56
PrimeTable* CreateOnTheFlyPrimeTable() {
57
return new OnTheFlyPrimeTable();
60
template <size_t max_precalculated>
61
PrimeTable* CreatePreCalculatedPrimeTable() {
62
return new PreCalculatedPrimeTable(max_precalculated);
65
// Inside the test body, fixture constructor, SetUp(), and TearDown()
66
// you can refer to the test parameter by GetParam().
67
// In this case, the test parameter is a PrimeTableFactory interface pointer
68
// which we use in fixture's SetUp() to create and store an instance of
70
class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
72
virtual ~PrimeTableTest() { delete table_; }
73
virtual void SetUp() { table_ = (*GetParam())(); }
74
virtual void TearDown() {
83
TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
84
EXPECT_FALSE(table_->IsPrime(-5));
85
EXPECT_FALSE(table_->IsPrime(0));
86
EXPECT_FALSE(table_->IsPrime(1));
87
EXPECT_FALSE(table_->IsPrime(4));
88
EXPECT_FALSE(table_->IsPrime(6));
89
EXPECT_FALSE(table_->IsPrime(100));
92
TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
93
EXPECT_TRUE(table_->IsPrime(2));
94
EXPECT_TRUE(table_->IsPrime(3));
95
EXPECT_TRUE(table_->IsPrime(5));
96
EXPECT_TRUE(table_->IsPrime(7));
97
EXPECT_TRUE(table_->IsPrime(11));
98
EXPECT_TRUE(table_->IsPrime(131));
101
TEST_P(PrimeTableTest, CanGetNextPrime) {
102
EXPECT_EQ(2, table_->GetNextPrime(0));
103
EXPECT_EQ(3, table_->GetNextPrime(2));
104
EXPECT_EQ(5, table_->GetNextPrime(3));
105
EXPECT_EQ(7, table_->GetNextPrime(5));
106
EXPECT_EQ(11, table_->GetNextPrime(7));
107
EXPECT_EQ(131, table_->GetNextPrime(128));
110
// In order to run value-parameterized tests, you need to instantiate them,
111
// or bind them to a list of values which will be used as test parameters.
112
// You can instantiate them in a different translation module, or even
113
// instantiate them several times.
115
// Here, we instantiate our tests with a list of two PrimeTable object
116
// factory functions:
117
INSTANTIATE_TEST_CASE_P(
118
OnTheFlyAndPreCalculated,
120
Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>));
124
// Google Test doesn't support value-parameterized tests on some platforms
125
// and compilers, such as MSVC 7.1. If we use conditional compilation to
126
// compile out all code referring to the gtest_main library, MSVC linker
127
// will not link that library at all and consequently complain about
128
// missing entry point defined in that library (fatal error LNK1561:
129
// entry point must be defined). This dummy test keeps gtest_main linked in.
130
TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
132
#endif // GTEST_HAS_PARAM_TEST