113481Sgiacomo.travaglini@arm.com// Copyright 2005, Google Inc. 213481Sgiacomo.travaglini@arm.com// All rights reserved. 313481Sgiacomo.travaglini@arm.com// 413481Sgiacomo.travaglini@arm.com// Redistribution and use in source and binary forms, with or without 513481Sgiacomo.travaglini@arm.com// modification, are permitted provided that the following conditions are 613481Sgiacomo.travaglini@arm.com// met: 713481Sgiacomo.travaglini@arm.com// 813481Sgiacomo.travaglini@arm.com// * Redistributions of source code must retain the above copyright 913481Sgiacomo.travaglini@arm.com// notice, this list of conditions and the following disclaimer. 1013481Sgiacomo.travaglini@arm.com// * Redistributions in binary form must reproduce the above 1113481Sgiacomo.travaglini@arm.com// copyright notice, this list of conditions and the following disclaimer 1213481Sgiacomo.travaglini@arm.com// in the documentation and/or other materials provided with the 1313481Sgiacomo.travaglini@arm.com// distribution. 1413481Sgiacomo.travaglini@arm.com// * Neither the name of Google Inc. nor the names of its 1513481Sgiacomo.travaglini@arm.com// contributors may be used to endorse or promote products derived from 1613481Sgiacomo.travaglini@arm.com// this software without specific prior written permission. 1713481Sgiacomo.travaglini@arm.com// 1813481Sgiacomo.travaglini@arm.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1913481Sgiacomo.travaglini@arm.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2013481Sgiacomo.travaglini@arm.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2113481Sgiacomo.travaglini@arm.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2213481Sgiacomo.travaglini@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2313481Sgiacomo.travaglini@arm.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2413481Sgiacomo.travaglini@arm.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2513481Sgiacomo.travaglini@arm.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2613481Sgiacomo.travaglini@arm.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2713481Sgiacomo.travaglini@arm.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2813481Sgiacomo.travaglini@arm.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2913481Sgiacomo.travaglini@arm.com// 3013481Sgiacomo.travaglini@arm.com// Author: wan@google.com (Zhanyong Wan) 3113481Sgiacomo.travaglini@arm.com 3213481Sgiacomo.travaglini@arm.com// This sample teaches how to reuse a test fixture in multiple test 3313481Sgiacomo.travaglini@arm.com// cases by deriving sub-fixtures from it. 3413481Sgiacomo.travaglini@arm.com// 3513481Sgiacomo.travaglini@arm.com// When you define a test fixture, you specify the name of the test 3613481Sgiacomo.travaglini@arm.com// case that will use this fixture. Therefore, a test fixture can 3713481Sgiacomo.travaglini@arm.com// be used by only one test case. 3813481Sgiacomo.travaglini@arm.com// 3913481Sgiacomo.travaglini@arm.com// Sometimes, more than one test cases may want to use the same or 4013481Sgiacomo.travaglini@arm.com// slightly different test fixtures. For example, you may want to 4113481Sgiacomo.travaglini@arm.com// make sure that all tests for a GUI library don't leak important 4213481Sgiacomo.travaglini@arm.com// system resources like fonts and brushes. In Google Test, you do 4313481Sgiacomo.travaglini@arm.com// this by putting the shared logic in a super (as in "super class") 4413481Sgiacomo.travaglini@arm.com// test fixture, and then have each test case use a fixture derived 4513481Sgiacomo.travaglini@arm.com// from this super fixture. 4613481Sgiacomo.travaglini@arm.com 4713481Sgiacomo.travaglini@arm.com#include <limits.h> 4813481Sgiacomo.travaglini@arm.com#include <time.h> 4913481Sgiacomo.travaglini@arm.com#include "sample3-inl.h" 5013481Sgiacomo.travaglini@arm.com#include "gtest/gtest.h" 5113481Sgiacomo.travaglini@arm.com#include "sample1.h" 5213481Sgiacomo.travaglini@arm.com 5313481Sgiacomo.travaglini@arm.com// In this sample, we want to ensure that every test finishes within 5413481Sgiacomo.travaglini@arm.com// ~5 seconds. If a test takes longer to run, we consider it a 5513481Sgiacomo.travaglini@arm.com// failure. 5613481Sgiacomo.travaglini@arm.com// 5713481Sgiacomo.travaglini@arm.com// We put the code for timing a test in a test fixture called 5813481Sgiacomo.travaglini@arm.com// "QuickTest". QuickTest is intended to be the super fixture that 5913481Sgiacomo.travaglini@arm.com// other fixtures derive from, therefore there is no test case with 6013481Sgiacomo.travaglini@arm.com// the name "QuickTest". This is OK. 6113481Sgiacomo.travaglini@arm.com// 6213481Sgiacomo.travaglini@arm.com// Later, we will derive multiple test fixtures from QuickTest. 6313481Sgiacomo.travaglini@arm.comclass QuickTest : public testing::Test { 6413481Sgiacomo.travaglini@arm.com protected: 6513481Sgiacomo.travaglini@arm.com // Remember that SetUp() is run immediately before a test starts. 6613481Sgiacomo.travaglini@arm.com // This is a good place to record the start time. 6713481Sgiacomo.travaglini@arm.com virtual void SetUp() { 6813481Sgiacomo.travaglini@arm.com start_time_ = time(NULL); 6913481Sgiacomo.travaglini@arm.com } 7013481Sgiacomo.travaglini@arm.com 7113481Sgiacomo.travaglini@arm.com // TearDown() is invoked immediately after a test finishes. Here we 7213481Sgiacomo.travaglini@arm.com // check if the test was too slow. 7313481Sgiacomo.travaglini@arm.com virtual void TearDown() { 7413481Sgiacomo.travaglini@arm.com // Gets the time when the test finishes 7513481Sgiacomo.travaglini@arm.com const time_t end_time = time(NULL); 7613481Sgiacomo.travaglini@arm.com 7713481Sgiacomo.travaglini@arm.com // Asserts that the test took no more than ~5 seconds. Did you 7813481Sgiacomo.travaglini@arm.com // know that you can use assertions in SetUp() and TearDown() as 7913481Sgiacomo.travaglini@arm.com // well? 8013481Sgiacomo.travaglini@arm.com EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long."; 8113481Sgiacomo.travaglini@arm.com } 8213481Sgiacomo.travaglini@arm.com 8313481Sgiacomo.travaglini@arm.com // The UTC time (in seconds) when the test starts 8413481Sgiacomo.travaglini@arm.com time_t start_time_; 8513481Sgiacomo.travaglini@arm.com}; 8613481Sgiacomo.travaglini@arm.com 8713481Sgiacomo.travaglini@arm.com 8813481Sgiacomo.travaglini@arm.com// We derive a fixture named IntegerFunctionTest from the QuickTest 8913481Sgiacomo.travaglini@arm.com// fixture. All tests using this fixture will be automatically 9013481Sgiacomo.travaglini@arm.com// required to be quick. 9113481Sgiacomo.travaglini@arm.comclass IntegerFunctionTest : public QuickTest { 9213481Sgiacomo.travaglini@arm.com // We don't need any more logic than already in the QuickTest fixture. 9313481Sgiacomo.travaglini@arm.com // Therefore the body is empty. 9413481Sgiacomo.travaglini@arm.com}; 9513481Sgiacomo.travaglini@arm.com 9613481Sgiacomo.travaglini@arm.com 9713481Sgiacomo.travaglini@arm.com// Now we can write tests in the IntegerFunctionTest test case. 9813481Sgiacomo.travaglini@arm.com 9913481Sgiacomo.travaglini@arm.com// Tests Factorial() 10013481Sgiacomo.travaglini@arm.comTEST_F(IntegerFunctionTest, Factorial) { 10113481Sgiacomo.travaglini@arm.com // Tests factorial of negative numbers. 10213481Sgiacomo.travaglini@arm.com EXPECT_EQ(1, Factorial(-5)); 10313481Sgiacomo.travaglini@arm.com EXPECT_EQ(1, Factorial(-1)); 10413481Sgiacomo.travaglini@arm.com EXPECT_GT(Factorial(-10), 0); 10513481Sgiacomo.travaglini@arm.com 10613481Sgiacomo.travaglini@arm.com // Tests factorial of 0. 10713481Sgiacomo.travaglini@arm.com EXPECT_EQ(1, Factorial(0)); 10813481Sgiacomo.travaglini@arm.com 10913481Sgiacomo.travaglini@arm.com // Tests factorial of positive numbers. 11013481Sgiacomo.travaglini@arm.com EXPECT_EQ(1, Factorial(1)); 11113481Sgiacomo.travaglini@arm.com EXPECT_EQ(2, Factorial(2)); 11213481Sgiacomo.travaglini@arm.com EXPECT_EQ(6, Factorial(3)); 11313481Sgiacomo.travaglini@arm.com EXPECT_EQ(40320, Factorial(8)); 11413481Sgiacomo.travaglini@arm.com} 11513481Sgiacomo.travaglini@arm.com 11613481Sgiacomo.travaglini@arm.com 11713481Sgiacomo.travaglini@arm.com// Tests IsPrime() 11813481Sgiacomo.travaglini@arm.comTEST_F(IntegerFunctionTest, IsPrime) { 11913481Sgiacomo.travaglini@arm.com // Tests negative input. 12013481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(-1)); 12113481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(-2)); 12213481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(INT_MIN)); 12313481Sgiacomo.travaglini@arm.com 12413481Sgiacomo.travaglini@arm.com // Tests some trivial cases. 12513481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(0)); 12613481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(1)); 12713481Sgiacomo.travaglini@arm.com EXPECT_TRUE(IsPrime(2)); 12813481Sgiacomo.travaglini@arm.com EXPECT_TRUE(IsPrime(3)); 12913481Sgiacomo.travaglini@arm.com 13013481Sgiacomo.travaglini@arm.com // Tests positive input. 13113481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(4)); 13213481Sgiacomo.travaglini@arm.com EXPECT_TRUE(IsPrime(5)); 13313481Sgiacomo.travaglini@arm.com EXPECT_FALSE(IsPrime(6)); 13413481Sgiacomo.travaglini@arm.com EXPECT_TRUE(IsPrime(23)); 13513481Sgiacomo.travaglini@arm.com} 13613481Sgiacomo.travaglini@arm.com 13713481Sgiacomo.travaglini@arm.com 13813481Sgiacomo.travaglini@arm.com// The next test case (named "QueueTest") also needs to be quick, so 13913481Sgiacomo.travaglini@arm.com// we derive another fixture from QuickTest. 14013481Sgiacomo.travaglini@arm.com// 14113481Sgiacomo.travaglini@arm.com// The QueueTest test fixture has some logic and shared objects in 14213481Sgiacomo.travaglini@arm.com// addition to what's in QuickTest already. We define the additional 14313481Sgiacomo.travaglini@arm.com// stuff inside the body of the test fixture, as usual. 14413481Sgiacomo.travaglini@arm.comclass QueueTest : public QuickTest { 14513481Sgiacomo.travaglini@arm.com protected: 14613481Sgiacomo.travaglini@arm.com virtual void SetUp() { 14713481Sgiacomo.travaglini@arm.com // First, we need to set up the super fixture (QuickTest). 14813481Sgiacomo.travaglini@arm.com QuickTest::SetUp(); 14913481Sgiacomo.travaglini@arm.com 15013481Sgiacomo.travaglini@arm.com // Second, some additional setup for this fixture. 15113481Sgiacomo.travaglini@arm.com q1_.Enqueue(1); 15213481Sgiacomo.travaglini@arm.com q2_.Enqueue(2); 15313481Sgiacomo.travaglini@arm.com q2_.Enqueue(3); 15413481Sgiacomo.travaglini@arm.com } 15513481Sgiacomo.travaglini@arm.com 15613481Sgiacomo.travaglini@arm.com // By default, TearDown() inherits the behavior of 15713481Sgiacomo.travaglini@arm.com // QuickTest::TearDown(). As we have no additional cleaning work 15813481Sgiacomo.travaglini@arm.com // for QueueTest, we omit it here. 15913481Sgiacomo.travaglini@arm.com // 16013481Sgiacomo.travaglini@arm.com // virtual void TearDown() { 16113481Sgiacomo.travaglini@arm.com // QuickTest::TearDown(); 16213481Sgiacomo.travaglini@arm.com // } 16313481Sgiacomo.travaglini@arm.com 16413481Sgiacomo.travaglini@arm.com Queue<int> q0_; 16513481Sgiacomo.travaglini@arm.com Queue<int> q1_; 16613481Sgiacomo.travaglini@arm.com Queue<int> q2_; 16713481Sgiacomo.travaglini@arm.com}; 16813481Sgiacomo.travaglini@arm.com 16913481Sgiacomo.travaglini@arm.com 17013481Sgiacomo.travaglini@arm.com// Now, let's write tests using the QueueTest fixture. 17113481Sgiacomo.travaglini@arm.com 17213481Sgiacomo.travaglini@arm.com// Tests the default constructor. 17313481Sgiacomo.travaglini@arm.comTEST_F(QueueTest, DefaultConstructor) { 17413481Sgiacomo.travaglini@arm.com EXPECT_EQ(0u, q0_.Size()); 17513481Sgiacomo.travaglini@arm.com} 17613481Sgiacomo.travaglini@arm.com 17713481Sgiacomo.travaglini@arm.com// Tests Dequeue(). 17813481Sgiacomo.travaglini@arm.comTEST_F(QueueTest, Dequeue) { 17913481Sgiacomo.travaglini@arm.com int* n = q0_.Dequeue(); 18013481Sgiacomo.travaglini@arm.com EXPECT_TRUE(n == NULL); 18113481Sgiacomo.travaglini@arm.com 18213481Sgiacomo.travaglini@arm.com n = q1_.Dequeue(); 18313481Sgiacomo.travaglini@arm.com EXPECT_TRUE(n != NULL); 18413481Sgiacomo.travaglini@arm.com EXPECT_EQ(1, *n); 18513481Sgiacomo.travaglini@arm.com EXPECT_EQ(0u, q1_.Size()); 18613481Sgiacomo.travaglini@arm.com delete n; 18713481Sgiacomo.travaglini@arm.com 18813481Sgiacomo.travaglini@arm.com n = q2_.Dequeue(); 18913481Sgiacomo.travaglini@arm.com EXPECT_TRUE(n != NULL); 19013481Sgiacomo.travaglini@arm.com EXPECT_EQ(2, *n); 19113481Sgiacomo.travaglini@arm.com EXPECT_EQ(1u, q2_.Size()); 19213481Sgiacomo.travaglini@arm.com delete n; 19313481Sgiacomo.travaglini@arm.com} 19413481Sgiacomo.travaglini@arm.com 19513481Sgiacomo.travaglini@arm.com// If necessary, you can derive further test fixtures from a derived 19613481Sgiacomo.travaglini@arm.com// fixture itself. For example, you can derive another fixture from 19713481Sgiacomo.travaglini@arm.com// QueueTest. Google Test imposes no limit on how deep the hierarchy 19813481Sgiacomo.travaglini@arm.com// can be. In practice, however, you probably don't want it to be too 19913481Sgiacomo.travaglini@arm.com// deep as to be confusing. 200