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

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial