Blame googletest/samples/sample8_unittest.cc

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