Blame nss/external_tests/google_test/gtest/samples/sample8_unittest.cc

Packit 40b132
// Copyright 2008 Google Inc.
Packit 40b132
// All Rights Reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
//
Packit 40b132
// Author: vladl@google.com (Vlad Losev)
Packit 40b132
Packit 40b132
// This sample shows how to test code relying on some global flag variables.
Packit 40b132
// Combine() helps with generating all possible combinations of such flags,
Packit 40b132
// and each test is given one combination as a parameter.
Packit 40b132
Packit 40b132
// Use class definitions to test from this header.
Packit 40b132
#include "prime_tables.h"
Packit 40b132
Packit 40b132
#include "gtest/gtest.h"
Packit 40b132
Packit 40b132
#if GTEST_HAS_COMBINE
Packit 40b132
Packit 40b132
// Suppose we want to introduce a new, improved implementation of PrimeTable
Packit 40b132
// which combines speed of PrecalcPrimeTable and versatility of
Packit 40b132
// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
Packit 40b132
// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
Packit 40b132
// appropriate under the circumstances. But in low memory conditions, it can be
Packit 40b132
// told to instantiate without PrecalcPrimeTable instance at all and use only
Packit 40b132
// OnTheFlyPrimeTable.
Packit 40b132
class HybridPrimeTable : public PrimeTable {
Packit 40b132
 public:
Packit 40b132
  HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
Packit 40b132
      : on_the_fly_impl_(new OnTheFlyPrimeTable),
Packit 40b132
        precalc_impl_(force_on_the_fly ? NULL :
Packit 40b132
                          new PreCalculatedPrimeTable(max_precalculated)),
Packit 40b132
        max_precalculated_(max_precalculated) {}
Packit 40b132
  virtual ~HybridPrimeTable() {
Packit 40b132
    delete on_the_fly_impl_;
Packit 40b132
    delete precalc_impl_;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  virtual bool IsPrime(int n) const {
Packit 40b132
    if (precalc_impl_ != NULL && n < max_precalculated_)
Packit 40b132
      return precalc_impl_->IsPrime(n);
Packit 40b132
    else
Packit 40b132
      return on_the_fly_impl_->IsPrime(n);
Packit 40b132
  }
Packit 40b132
Packit 40b132
  virtual int GetNextPrime(int p) const {
Packit 40b132
    int next_prime = -1;
Packit 40b132
    if (precalc_impl_ != NULL && p < max_precalculated_)
Packit 40b132
      next_prime = precalc_impl_->GetNextPrime(p);
Packit 40b132
Packit 40b132
    return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
Packit 40b132
  }
Packit 40b132
Packit 40b132
 private:
Packit 40b132
  OnTheFlyPrimeTable* on_the_fly_impl_;
Packit 40b132
  PreCalculatedPrimeTable* precalc_impl_;
Packit 40b132
  int max_precalculated_;
Packit 40b132
};
Packit 40b132
Packit 40b132
using ::testing::TestWithParam;
Packit 40b132
using ::testing::Bool;
Packit 40b132
using ::testing::Values;
Packit 40b132
using ::testing::Combine;
Packit 40b132
Packit 40b132
// To test all code paths for HybridPrimeTable we must test it with numbers
Packit 40b132
// both within and outside PreCalculatedPrimeTable's capacity and also with
Packit 40b132
// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
Packit 40b132
// accept different combinations of parameters for instantiating a
Packit 40b132
// HybridPrimeTable instance.
Packit 40b132
class PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > {
Packit 40b132
 protected:
Packit 40b132
  virtual void SetUp() {
Packit 40b132
    // This can be written as
Packit 40b132
    //
Packit 40b132
    // bool force_on_the_fly;
Packit 40b132
    // int max_precalculated;
Packit 40b132
    // tie(force_on_the_fly, max_precalculated) = GetParam();
Packit 40b132
    //
Packit 40b132
    // once the Google C++ Style Guide allows use of ::std::tr1::tie.
Packit 40b132
    //
Packit 40b132
    bool force_on_the_fly = ::testing::get<0>(GetParam());
Packit 40b132
    int max_precalculated = ::testing::get<1>(GetParam());
Packit 40b132
    table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
Packit 40b132
  }
Packit 40b132
  virtual void TearDown() {
Packit 40b132
    delete table_;
Packit 40b132
    table_ = NULL;
Packit 40b132
  }
Packit 40b132
  HybridPrimeTable* table_;
Packit 40b132
};
Packit 40b132
Packit 40b132
TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
Packit 40b132
  // Inside the test body, you can refer to the test parameter by GetParam().
Packit 40b132
  // In this case, the test parameter is a PrimeTable interface pointer which
Packit 40b132
  // we can use directly.
Packit 40b132
  // Please note that you can also save it in the fixture's SetUp() method
Packit 40b132
  // or constructor and use saved copy in the tests.
Packit 40b132
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(-5));
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(0));
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(1));
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(4));
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(6));
Packit 40b132
  EXPECT_FALSE(table_->IsPrime(100));
Packit 40b132
}
Packit 40b132
Packit 40b132
TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(2));
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(3));
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(5));
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(7));
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(11));
Packit 40b132
  EXPECT_TRUE(table_->IsPrime(131));
Packit 40b132
}
Packit 40b132
Packit 40b132
TEST_P(PrimeTableTest, CanGetNextPrime) {
Packit 40b132
  EXPECT_EQ(2, table_->GetNextPrime(0));
Packit 40b132
  EXPECT_EQ(3, table_->GetNextPrime(2));
Packit 40b132
  EXPECT_EQ(5, table_->GetNextPrime(3));
Packit 40b132
  EXPECT_EQ(7, table_->GetNextPrime(5));
Packit 40b132
  EXPECT_EQ(11, table_->GetNextPrime(7));
Packit 40b132
  EXPECT_EQ(131, table_->GetNextPrime(128));
Packit 40b132
}
Packit 40b132
Packit 40b132
// In order to run value-parameterized tests, you need to instantiate them,
Packit 40b132
// or bind them to a list of values which will be used as test parameters.
Packit 40b132
// You can instantiate them in a different translation module, or even
Packit 40b132
// instantiate them several times.
Packit 40b132
//
Packit 40b132
// Here, we instantiate our tests with a list of parameters. We must combine
Packit 40b132
// all variations of the boolean flag suppressing PrecalcPrimeTable and some
Packit 40b132
// meaningful values for tests. We choose a small value (1), and a value that
Packit 40b132
// will put some of the tested numbers beyond the capability of the
Packit 40b132
// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
Packit 40b132
// possible combinations.
Packit 40b132
INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
Packit 40b132
                        PrimeTableTest,
Packit 40b132
                        Combine(Bool(), Values(1, 10)));
Packit 40b132
Packit 40b132
#else
Packit 40b132
Packit 40b132
// Google Test may not support Combine() with some compilers. If we
Packit 40b132
// use conditional compilation to compile out all code referring to
Packit 40b132
// the gtest_main library, MSVC linker will not link that library at
Packit 40b132
// all and consequently complain about missing entry point defined in
Packit 40b132
// that library (fatal error LNK1561: entry point must be
Packit 40b132
// defined). This dummy test keeps gtest_main linked in.
Packit 40b132
TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
Packit 40b132
Packit 40b132
#endif  // GTEST_HAS_COMBINE