sample5_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // This sample teaches how to reuse a test fixture in multiple test
00033 // cases by deriving sub-fixtures from it.
00034 //
00035 // When you define a test fixture, you specify the name of the test
00036 // case that will use this fixture.  Therefore, a test fixture can
00037 // be used by only one test case.
00038 //
00039 // Sometimes, more than one test cases may want to use the same or
00040 // slightly different test fixtures.  For example, you may want to
00041 // make sure that all tests for a GUI library don't leak important
00042 // system resources like fonts and brushes.  In Google Test, you do
00043 // this by putting the shared logic in a super (as in "super class")
00044 // test fixture, and then have each test case use a fixture derived
00045 // from this super fixture.
00046 
00047 #include <limits.h>
00048 #include <time.h>
00049 #include "sample3-inl.h"
00050 #include "gtest/gtest.h"
00051 #include "sample1.h"
00052 
00053 // In this sample, we want to ensure that every test finishes within
00054 // ~5 seconds.  If a test takes longer to run, we consider it a
00055 // failure.
00056 //
00057 // We put the code for timing a test in a test fixture called
00058 // "QuickTest".  QuickTest is intended to be the super fixture that
00059 // other fixtures derive from, therefore there is no test case with
00060 // the name "QuickTest".  This is OK.
00061 //
00062 // Later, we will derive multiple test fixtures from QuickTest.
00063 class QuickTest : public testing::Test {
00064  protected:
00065   // Remember that SetUp() is run immediately before a test starts.
00066   // This is a good place to record the start time.
00067   virtual void SetUp() {
00068     start_time_ = time(NULL);
00069   }
00070 
00071   // TearDown() is invoked immediately after a test finishes.  Here we
00072   // check if the test was too slow.
00073   virtual void TearDown() {
00074     // Gets the time when the test finishes
00075     const time_t end_time = time(NULL);
00076 
00077     // Asserts that the test took no more than ~5 seconds.  Did you
00078     // know that you can use assertions in SetUp() and TearDown() as
00079     // well?
00080     EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
00081   }
00082 
00083   // The UTC time (in seconds) when the test starts
00084   time_t start_time_;
00085 };
00086 
00087 
00088 // We derive a fixture named IntegerFunctionTest from the QuickTest
00089 // fixture.  All tests using this fixture will be automatically
00090 // required to be quick.
00091 class IntegerFunctionTest : public QuickTest {
00092   // We don't need any more logic than already in the QuickTest fixture.
00093   // Therefore the body is empty.
00094 };
00095 
00096 
00097 // Now we can write tests in the IntegerFunctionTest test case.
00098 
00099 // Tests Factorial()
00100 TEST_F(IntegerFunctionTest, Factorial) {
00101   // Tests factorial of negative numbers.
00102   EXPECT_EQ(1, Factorial(-5));
00103   EXPECT_EQ(1, Factorial(-1));
00104   EXPECT_GT(Factorial(-10), 0);
00105 
00106   // Tests factorial of 0.
00107   EXPECT_EQ(1, Factorial(0));
00108 
00109   // Tests factorial of positive numbers.
00110   EXPECT_EQ(1, Factorial(1));
00111   EXPECT_EQ(2, Factorial(2));
00112   EXPECT_EQ(6, Factorial(3));
00113   EXPECT_EQ(40320, Factorial(8));
00114 }
00115 
00116 
00117 // Tests IsPrime()
00118 TEST_F(IntegerFunctionTest, IsPrime) {
00119   // Tests negative input.
00120   EXPECT_FALSE(IsPrime(-1));
00121   EXPECT_FALSE(IsPrime(-2));
00122   EXPECT_FALSE(IsPrime(INT_MIN));
00123 
00124   // Tests some trivial cases.
00125   EXPECT_FALSE(IsPrime(0));
00126   EXPECT_FALSE(IsPrime(1));
00127   EXPECT_TRUE(IsPrime(2));
00128   EXPECT_TRUE(IsPrime(3));
00129 
00130   // Tests positive input.
00131   EXPECT_FALSE(IsPrime(4));
00132   EXPECT_TRUE(IsPrime(5));
00133   EXPECT_FALSE(IsPrime(6));
00134   EXPECT_TRUE(IsPrime(23));
00135 }
00136 
00137 
00138 // The next test case (named "QueueTest") also needs to be quick, so
00139 // we derive another fixture from QuickTest.
00140 //
00141 // The QueueTest test fixture has some logic and shared objects in
00142 // addition to what's in QuickTest already.  We define the additional
00143 // stuff inside the body of the test fixture, as usual.
00144 class QueueTest : public QuickTest {
00145  protected:
00146   virtual void SetUp() {
00147     // First, we need to set up the super fixture (QuickTest).
00148     QuickTest::SetUp();
00149 
00150     // Second, some additional setup for this fixture.
00151     q1_.Enqueue(1);
00152     q2_.Enqueue(2);
00153     q2_.Enqueue(3);
00154   }
00155 
00156   // By default, TearDown() inherits the behavior of
00157   // QuickTest::TearDown().  As we have no additional cleaning work
00158   // for QueueTest, we omit it here.
00159   //
00160   // virtual void TearDown() {
00161   //   QuickTest::TearDown();
00162   // }
00163 
00164   Queue<int> q0_;
00165   Queue<int> q1_;
00166   Queue<int> q2_;
00167 };
00168 
00169 
00170 // Now, let's write tests using the QueueTest fixture.
00171 
00172 // Tests the default constructor.
00173 TEST_F(QueueTest, DefaultConstructor) {
00174   EXPECT_EQ(0u, q0_.Size());
00175 }
00176 
00177 // Tests Dequeue().
00178 TEST_F(QueueTest, Dequeue) {
00179   int* n = q0_.Dequeue();
00180   EXPECT_TRUE(n == NULL);
00181 
00182   n = q1_.Dequeue();
00183   EXPECT_TRUE(n != NULL);
00184   EXPECT_EQ(1, *n);
00185   EXPECT_EQ(0u, q1_.Size());
00186   delete n;
00187 
00188   n = q2_.Dequeue();
00189   EXPECT_TRUE(n != NULL);
00190   EXPECT_EQ(2, *n);
00191   EXPECT_EQ(1u, q2_.Size());
00192   delete n;
00193 }
00194 
00195 // If necessary, you can derive further test fixtures from a derived
00196 // fixture itself.  For example, you can derive another fixture from
00197 // QueueTest.  Google Test imposes no limit on how deep the hierarchy
00198 // can be.  In practice, however, you probably don't want it to be too
00199 // deep as to be confusing.


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06