1c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copyright 2005, Google Inc. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// All rights reserved. 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Redistribution and use in source and binary forms, with or without 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// modification, are permitted provided that the following conditions are 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// met: 7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// * Redistributions of source code must retain the above copyright 9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// notice, this list of conditions and the following disclaimer. 10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// * Redistributions in binary form must reproduce the above 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// copyright notice, this list of conditions and the following disclaimer 12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// in the documentation and/or other materials provided with the 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// distribution. 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// * Neither the name of Google Inc. nor the names of its 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// contributors may be used to endorse or promote products derived from 16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// this software without specific prior written permission. 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Author: wan@google.com (Zhanyong Wan) 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This sample teaches how to reuse a test fixture in multiple test 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// cases by deriving sub-fixtures from it. 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// When you define a test fixture, you specify the name of the test 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// case that will use this fixture. Therefore, a test fixture can 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be used by only one test case. 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Sometimes, more than one test cases may want to use the same or 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// slightly different test fixtures. For example, you may want to 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// make sure that all tests for a GUI library don't leak important 42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// system resources like fonts and brushes. In Google Test, you do 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// this by putting the shared logic in a super (as in "super class") 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// test fixture, and then have each test case use a fixture derived 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// from this super fixture. 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <limits.h> 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <time.h> 49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "sample3-inl.h" 50731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick#include "gtest/gtest.h" 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "sample1.h" 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// In this sample, we want to ensure that every test finishes within 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// ~5 seconds. If a test takes longer to run, we consider it a 55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// failure. 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// We put the code for timing a test in a test fixture called 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// "QuickTest". QuickTest is intended to be the super fixture that 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// other fixtures derive from, therefore there is no test case with 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the name "QuickTest". This is OK. 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Later, we will derive multiple test fixtures from QuickTest. 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass QuickTest : public testing::Test { 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected: 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Remember that SetUp() is run immediately before a test starts. 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // This is a good place to record the start time. 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void SetUp() { 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch start_time_ = time(NULL); 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // TearDown() is invoked immediately after a test finishes. Here we 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // check if the test was too slow. 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void TearDown() { 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Gets the time when the test finishes 75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const time_t end_time = time(NULL); 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Asserts that the test took no more than ~5 seconds. Did you 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // know that you can use assertions in SetUp() and TearDown() as 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // well? 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long."; 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // The UTC time (in seconds) when the test starts 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch time_t start_time_; 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// We derive a fixture named IntegerFunctionTest from the QuickTest 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// fixture. All tests using this fixture will be automatically 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// required to be quick. 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass IntegerFunctionTest : public QuickTest { 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // We don't need any more logic than already in the QuickTest fixture. 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Therefore the body is empty. 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Now we can write tests in the IntegerFunctionTest test case. 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Tests Factorial() 100c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(IntegerFunctionTest, Factorial) { 101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests factorial of negative numbers. 102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, Factorial(-5)); 103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, Factorial(-1)); 104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(Factorial(-10) > 0); 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests factorial of 0. 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, Factorial(0)); 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests factorial of positive numbers. 110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, Factorial(1)); 111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(2, Factorial(2)); 112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(6, Factorial(3)); 113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(40320, Factorial(8)); 114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Tests IsPrime() 118c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(IntegerFunctionTest, IsPrime) { 119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests negative input. 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(-1)); 121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(-2)); 122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(INT_MIN)); 123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests some trivial cases. 125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(0)); 126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(1)); 127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(IsPrime(2)); 128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(IsPrime(3)); 129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Tests positive input. 131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(4)); 132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(IsPrime(5)); 133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(!IsPrime(6)); 134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(IsPrime(23)); 135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The next test case (named "QueueTest") also needs to be quick, so 139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// we derive another fixture from QuickTest. 140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// 141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The QueueTest test fixture has some logic and shared objects in 142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// addition to what's in QuickTest already. We define the additional 143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// stuff inside the body of the test fixture, as usual. 144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass QueueTest : public QuickTest { 145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected: 146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void SetUp() { 147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // First, we need to set up the super fixture (QuickTest). 148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch QuickTest::SetUp(); 149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Second, some additional setup for this fixture. 151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch q1_.Enqueue(1); 152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch q2_.Enqueue(2); 153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch q2_.Enqueue(3); 154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // By default, TearDown() inherits the behavior of 157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // QuickTest::TearDown(). As we have no additional cleaning work 158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // for QueueTest, we omit it here. 159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // 160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // virtual void TearDown() { 161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // QuickTest::TearDown(); 162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // } 163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch Queue<int> q0_; 165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch Queue<int> q1_; 166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch Queue<int> q2_; 167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Now, let's write tests using the QueueTest fixture. 171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Tests the default constructor. 173c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(QueueTest, DefaultConstructor) { 174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(0u, q0_.Size()); 175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Tests Dequeue(). 178c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochTEST_F(QueueTest, Dequeue) { 179c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int* n = q0_.Dequeue(); 180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(n == NULL); 181c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 182c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch n = q1_.Dequeue(); 183c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(n != NULL); 184c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1, *n); 185c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(0u, q1_.Size()); 186c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch delete n; 187c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 188c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch n = q2_.Dequeue(); 189c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_TRUE(n != NULL); 190c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(2, *n); 191c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch EXPECT_EQ(1u, q2_.Size()); 192c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch delete n; 193c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 194c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 195c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// If necessary, you can derive further test fixtures from a derived 196c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// fixture itself. For example, you can derive another fixture from 197c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// QueueTest. Google Test imposes no limit on how deep the hierarchy 198c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// can be. In practice, however, you probably don't want it to be too 199c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// deep as to be confusing. 200