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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample8_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +// Copyright 2008 Google Inc.
     1.5 +// All Rights Reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: vladl@google.com (Vlad Losev)
    1.34 +
    1.35 +// This sample shows how to test code relying on some global flag variables.
    1.36 +// Combine() helps with generating all possible combinations of such flags,
    1.37 +// and each test is given one combination as a parameter.
    1.38 +
    1.39 +// Use class definitions to test from this header.
    1.40 +#include "prime_tables.h"
    1.41 +
    1.42 +#include "gtest/gtest.h"
    1.43 +
    1.44 +#if GTEST_HAS_COMBINE
    1.45 +
    1.46 +// Suppose we want to introduce a new, improved implementation of PrimeTable
    1.47 +// which combines speed of PrecalcPrimeTable and versatility of
    1.48 +// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
    1.49 +// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
    1.50 +// appropriate under the circumstances. But in low memory conditions, it can be
    1.51 +// told to instantiate without PrecalcPrimeTable instance at all and use only
    1.52 +// OnTheFlyPrimeTable.
    1.53 +class HybridPrimeTable : public PrimeTable {
    1.54 + public:
    1.55 +  HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
    1.56 +      : on_the_fly_impl_(new OnTheFlyPrimeTable),
    1.57 +        precalc_impl_(force_on_the_fly ? NULL :
    1.58 +                          new PreCalculatedPrimeTable(max_precalculated)),
    1.59 +        max_precalculated_(max_precalculated) {}
    1.60 +  virtual ~HybridPrimeTable() {
    1.61 +    delete on_the_fly_impl_;
    1.62 +    delete precalc_impl_;
    1.63 +  }
    1.64 +
    1.65 +  virtual bool IsPrime(int n) const {
    1.66 +    if (precalc_impl_ != NULL && n < max_precalculated_)
    1.67 +      return precalc_impl_->IsPrime(n);
    1.68 +    else
    1.69 +      return on_the_fly_impl_->IsPrime(n);
    1.70 +  }
    1.71 +
    1.72 +  virtual int GetNextPrime(int p) const {
    1.73 +    int next_prime = -1;
    1.74 +    if (precalc_impl_ != NULL && p < max_precalculated_)
    1.75 +      next_prime = precalc_impl_->GetNextPrime(p);
    1.76 +
    1.77 +    return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
    1.78 +  }
    1.79 +
    1.80 + private:
    1.81 +  OnTheFlyPrimeTable* on_the_fly_impl_;
    1.82 +  PreCalculatedPrimeTable* precalc_impl_;
    1.83 +  int max_precalculated_;
    1.84 +};
    1.85 +
    1.86 +using ::testing::TestWithParam;
    1.87 +using ::testing::Bool;
    1.88 +using ::testing::Values;
    1.89 +using ::testing::Combine;
    1.90 +
    1.91 +// To test all code paths for HybridPrimeTable we must test it with numbers
    1.92 +// both within and outside PreCalculatedPrimeTable's capacity and also with
    1.93 +// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
    1.94 +// accept different combinations of parameters for instantiating a
    1.95 +// HybridPrimeTable instance.
    1.96 +class PrimeTableTest : public TestWithParam< ::std::tr1::tuple<bool, int> > {
    1.97 + protected:
    1.98 +  virtual void SetUp() {
    1.99 +    // This can be written as
   1.100 +    //
   1.101 +    // bool force_on_the_fly;
   1.102 +    // int max_precalculated;
   1.103 +    // tie(force_on_the_fly, max_precalculated) = GetParam();
   1.104 +    //
   1.105 +    // once the Google C++ Style Guide allows use of ::std::tr1::tie.
   1.106 +    //
   1.107 +    bool force_on_the_fly = ::std::tr1::get<0>(GetParam());
   1.108 +    int max_precalculated = ::std::tr1::get<1>(GetParam());
   1.109 +    table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
   1.110 +  }
   1.111 +  virtual void TearDown() {
   1.112 +    delete table_;
   1.113 +    table_ = NULL;
   1.114 +  }
   1.115 +  HybridPrimeTable* table_;
   1.116 +};
   1.117 +
   1.118 +TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
   1.119 +  // Inside the test body, you can refer to the test parameter by GetParam().
   1.120 +  // In this case, the test parameter is a PrimeTable interface pointer which
   1.121 +  // we can use directly.
   1.122 +  // Please note that you can also save it in the fixture's SetUp() method
   1.123 +  // or constructor and use saved copy in the tests.
   1.124 +
   1.125 +  EXPECT_FALSE(table_->IsPrime(-5));
   1.126 +  EXPECT_FALSE(table_->IsPrime(0));
   1.127 +  EXPECT_FALSE(table_->IsPrime(1));
   1.128 +  EXPECT_FALSE(table_->IsPrime(4));
   1.129 +  EXPECT_FALSE(table_->IsPrime(6));
   1.130 +  EXPECT_FALSE(table_->IsPrime(100));
   1.131 +}
   1.132 +
   1.133 +TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
   1.134 +  EXPECT_TRUE(table_->IsPrime(2));
   1.135 +  EXPECT_TRUE(table_->IsPrime(3));
   1.136 +  EXPECT_TRUE(table_->IsPrime(5));
   1.137 +  EXPECT_TRUE(table_->IsPrime(7));
   1.138 +  EXPECT_TRUE(table_->IsPrime(11));
   1.139 +  EXPECT_TRUE(table_->IsPrime(131));
   1.140 +}
   1.141 +
   1.142 +TEST_P(PrimeTableTest, CanGetNextPrime) {
   1.143 +  EXPECT_EQ(2, table_->GetNextPrime(0));
   1.144 +  EXPECT_EQ(3, table_->GetNextPrime(2));
   1.145 +  EXPECT_EQ(5, table_->GetNextPrime(3));
   1.146 +  EXPECT_EQ(7, table_->GetNextPrime(5));
   1.147 +  EXPECT_EQ(11, table_->GetNextPrime(7));
   1.148 +  EXPECT_EQ(131, table_->GetNextPrime(128));
   1.149 +}
   1.150 +
   1.151 +// In order to run value-parameterized tests, you need to instantiate them,
   1.152 +// or bind them to a list of values which will be used as test parameters.
   1.153 +// You can instantiate them in a different translation module, or even
   1.154 +// instantiate them several times.
   1.155 +//
   1.156 +// Here, we instantiate our tests with a list of parameters. We must combine
   1.157 +// all variations of the boolean flag suppressing PrecalcPrimeTable and some
   1.158 +// meaningful values for tests. We choose a small value (1), and a value that
   1.159 +// will put some of the tested numbers beyond the capability of the
   1.160 +// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
   1.161 +// possible combinations.
   1.162 +INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
   1.163 +                        PrimeTableTest,
   1.164 +                        Combine(Bool(), Values(1, 10)));
   1.165 +
   1.166 +#else
   1.167 +
   1.168 +// Google Test may not support Combine() with some compilers. If we
   1.169 +// use conditional compilation to compile out all code referring to
   1.170 +// the gtest_main library, MSVC linker will not link that library at
   1.171 +// all and consequently complain about missing entry point defined in
   1.172 +// that library (fatal error LNK1561: entry point must be
   1.173 +// defined). This dummy test keeps gtest_main linked in.
   1.174 +TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
   1.175 +
   1.176 +#endif  // GTEST_HAS_COMBINE

mercurial