• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Author: vladl@google.com (Vlad Losev)
31 
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
36 // tested.
37 
38 // The interface and its implementations are in this header.
39 #include "prime_tables.h"
40 
41 #include "gtest/gtest.h"
42 
43 #if GTEST_HAS_PARAM_TEST
44 
45 using ::testing::TestWithParam;
46 using ::testing::Values;
47 
48 // As a general rule, to prevent a test from affecting the tests that come
49 // after it, you should create and destroy the tested objects for each test
50 // instead of reusing them.  In this sample we will define a simple factory
51 // function for PrimeTable objects.  We will instantiate objects in test's
52 // SetUp() method and delete them in TearDown() method.
53 typedef PrimeTable* CreatePrimeTableFunc();
54 
CreateOnTheFlyPrimeTable()55 PrimeTable* CreateOnTheFlyPrimeTable() {
56   return new OnTheFlyPrimeTable();
57 }
58 
59 template <size_t max_precalculated>
CreatePreCalculatedPrimeTable()60 PrimeTable* CreatePreCalculatedPrimeTable() {
61   return new PreCalculatedPrimeTable(max_precalculated);
62 }
63 
64 // Inside the test body, fixture constructor, SetUp(), and TearDown() you
65 // can refer to the test parameter by GetParam().  In this case, the test
66 // parameter is a factory function which we call in fixture's SetUp() to
67 // create and store an instance of PrimeTable.
68 class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
69  public:
~PrimeTableTest()70   virtual ~PrimeTableTest() { delete table_; }
SetUp()71   virtual void SetUp() { table_ = (*GetParam())(); }
TearDown()72   virtual void TearDown() {
73     delete table_;
74     table_ = NULL;
75   }
76 
77  protected:
78   PrimeTable* table_;
79 };
80 
TEST_P(PrimeTableTest,ReturnsFalseForNonPrimes)81 TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
82   EXPECT_FALSE(table_->IsPrime(-5));
83   EXPECT_FALSE(table_->IsPrime(0));
84   EXPECT_FALSE(table_->IsPrime(1));
85   EXPECT_FALSE(table_->IsPrime(4));
86   EXPECT_FALSE(table_->IsPrime(6));
87   EXPECT_FALSE(table_->IsPrime(100));
88 }
89 
TEST_P(PrimeTableTest,ReturnsTrueForPrimes)90 TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
91   EXPECT_TRUE(table_->IsPrime(2));
92   EXPECT_TRUE(table_->IsPrime(3));
93   EXPECT_TRUE(table_->IsPrime(5));
94   EXPECT_TRUE(table_->IsPrime(7));
95   EXPECT_TRUE(table_->IsPrime(11));
96   EXPECT_TRUE(table_->IsPrime(131));
97 }
98 
TEST_P(PrimeTableTest,CanGetNextPrime)99 TEST_P(PrimeTableTest, CanGetNextPrime) {
100   EXPECT_EQ(2, table_->GetNextPrime(0));
101   EXPECT_EQ(3, table_->GetNextPrime(2));
102   EXPECT_EQ(5, table_->GetNextPrime(3));
103   EXPECT_EQ(7, table_->GetNextPrime(5));
104   EXPECT_EQ(11, table_->GetNextPrime(7));
105   EXPECT_EQ(131, table_->GetNextPrime(128));
106 }
107 
108 // In order to run value-parameterized tests, you need to instantiate them,
109 // or bind them to a list of values which will be used as test parameters.
110 // You can instantiate them in a different translation module, or even
111 // instantiate them several times.
112 //
113 // Here, we instantiate our tests with a list of two PrimeTable object
114 // factory functions:
115 INSTANTIATE_TEST_CASE_P(
116     OnTheFlyAndPreCalculated,
117     PrimeTableTest,
118     Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>));
119 
120 #else
121 
122 // Google Test may not support value-parameterized tests with some
123 // compilers. If we use conditional compilation to compile out all
124 // code referring to the gtest_main library, MSVC linker will not link
125 // that library at all and consequently complain about missing entry
126 // point defined in that library (fatal error LNK1561: entry point
127 // must be defined). This dummy test keeps gtest_main linked in.
TEST(DummyTest,ValueParameterizedTestsAreNotSupportedOnThisPlatform)128 TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
129 
130 #endif  // GTEST_HAS_PARAM_TEST
131