Blame googletest/samples/sample5_unittest.cc

Packit bd1cd8
// Copyright 2005, 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: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// This sample teaches how to reuse a test fixture in multiple test
Packit bd1cd8
// cases by deriving sub-fixtures from it.
Packit bd1cd8
//
Packit bd1cd8
// When you define a test fixture, you specify the name of the test
Packit bd1cd8
// case that will use this fixture.  Therefore, a test fixture can
Packit bd1cd8
// be used by only one test case.
Packit bd1cd8
//
Packit bd1cd8
// Sometimes, more than one test cases may want to use the same or
Packit bd1cd8
// slightly different test fixtures.  For example, you may want to
Packit bd1cd8
// make sure that all tests for a GUI library don't leak important
Packit bd1cd8
// system resources like fonts and brushes.  In Google Test, you do
Packit bd1cd8
// this by putting the shared logic in a super (as in "super class")
Packit bd1cd8
// test fixture, and then have each test case use a fixture derived
Packit bd1cd8
// from this super fixture.
Packit bd1cd8
Packit bd1cd8
#include <limits.h>
Packit bd1cd8
#include <time.h>
Packit bd1cd8
#include "sample3-inl.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
#include "sample1.h"
Packit bd1cd8
Packit bd1cd8
// In this sample, we want to ensure that every test finishes within
Packit bd1cd8
// ~5 seconds.  If a test takes longer to run, we consider it a
Packit bd1cd8
// failure.
Packit bd1cd8
//
Packit bd1cd8
// We put the code for timing a test in a test fixture called
Packit bd1cd8
// "QuickTest".  QuickTest is intended to be the super fixture that
Packit bd1cd8
// other fixtures derive from, therefore there is no test case with
Packit bd1cd8
// the name "QuickTest".  This is OK.
Packit bd1cd8
//
Packit bd1cd8
// Later, we will derive multiple test fixtures from QuickTest.
Packit bd1cd8
class QuickTest : public testing::Test {
Packit bd1cd8
 protected:
Packit bd1cd8
  // Remember that SetUp() is run immediately before a test starts.
Packit bd1cd8
  // This is a good place to record the start time.
Packit bd1cd8
  virtual void SetUp() {
Packit bd1cd8
    start_time_ = time(NULL);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // TearDown() is invoked immediately after a test finishes.  Here we
Packit bd1cd8
  // check if the test was too slow.
Packit bd1cd8
  virtual void TearDown() {
Packit bd1cd8
    // Gets the time when the test finishes
Packit bd1cd8
    const time_t end_time = time(NULL);
Packit bd1cd8
Packit bd1cd8
    // Asserts that the test took no more than ~5 seconds.  Did you
Packit bd1cd8
    // know that you can use assertions in SetUp() and TearDown() as
Packit bd1cd8
    // well?
Packit bd1cd8
    EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // The UTC time (in seconds) when the test starts
Packit bd1cd8
  time_t start_time_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// We derive a fixture named IntegerFunctionTest from the QuickTest
Packit bd1cd8
// fixture.  All tests using this fixture will be automatically
Packit bd1cd8
// required to be quick.
Packit bd1cd8
class IntegerFunctionTest : public QuickTest {
Packit bd1cd8
  // We don't need any more logic than already in the QuickTest fixture.
Packit bd1cd8
  // Therefore the body is empty.
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Now we can write tests in the IntegerFunctionTest test case.
Packit bd1cd8
Packit bd1cd8
// Tests Factorial()
Packit bd1cd8
TEST_F(IntegerFunctionTest, Factorial) {
Packit bd1cd8
  // Tests factorial of negative numbers.
Packit bd1cd8
  EXPECT_EQ(1, Factorial(-5));
Packit bd1cd8
  EXPECT_EQ(1, Factorial(-1));
Packit bd1cd8
  EXPECT_GT(Factorial(-10), 0);
Packit bd1cd8
Packit bd1cd8
  // Tests factorial of 0.
Packit bd1cd8
  EXPECT_EQ(1, Factorial(0));
Packit bd1cd8
Packit bd1cd8
  // Tests factorial of positive numbers.
Packit bd1cd8
  EXPECT_EQ(1, Factorial(1));
Packit bd1cd8
  EXPECT_EQ(2, Factorial(2));
Packit bd1cd8
  EXPECT_EQ(6, Factorial(3));
Packit bd1cd8
  EXPECT_EQ(40320, Factorial(8));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Tests IsPrime()
Packit bd1cd8
TEST_F(IntegerFunctionTest, IsPrime) {
Packit bd1cd8
  // Tests negative input.
Packit bd1cd8
  EXPECT_FALSE(IsPrime(-1));
Packit bd1cd8
  EXPECT_FALSE(IsPrime(-2));
Packit bd1cd8
  EXPECT_FALSE(IsPrime(INT_MIN));
Packit bd1cd8
Packit bd1cd8
  // Tests some trivial cases.
Packit bd1cd8
  EXPECT_FALSE(IsPrime(0));
Packit bd1cd8
  EXPECT_FALSE(IsPrime(1));
Packit bd1cd8
  EXPECT_TRUE(IsPrime(2));
Packit bd1cd8
  EXPECT_TRUE(IsPrime(3));
Packit bd1cd8
Packit bd1cd8
  // Tests positive input.
Packit bd1cd8
  EXPECT_FALSE(IsPrime(4));
Packit bd1cd8
  EXPECT_TRUE(IsPrime(5));
Packit bd1cd8
  EXPECT_FALSE(IsPrime(6));
Packit bd1cd8
  EXPECT_TRUE(IsPrime(23));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// The next test case (named "QueueTest") also needs to be quick, so
Packit bd1cd8
// we derive another fixture from QuickTest.
Packit bd1cd8
//
Packit bd1cd8
// The QueueTest test fixture has some logic and shared objects in
Packit bd1cd8
// addition to what's in QuickTest already.  We define the additional
Packit bd1cd8
// stuff inside the body of the test fixture, as usual.
Packit bd1cd8
class QueueTest : public QuickTest {
Packit bd1cd8
 protected:
Packit bd1cd8
  virtual void SetUp() {
Packit bd1cd8
    // First, we need to set up the super fixture (QuickTest).
Packit bd1cd8
    QuickTest::SetUp();
Packit bd1cd8
Packit bd1cd8
    // Second, some additional setup for this fixture.
Packit bd1cd8
    q1_.Enqueue(1);
Packit bd1cd8
    q2_.Enqueue(2);
Packit bd1cd8
    q2_.Enqueue(3);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // By default, TearDown() inherits the behavior of
Packit bd1cd8
  // QuickTest::TearDown().  As we have no additional cleaning work
Packit bd1cd8
  // for QueueTest, we omit it here.
Packit bd1cd8
  //
Packit bd1cd8
  // virtual void TearDown() {
Packit bd1cd8
  //   QuickTest::TearDown();
Packit bd1cd8
  // }
Packit bd1cd8
Packit bd1cd8
  Queue<int> q0_;
Packit bd1cd8
  Queue<int> q1_;
Packit bd1cd8
  Queue<int> q2_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// Now, let's write tests using the QueueTest fixture.
Packit bd1cd8
Packit bd1cd8
// Tests the default constructor.
Packit bd1cd8
TEST_F(QueueTest, DefaultConstructor) {
Packit bd1cd8
  EXPECT_EQ(0u, q0_.Size());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests Dequeue().
Packit bd1cd8
TEST_F(QueueTest, Dequeue) {
Packit bd1cd8
  int* n = q0_.Dequeue();
Packit bd1cd8
  EXPECT_TRUE(n == NULL);
Packit bd1cd8
Packit bd1cd8
  n = q1_.Dequeue();
Packit bd1cd8
  EXPECT_TRUE(n != NULL);
Packit bd1cd8
  EXPECT_EQ(1, *n);
Packit bd1cd8
  EXPECT_EQ(0u, q1_.Size());
Packit bd1cd8
  delete n;
Packit bd1cd8
Packit bd1cd8
  n = q2_.Dequeue();
Packit bd1cd8
  EXPECT_TRUE(n != NULL);
Packit bd1cd8
  EXPECT_EQ(2, *n);
Packit bd1cd8
  EXPECT_EQ(1u, q2_.Size());
Packit bd1cd8
  delete n;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// If necessary, you can derive further test fixtures from a derived
Packit bd1cd8
// fixture itself.  For example, you can derive another fixture from
Packit bd1cd8
// QueueTest.  Google Test imposes no limit on how deep the hierarchy
Packit bd1cd8
// can be.  In practice, however, you probably don't want it to be too
Packit bd1cd8
// deep as to be confusing.