Blame nss/external_tests/google_test/gtest/samples/sample6_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: wan@google.com (Zhanyong Wan)
Packit 40b132
Packit 40b132
// This sample shows how to test common properties of multiple
Packit 40b132
// implementations of the same interface (aka interface tests).
Packit 40b132
Packit 40b132
// The interface and its implementations are in this header.
Packit 40b132
#include "prime_tables.h"
Packit 40b132
Packit 40b132
#include "gtest/gtest.h"
Packit 40b132
Packit 40b132
// First, we define some factory functions for creating instances of
Packit 40b132
// the implementations.  You may be able to skip this step if all your
Packit 40b132
// implementations can be constructed the same way.
Packit 40b132
Packit 40b132
template <class T>
Packit 40b132
PrimeTable* CreatePrimeTable();
Packit 40b132
Packit 40b132
template <>
Packit 40b132
PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
Packit 40b132
  return new OnTheFlyPrimeTable;
Packit 40b132
}
Packit 40b132
Packit 40b132
template <>
Packit 40b132
PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
Packit 40b132
  return new PreCalculatedPrimeTable(10000);
Packit 40b132
}
Packit 40b132
Packit 40b132
// Then we define a test fixture class template.
Packit 40b132
template <class T>
Packit 40b132
class PrimeTableTest : public testing::Test {
Packit 40b132
 protected:
Packit 40b132
  // The ctor calls the factory function to create a prime table
Packit 40b132
  // implemented by T.
Packit 40b132
  PrimeTableTest() : table_(CreatePrimeTable<T>()) {}
Packit 40b132
Packit 40b132
  virtual ~PrimeTableTest() { delete table_; }
Packit 40b132
Packit 40b132
  // Note that we test an implementation via the base interface
Packit 40b132
  // instead of the actual implementation class.  This is important
Packit 40b132
  // for keeping the tests close to the real world scenario, where the
Packit 40b132
  // implementation is invoked via the base interface.  It avoids
Packit 40b132
  // got-yas where the implementation class has a method that shadows
Packit 40b132
  // a method with the same name (but slightly different argument
Packit 40b132
  // types) in the base interface, for example.
Packit 40b132
  PrimeTable* const table_;
Packit 40b132
};
Packit 40b132
Packit 40b132
#if GTEST_HAS_TYPED_TEST
Packit 40b132
Packit 40b132
using testing::Types;
Packit 40b132
Packit 40b132
// Google Test offers two ways for reusing tests for different types.
Packit 40b132
// The first is called "typed tests".  You should use it if you
Packit 40b132
// already know *all* the types you are gonna exercise when you write
Packit 40b132
// the tests.
Packit 40b132
Packit 40b132
// To write a typed test case, first use
Packit 40b132
//
Packit 40b132
//   TYPED_TEST_CASE(TestCaseName, TypeList);
Packit 40b132
//
Packit 40b132
// to declare it and specify the type parameters.  As with TEST_F,
Packit 40b132
// TestCaseName must match the test fixture name.
Packit 40b132
Packit 40b132
// The list of types we want to test.
Packit 40b132
typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
Packit 40b132
Packit 40b132
TYPED_TEST_CASE(PrimeTableTest, Implementations);
Packit 40b132
Packit 40b132
// Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
Packit 40b132
// similar to TEST_F.
Packit 40b132
TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
Packit 40b132
  // Inside the test body, you can refer to the type parameter by
Packit 40b132
  // TypeParam, and refer to the fixture class by TestFixture.  We
Packit 40b132
  // don't need them in this example.
Packit 40b132
Packit 40b132
  // Since we are in the template world, C++ requires explicitly
Packit 40b132
  // writing 'this->' when referring to members of the fixture class.
Packit 40b132
  // This is something you have to learn to live with.
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(-5));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(0));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(1));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(4));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(6));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(100));
Packit 40b132
}
Packit 40b132
Packit 40b132
TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(2));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(3));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(5));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(7));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(11));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(131));
Packit 40b132
}
Packit 40b132
Packit 40b132
TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
Packit 40b132
  EXPECT_EQ(2, this->table_->GetNextPrime(0));
Packit 40b132
  EXPECT_EQ(3, this->table_->GetNextPrime(2));
Packit 40b132
  EXPECT_EQ(5, this->table_->GetNextPrime(3));
Packit 40b132
  EXPECT_EQ(7, this->table_->GetNextPrime(5));
Packit 40b132
  EXPECT_EQ(11, this->table_->GetNextPrime(7));
Packit 40b132
  EXPECT_EQ(131, this->table_->GetNextPrime(128));
Packit 40b132
}
Packit 40b132
Packit 40b132
// That's it!  Google Test will repeat each TYPED_TEST for each type
Packit 40b132
// in the type list specified in TYPED_TEST_CASE.  Sit back and be
Packit 40b132
// happy that you don't have to define them multiple times.
Packit 40b132
Packit 40b132
#endif  // GTEST_HAS_TYPED_TEST
Packit 40b132
Packit 40b132
#if GTEST_HAS_TYPED_TEST_P
Packit 40b132
Packit 40b132
using testing::Types;
Packit 40b132
Packit 40b132
// Sometimes, however, you don't yet know all the types that you want
Packit 40b132
// to test when you write the tests.  For example, if you are the
Packit 40b132
// author of an interface and expect other people to implement it, you
Packit 40b132
// might want to write a set of tests to make sure each implementation
Packit 40b132
// conforms to some basic requirements, but you don't know what
Packit 40b132
// implementations will be written in the future.
Packit 40b132
//
Packit 40b132
// How can you write the tests without committing to the type
Packit 40b132
// parameters?  That's what "type-parameterized tests" can do for you.
Packit 40b132
// It is a bit more involved than typed tests, but in return you get a
Packit 40b132
// test pattern that can be reused in many contexts, which is a big
Packit 40b132
// win.  Here's how you do it:
Packit 40b132
Packit 40b132
// First, define a test fixture class template.  Here we just reuse
Packit 40b132
// the PrimeTableTest fixture defined earlier:
Packit 40b132
Packit 40b132
template <class T>
Packit 40b132
class PrimeTableTest2 : public PrimeTableTest<T> {
Packit 40b132
};
Packit 40b132
Packit 40b132
// Then, declare the test case.  The argument is the name of the test
Packit 40b132
// fixture, and also the name of the test case (as usual).  The _P
Packit 40b132
// suffix is for "parameterized" or "pattern".
Packit 40b132
TYPED_TEST_CASE_P(PrimeTableTest2);
Packit 40b132
Packit 40b132
// Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
Packit 40b132
// similar to what you do with TEST_F.
Packit 40b132
TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(-5));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(0));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(1));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(4));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(6));
Packit 40b132
  EXPECT_FALSE(this->table_->IsPrime(100));
Packit 40b132
}
Packit 40b132
Packit 40b132
TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(2));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(3));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(5));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(7));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(11));
Packit 40b132
  EXPECT_TRUE(this->table_->IsPrime(131));
Packit 40b132
}
Packit 40b132
Packit 40b132
TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
Packit 40b132
  EXPECT_EQ(2, this->table_->GetNextPrime(0));
Packit 40b132
  EXPECT_EQ(3, this->table_->GetNextPrime(2));
Packit 40b132
  EXPECT_EQ(5, this->table_->GetNextPrime(3));
Packit 40b132
  EXPECT_EQ(7, this->table_->GetNextPrime(5));
Packit 40b132
  EXPECT_EQ(11, this->table_->GetNextPrime(7));
Packit 40b132
  EXPECT_EQ(131, this->table_->GetNextPrime(128));
Packit 40b132
}
Packit 40b132
Packit 40b132
// Type-parameterized tests involve one extra step: you have to
Packit 40b132
// enumerate the tests you defined:
Packit 40b132
REGISTER_TYPED_TEST_CASE_P(
Packit 40b132
    PrimeTableTest2,  // The first argument is the test case name.
Packit 40b132
    // The rest of the arguments are the test names.
Packit 40b132
    ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
Packit 40b132
Packit 40b132
// At this point the test pattern is done.  However, you don't have
Packit 40b132
// any real test yet as you haven't said which types you want to run
Packit 40b132
// the tests with.
Packit 40b132
Packit 40b132
// To turn the abstract test pattern into real tests, you instantiate
Packit 40b132
// it with a list of types.  Usually the test pattern will be defined
Packit 40b132
// in a .h file, and anyone can #include and instantiate it.  You can
Packit 40b132
// even instantiate it more than once in the same program.  To tell
Packit 40b132
// different instances apart, you give each of them a name, which will
Packit 40b132
// become part of the test case name and can be used in test filters.
Packit 40b132
Packit 40b132
// The list of types we want to test.  Note that it doesn't have to be
Packit 40b132
// defined at the time we write the TYPED_TEST_P()s.
Packit 40b132
typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
Packit 40b132
    PrimeTableImplementations;
Packit 40b132
INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated,    // Instance name
Packit 40b132
                              PrimeTableTest2,             // Test case name
Packit 40b132
                              PrimeTableImplementations);  // Type list
Packit 40b132
Packit 40b132
#endif  // GTEST_HAS_TYPED_TEST_P