media/webrtc/trunk/testing/gtest/samples/sample8_unittest.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 // Copyright 2008 Google Inc.
michael@0 2 // All Rights Reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Author: vladl@google.com (Vlad Losev)
michael@0 31
michael@0 32 // This sample shows how to test code relying on some global flag variables.
michael@0 33 // Combine() helps with generating all possible combinations of such flags,
michael@0 34 // and each test is given one combination as a parameter.
michael@0 35
michael@0 36 // Use class definitions to test from this header.
michael@0 37 #include "prime_tables.h"
michael@0 38
michael@0 39 #include "gtest/gtest.h"
michael@0 40
michael@0 41 #if GTEST_HAS_COMBINE
michael@0 42
michael@0 43 // Suppose we want to introduce a new, improved implementation of PrimeTable
michael@0 44 // which combines speed of PrecalcPrimeTable and versatility of
michael@0 45 // OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
michael@0 46 // PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
michael@0 47 // appropriate under the circumstances. But in low memory conditions, it can be
michael@0 48 // told to instantiate without PrecalcPrimeTable instance at all and use only
michael@0 49 // OnTheFlyPrimeTable.
michael@0 50 class HybridPrimeTable : public PrimeTable {
michael@0 51 public:
michael@0 52 HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
michael@0 53 : on_the_fly_impl_(new OnTheFlyPrimeTable),
michael@0 54 precalc_impl_(force_on_the_fly ? NULL :
michael@0 55 new PreCalculatedPrimeTable(max_precalculated)),
michael@0 56 max_precalculated_(max_precalculated) {}
michael@0 57 virtual ~HybridPrimeTable() {
michael@0 58 delete on_the_fly_impl_;
michael@0 59 delete precalc_impl_;
michael@0 60 }
michael@0 61
michael@0 62 virtual bool IsPrime(int n) const {
michael@0 63 if (precalc_impl_ != NULL && n < max_precalculated_)
michael@0 64 return precalc_impl_->IsPrime(n);
michael@0 65 else
michael@0 66 return on_the_fly_impl_->IsPrime(n);
michael@0 67 }
michael@0 68
michael@0 69 virtual int GetNextPrime(int p) const {
michael@0 70 int next_prime = -1;
michael@0 71 if (precalc_impl_ != NULL && p < max_precalculated_)
michael@0 72 next_prime = precalc_impl_->GetNextPrime(p);
michael@0 73
michael@0 74 return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
michael@0 75 }
michael@0 76
michael@0 77 private:
michael@0 78 OnTheFlyPrimeTable* on_the_fly_impl_;
michael@0 79 PreCalculatedPrimeTable* precalc_impl_;
michael@0 80 int max_precalculated_;
michael@0 81 };
michael@0 82
michael@0 83 using ::testing::TestWithParam;
michael@0 84 using ::testing::Bool;
michael@0 85 using ::testing::Values;
michael@0 86 using ::testing::Combine;
michael@0 87
michael@0 88 // To test all code paths for HybridPrimeTable we must test it with numbers
michael@0 89 // both within and outside PreCalculatedPrimeTable's capacity and also with
michael@0 90 // PreCalculatedPrimeTable disabled. We do this by defining fixture which will
michael@0 91 // accept different combinations of parameters for instantiating a
michael@0 92 // HybridPrimeTable instance.
michael@0 93 class PrimeTableTest : public TestWithParam< ::std::tr1::tuple<bool, int> > {
michael@0 94 protected:
michael@0 95 virtual void SetUp() {
michael@0 96 // This can be written as
michael@0 97 //
michael@0 98 // bool force_on_the_fly;
michael@0 99 // int max_precalculated;
michael@0 100 // tie(force_on_the_fly, max_precalculated) = GetParam();
michael@0 101 //
michael@0 102 // once the Google C++ Style Guide allows use of ::std::tr1::tie.
michael@0 103 //
michael@0 104 bool force_on_the_fly = ::std::tr1::get<0>(GetParam());
michael@0 105 int max_precalculated = ::std::tr1::get<1>(GetParam());
michael@0 106 table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
michael@0 107 }
michael@0 108 virtual void TearDown() {
michael@0 109 delete table_;
michael@0 110 table_ = NULL;
michael@0 111 }
michael@0 112 HybridPrimeTable* table_;
michael@0 113 };
michael@0 114
michael@0 115 TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
michael@0 116 // Inside the test body, you can refer to the test parameter by GetParam().
michael@0 117 // In this case, the test parameter is a PrimeTable interface pointer which
michael@0 118 // we can use directly.
michael@0 119 // Please note that you can also save it in the fixture's SetUp() method
michael@0 120 // or constructor and use saved copy in the tests.
michael@0 121
michael@0 122 EXPECT_FALSE(table_->IsPrime(-5));
michael@0 123 EXPECT_FALSE(table_->IsPrime(0));
michael@0 124 EXPECT_FALSE(table_->IsPrime(1));
michael@0 125 EXPECT_FALSE(table_->IsPrime(4));
michael@0 126 EXPECT_FALSE(table_->IsPrime(6));
michael@0 127 EXPECT_FALSE(table_->IsPrime(100));
michael@0 128 }
michael@0 129
michael@0 130 TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
michael@0 131 EXPECT_TRUE(table_->IsPrime(2));
michael@0 132 EXPECT_TRUE(table_->IsPrime(3));
michael@0 133 EXPECT_TRUE(table_->IsPrime(5));
michael@0 134 EXPECT_TRUE(table_->IsPrime(7));
michael@0 135 EXPECT_TRUE(table_->IsPrime(11));
michael@0 136 EXPECT_TRUE(table_->IsPrime(131));
michael@0 137 }
michael@0 138
michael@0 139 TEST_P(PrimeTableTest, CanGetNextPrime) {
michael@0 140 EXPECT_EQ(2, table_->GetNextPrime(0));
michael@0 141 EXPECT_EQ(3, table_->GetNextPrime(2));
michael@0 142 EXPECT_EQ(5, table_->GetNextPrime(3));
michael@0 143 EXPECT_EQ(7, table_->GetNextPrime(5));
michael@0 144 EXPECT_EQ(11, table_->GetNextPrime(7));
michael@0 145 EXPECT_EQ(131, table_->GetNextPrime(128));
michael@0 146 }
michael@0 147
michael@0 148 // In order to run value-parameterized tests, you need to instantiate them,
michael@0 149 // or bind them to a list of values which will be used as test parameters.
michael@0 150 // You can instantiate them in a different translation module, or even
michael@0 151 // instantiate them several times.
michael@0 152 //
michael@0 153 // Here, we instantiate our tests with a list of parameters. We must combine
michael@0 154 // all variations of the boolean flag suppressing PrecalcPrimeTable and some
michael@0 155 // meaningful values for tests. We choose a small value (1), and a value that
michael@0 156 // will put some of the tested numbers beyond the capability of the
michael@0 157 // PrecalcPrimeTable instance and some inside it (10). Combine will produce all
michael@0 158 // possible combinations.
michael@0 159 INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
michael@0 160 PrimeTableTest,
michael@0 161 Combine(Bool(), Values(1, 10)));
michael@0 162
michael@0 163 #else
michael@0 164
michael@0 165 // Google Test may not support Combine() with some compilers. If we
michael@0 166 // use conditional compilation to compile out all code referring to
michael@0 167 // the gtest_main library, MSVC linker will not link that library at
michael@0 168 // all and consequently complain about missing entry point defined in
michael@0 169 // that library (fatal error LNK1561: entry point must be
michael@0 170 // defined). This dummy test keeps gtest_main linked in.
michael@0 171 TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
michael@0 172
michael@0 173 #endif // GTEST_HAS_COMBINE

mercurial