10488b0ea4509a6591f06b05ac5ae14433b652ee
[platform/upstream/gtest.git] / googletest / samples / sample8_unittest.cc
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
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"
39 namespace {
40
41 // Suppose we want to introduce a new, improved implementation of PrimeTable
42 // which combines speed of PrecalcPrimeTable and versatility of
43 // OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
44 // PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
45 // appropriate under the circumstances. But in low memory conditions, it can be
46 // told to instantiate without PrecalcPrimeTable instance at all and use only
47 // OnTheFlyPrimeTable.
48 class HybridPrimeTable : public PrimeTable {
49  public:
50   HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
51       : on_the_fly_impl_(new OnTheFlyPrimeTable),
52         precalc_impl_(force_on_the_fly
53                           ? nullptr
54                           : new PreCalculatedPrimeTable(max_precalculated)),
55         max_precalculated_(max_precalculated) {}
56   ~HybridPrimeTable() override {
57     delete on_the_fly_impl_;
58     delete precalc_impl_;
59   }
60
61   bool IsPrime(int n) const override {
62     if (precalc_impl_ != nullptr && n < max_precalculated_)
63       return precalc_impl_->IsPrime(n);
64     else
65       return on_the_fly_impl_->IsPrime(n);
66   }
67
68   int GetNextPrime(int p) const override {
69     int next_prime = -1;
70     if (precalc_impl_ != nullptr && 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
82 using ::testing::TestWithParam;
83 using ::testing::Bool;
84 using ::testing::Values;
85 using ::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.
92 class PrimeTableTest : public TestWithParam< ::std::tuple<bool, int> > {
93  protected:
94   void SetUp() override {
95     bool force_on_the_fly;
96     int max_precalculated;
97     std::tie(force_on_the_fly, max_precalculated) = GetParam();
98     table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
99   }
100   void TearDown() override {
101     delete table_;
102     table_ = nullptr;
103   }
104   HybridPrimeTable* table_;
105 };
106
107 TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
108   // Inside the test body, you can refer to the test parameter by GetParam().
109   // In this case, the test parameter is a PrimeTable interface pointer which
110   // we can use directly.
111   // Please note that you can also save it in the fixture's SetUp() method
112   // or constructor and use saved copy in the tests.
113
114   EXPECT_FALSE(table_->IsPrime(-5));
115   EXPECT_FALSE(table_->IsPrime(0));
116   EXPECT_FALSE(table_->IsPrime(1));
117   EXPECT_FALSE(table_->IsPrime(4));
118   EXPECT_FALSE(table_->IsPrime(6));
119   EXPECT_FALSE(table_->IsPrime(100));
120 }
121
122 TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
123   EXPECT_TRUE(table_->IsPrime(2));
124   EXPECT_TRUE(table_->IsPrime(3));
125   EXPECT_TRUE(table_->IsPrime(5));
126   EXPECT_TRUE(table_->IsPrime(7));
127   EXPECT_TRUE(table_->IsPrime(11));
128   EXPECT_TRUE(table_->IsPrime(131));
129 }
130
131 TEST_P(PrimeTableTest, CanGetNextPrime) {
132   EXPECT_EQ(2, table_->GetNextPrime(0));
133   EXPECT_EQ(3, table_->GetNextPrime(2));
134   EXPECT_EQ(5, table_->GetNextPrime(3));
135   EXPECT_EQ(7, table_->GetNextPrime(5));
136   EXPECT_EQ(11, table_->GetNextPrime(7));
137   EXPECT_EQ(131, table_->GetNextPrime(128));
138 }
139
140 // In order to run value-parameterized tests, you need to instantiate them,
141 // or bind them to a list of values which will be used as test parameters.
142 // You can instantiate them in a different translation module, or even
143 // instantiate them several times.
144 //
145 // Here, we instantiate our tests with a list of parameters. We must combine
146 // all variations of the boolean flag suppressing PrecalcPrimeTable and some
147 // meaningful values for tests. We choose a small value (1), and a value that
148 // will put some of the tested numbers beyond the capability of the
149 // PrecalcPrimeTable instance and some inside it (10). Combine will produce all
150 // possible combinations.
151 INSTANTIATE_TEST_SUITE_P(MeaningfulTestParameters, PrimeTableTest,
152                          Combine(Bool(), Values(1, 10)));
153
154 }  // namespace