blob: ce75cf030870c376dfec6031db247110870383f6 [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001// 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.
Austin Schuh889ac432018-10-29 22:57:02 -070029
Austin Schuh0cbef622015-09-06 17:34:52 -070030
31// This sample shows how to test code relying on some global flag variables.
32// Combine() helps with generating all possible combinations of such flags,
33// and each test is given one combination as a parameter.
34
35// Use class definitions to test from this header.
36#include "prime_tables.h"
37
38#include "gtest/gtest.h"
Austin Schuh889ac432018-10-29 22:57:02 -070039namespace {
Austin Schuh0cbef622015-09-06 17:34:52 -070040#if GTEST_HAS_COMBINE
41
42// Suppose we want to introduce a new, improved implementation of PrimeTable
43// which combines speed of PrecalcPrimeTable and versatility of
44// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
45// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
46// appropriate under the circumstances. But in low memory conditions, it can be
47// told to instantiate without PrecalcPrimeTable instance at all and use only
48// OnTheFlyPrimeTable.
49class HybridPrimeTable : public PrimeTable {
50 public:
51 HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
52 : on_the_fly_impl_(new OnTheFlyPrimeTable),
53 precalc_impl_(force_on_the_fly ? NULL :
54 new PreCalculatedPrimeTable(max_precalculated)),
55 max_precalculated_(max_precalculated) {}
56 virtual ~HybridPrimeTable() {
57 delete on_the_fly_impl_;
58 delete precalc_impl_;
59 }
60
61 virtual bool IsPrime(int n) const {
62 if (precalc_impl_ != NULL && n < max_precalculated_)
63 return precalc_impl_->IsPrime(n);
64 else
65 return on_the_fly_impl_->IsPrime(n);
66 }
67
68 virtual int GetNextPrime(int p) const {
69 int next_prime = -1;
70 if (precalc_impl_ != NULL && p < max_precalculated_)
71 next_prime = precalc_impl_->GetNextPrime(p);
72
73 return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
74 }
75
76 private:
77 OnTheFlyPrimeTable* on_the_fly_impl_;
78 PreCalculatedPrimeTable* precalc_impl_;
79 int max_precalculated_;
80};
81
82using ::testing::TestWithParam;
83using ::testing::Bool;
84using ::testing::Values;
85using ::testing::Combine;
86
87// To test all code paths for HybridPrimeTable we must test it with numbers
88// both within and outside PreCalculatedPrimeTable's capacity and also with
89// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
90// accept different combinations of parameters for instantiating a
91// HybridPrimeTable instance.
92class PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > {
93 protected:
94 virtual void SetUp() {
95 // This can be written as
96 //
97 // bool force_on_the_fly;
98 // int max_precalculated;
99 // tie(force_on_the_fly, max_precalculated) = GetParam();
100 //
101 // once the Google C++ Style Guide allows use of ::std::tr1::tie.
102 //
103 bool force_on_the_fly = ::testing::get<0>(GetParam());
104 int max_precalculated = ::testing::get<1>(GetParam());
105 table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
106 }
107 virtual void TearDown() {
108 delete table_;
109 table_ = NULL;
110 }
111 HybridPrimeTable* table_;
112};
113
114TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
115 // Inside the test body, you can refer to the test parameter by GetParam().
116 // In this case, the test parameter is a PrimeTable interface pointer which
117 // we can use directly.
118 // Please note that you can also save it in the fixture's SetUp() method
119 // or constructor and use saved copy in the tests.
120
121 EXPECT_FALSE(table_->IsPrime(-5));
122 EXPECT_FALSE(table_->IsPrime(0));
123 EXPECT_FALSE(table_->IsPrime(1));
124 EXPECT_FALSE(table_->IsPrime(4));
125 EXPECT_FALSE(table_->IsPrime(6));
126 EXPECT_FALSE(table_->IsPrime(100));
127}
128
129TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
130 EXPECT_TRUE(table_->IsPrime(2));
131 EXPECT_TRUE(table_->IsPrime(3));
132 EXPECT_TRUE(table_->IsPrime(5));
133 EXPECT_TRUE(table_->IsPrime(7));
134 EXPECT_TRUE(table_->IsPrime(11));
135 EXPECT_TRUE(table_->IsPrime(131));
136}
137
138TEST_P(PrimeTableTest, CanGetNextPrime) {
139 EXPECT_EQ(2, table_->GetNextPrime(0));
140 EXPECT_EQ(3, table_->GetNextPrime(2));
141 EXPECT_EQ(5, table_->GetNextPrime(3));
142 EXPECT_EQ(7, table_->GetNextPrime(5));
143 EXPECT_EQ(11, table_->GetNextPrime(7));
144 EXPECT_EQ(131, table_->GetNextPrime(128));
145}
146
147// In order to run value-parameterized tests, you need to instantiate them,
148// or bind them to a list of values which will be used as test parameters.
149// You can instantiate them in a different translation module, or even
150// instantiate them several times.
151//
152// Here, we instantiate our tests with a list of parameters. We must combine
153// all variations of the boolean flag suppressing PrecalcPrimeTable and some
154// meaningful values for tests. We choose a small value (1), and a value that
155// will put some of the tested numbers beyond the capability of the
156// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
157// possible combinations.
158INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
159 PrimeTableTest,
160 Combine(Bool(), Values(1, 10)));
161
162#else
163
164// Google Test may not support Combine() with some compilers. If we
165// use conditional compilation to compile out all code referring to
166// the gtest_main library, MSVC linker will not link that library at
167// all and consequently complain about missing entry point defined in
168// that library (fatal error LNK1561: entry point must be
169// defined). This dummy test keeps gtest_main linked in.
170TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
171
172#endif // GTEST_HAS_COMBINE
Austin Schuh889ac432018-10-29 22:57:02 -0700173} // namespace