media/webrtc/trunk/testing/gtest/samples/sample5_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample5_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,199 @@
     1.4 +// Copyright 2005, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: wan@google.com (Zhanyong Wan)
    1.34 +
    1.35 +// This sample teaches how to reuse a test fixture in multiple test
    1.36 +// cases by deriving sub-fixtures from it.
    1.37 +//
    1.38 +// When you define a test fixture, you specify the name of the test
    1.39 +// case that will use this fixture.  Therefore, a test fixture can
    1.40 +// be used by only one test case.
    1.41 +//
    1.42 +// Sometimes, more than one test cases may want to use the same or
    1.43 +// slightly different test fixtures.  For example, you may want to
    1.44 +// make sure that all tests for a GUI library don't leak important
    1.45 +// system resources like fonts and brushes.  In Google Test, you do
    1.46 +// this by putting the shared logic in a super (as in "super class")
    1.47 +// test fixture, and then have each test case use a fixture derived
    1.48 +// from this super fixture.
    1.49 +
    1.50 +#include <limits.h>
    1.51 +#include <time.h>
    1.52 +#include "sample3-inl.h"
    1.53 +#include "gtest/gtest.h"
    1.54 +#include "sample1.h"
    1.55 +
    1.56 +// In this sample, we want to ensure that every test finishes within
    1.57 +// ~5 seconds.  If a test takes longer to run, we consider it a
    1.58 +// failure.
    1.59 +//
    1.60 +// We put the code for timing a test in a test fixture called
    1.61 +// "QuickTest".  QuickTest is intended to be the super fixture that
    1.62 +// other fixtures derive from, therefore there is no test case with
    1.63 +// the name "QuickTest".  This is OK.
    1.64 +//
    1.65 +// Later, we will derive multiple test fixtures from QuickTest.
    1.66 +class QuickTest : public testing::Test {
    1.67 + protected:
    1.68 +  // Remember that SetUp() is run immediately before a test starts.
    1.69 +  // This is a good place to record the start time.
    1.70 +  virtual void SetUp() {
    1.71 +    start_time_ = time(NULL);
    1.72 +  }
    1.73 +
    1.74 +  // TearDown() is invoked immediately after a test finishes.  Here we
    1.75 +  // check if the test was too slow.
    1.76 +  virtual void TearDown() {
    1.77 +    // Gets the time when the test finishes
    1.78 +    const time_t end_time = time(NULL);
    1.79 +
    1.80 +    // Asserts that the test took no more than ~5 seconds.  Did you
    1.81 +    // know that you can use assertions in SetUp() and TearDown() as
    1.82 +    // well?
    1.83 +    EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
    1.84 +  }
    1.85 +
    1.86 +  // The UTC time (in seconds) when the test starts
    1.87 +  time_t start_time_;
    1.88 +};
    1.89 +
    1.90 +
    1.91 +// We derive a fixture named IntegerFunctionTest from the QuickTest
    1.92 +// fixture.  All tests using this fixture will be automatically
    1.93 +// required to be quick.
    1.94 +class IntegerFunctionTest : public QuickTest {
    1.95 +  // We don't need any more logic than already in the QuickTest fixture.
    1.96 +  // Therefore the body is empty.
    1.97 +};
    1.98 +
    1.99 +
   1.100 +// Now we can write tests in the IntegerFunctionTest test case.
   1.101 +
   1.102 +// Tests Factorial()
   1.103 +TEST_F(IntegerFunctionTest, Factorial) {
   1.104 +  // Tests factorial of negative numbers.
   1.105 +  EXPECT_EQ(1, Factorial(-5));
   1.106 +  EXPECT_EQ(1, Factorial(-1));
   1.107 +  EXPECT_GT(Factorial(-10), 0);
   1.108 +
   1.109 +  // Tests factorial of 0.
   1.110 +  EXPECT_EQ(1, Factorial(0));
   1.111 +
   1.112 +  // Tests factorial of positive numbers.
   1.113 +  EXPECT_EQ(1, Factorial(1));
   1.114 +  EXPECT_EQ(2, Factorial(2));
   1.115 +  EXPECT_EQ(6, Factorial(3));
   1.116 +  EXPECT_EQ(40320, Factorial(8));
   1.117 +}
   1.118 +
   1.119 +
   1.120 +// Tests IsPrime()
   1.121 +TEST_F(IntegerFunctionTest, IsPrime) {
   1.122 +  // Tests negative input.
   1.123 +  EXPECT_FALSE(IsPrime(-1));
   1.124 +  EXPECT_FALSE(IsPrime(-2));
   1.125 +  EXPECT_FALSE(IsPrime(INT_MIN));
   1.126 +
   1.127 +  // Tests some trivial cases.
   1.128 +  EXPECT_FALSE(IsPrime(0));
   1.129 +  EXPECT_FALSE(IsPrime(1));
   1.130 +  EXPECT_TRUE(IsPrime(2));
   1.131 +  EXPECT_TRUE(IsPrime(3));
   1.132 +
   1.133 +  // Tests positive input.
   1.134 +  EXPECT_FALSE(IsPrime(4));
   1.135 +  EXPECT_TRUE(IsPrime(5));
   1.136 +  EXPECT_FALSE(IsPrime(6));
   1.137 +  EXPECT_TRUE(IsPrime(23));
   1.138 +}
   1.139 +
   1.140 +
   1.141 +// The next test case (named "QueueTest") also needs to be quick, so
   1.142 +// we derive another fixture from QuickTest.
   1.143 +//
   1.144 +// The QueueTest test fixture has some logic and shared objects in
   1.145 +// addition to what's in QuickTest already.  We define the additional
   1.146 +// stuff inside the body of the test fixture, as usual.
   1.147 +class QueueTest : public QuickTest {
   1.148 + protected:
   1.149 +  virtual void SetUp() {
   1.150 +    // First, we need to set up the super fixture (QuickTest).
   1.151 +    QuickTest::SetUp();
   1.152 +
   1.153 +    // Second, some additional setup for this fixture.
   1.154 +    q1_.Enqueue(1);
   1.155 +    q2_.Enqueue(2);
   1.156 +    q2_.Enqueue(3);
   1.157 +  }
   1.158 +
   1.159 +  // By default, TearDown() inherits the behavior of
   1.160 +  // QuickTest::TearDown().  As we have no additional cleaning work
   1.161 +  // for QueueTest, we omit it here.
   1.162 +  //
   1.163 +  // virtual void TearDown() {
   1.164 +  //   QuickTest::TearDown();
   1.165 +  // }
   1.166 +
   1.167 +  Queue<int> q0_;
   1.168 +  Queue<int> q1_;
   1.169 +  Queue<int> q2_;
   1.170 +};
   1.171 +
   1.172 +
   1.173 +// Now, let's write tests using the QueueTest fixture.
   1.174 +
   1.175 +// Tests the default constructor.
   1.176 +TEST_F(QueueTest, DefaultConstructor) {
   1.177 +  EXPECT_EQ(0u, q0_.Size());
   1.178 +}
   1.179 +
   1.180 +// Tests Dequeue().
   1.181 +TEST_F(QueueTest, Dequeue) {
   1.182 +  int* n = q0_.Dequeue();
   1.183 +  EXPECT_TRUE(n == NULL);
   1.184 +
   1.185 +  n = q1_.Dequeue();
   1.186 +  EXPECT_TRUE(n != NULL);
   1.187 +  EXPECT_EQ(1, *n);
   1.188 +  EXPECT_EQ(0u, q1_.Size());
   1.189 +  delete n;
   1.190 +
   1.191 +  n = q2_.Dequeue();
   1.192 +  EXPECT_TRUE(n != NULL);
   1.193 +  EXPECT_EQ(2, *n);
   1.194 +  EXPECT_EQ(1u, q2_.Size());
   1.195 +  delete n;
   1.196 +}
   1.197 +
   1.198 +// If necessary, you can derive further test fixtures from a derived
   1.199 +// fixture itself.  For example, you can derive another fixture from
   1.200 +// QueueTest.  Google Test imposes no limit on how deep the hierarchy
   1.201 +// can be.  In practice, however, you probably don't want it to be too
   1.202 +// deep as to be confusing.

mercurial