media/webrtc/trunk/testing/gtest/samples/sample3_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/sample3_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     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 +// A sample program demonstrating using Google C++ testing framework.
    1.34 +//
    1.35 +// Author: wan@google.com (Zhanyong Wan)
    1.36 +
    1.37 +
    1.38 +// In this example, we use a more advanced feature of Google Test called
    1.39 +// test fixture.
    1.40 +//
    1.41 +// A test fixture is a place to hold objects and functions shared by
    1.42 +// all tests in a test case.  Using a test fixture avoids duplicating
    1.43 +// the test code necessary to initialize and cleanup those common
    1.44 +// objects for each test.  It is also useful for defining sub-routines
    1.45 +// that your tests need to invoke a lot.
    1.46 +//
    1.47 +// <TechnicalDetails>
    1.48 +//
    1.49 +// The tests share the test fixture in the sense of code sharing, not
    1.50 +// data sharing.  Each test is given its own fresh copy of the
    1.51 +// fixture.  You cannot expect the data modified by one test to be
    1.52 +// passed on to another test, which is a bad idea.
    1.53 +//
    1.54 +// The reason for this design is that tests should be independent and
    1.55 +// repeatable.  In particular, a test should not fail as the result of
    1.56 +// another test's failure.  If one test depends on info produced by
    1.57 +// another test, then the two tests should really be one big test.
    1.58 +//
    1.59 +// The macros for indicating the success/failure of a test
    1.60 +// (EXPECT_TRUE, FAIL, etc) need to know what the current test is
    1.61 +// (when Google Test prints the test result, it tells you which test
    1.62 +// each failure belongs to).  Technically, these macros invoke a
    1.63 +// member function of the Test class.  Therefore, you cannot use them
    1.64 +// in a global function.  That's why you should put test sub-routines
    1.65 +// in a test fixture.
    1.66 +//
    1.67 +// </TechnicalDetails>
    1.68 +
    1.69 +#include "sample3-inl.h"
    1.70 +#include "gtest/gtest.h"
    1.71 +
    1.72 +// To use a test fixture, derive a class from testing::Test.
    1.73 +class QueueTest : public testing::Test {
    1.74 + protected:  // You should make the members protected s.t. they can be
    1.75 +             // accessed from sub-classes.
    1.76 +
    1.77 +  // virtual void SetUp() will be called before each test is run.  You
    1.78 +  // should define it if you need to initialize the varaibles.
    1.79 +  // Otherwise, this can be skipped.
    1.80 +  virtual void SetUp() {
    1.81 +    q1_.Enqueue(1);
    1.82 +    q2_.Enqueue(2);
    1.83 +    q2_.Enqueue(3);
    1.84 +  }
    1.85 +
    1.86 +  // virtual void TearDown() will be called after each test is run.
    1.87 +  // You should define it if there is cleanup work to do.  Otherwise,
    1.88 +  // you don't have to provide it.
    1.89 +  //
    1.90 +  // virtual void TearDown() {
    1.91 +  // }
    1.92 +
    1.93 +  // A helper function that some test uses.
    1.94 +  static int Double(int n) {
    1.95 +    return 2*n;
    1.96 +  }
    1.97 +
    1.98 +  // A helper function for testing Queue::Map().
    1.99 +  void MapTester(const Queue<int> * q) {
   1.100 +    // Creates a new queue, where each element is twice as big as the
   1.101 +    // corresponding one in q.
   1.102 +    const Queue<int> * const new_q = q->Map(Double);
   1.103 +
   1.104 +    // Verifies that the new queue has the same size as q.
   1.105 +    ASSERT_EQ(q->Size(), new_q->Size());
   1.106 +
   1.107 +    // Verifies the relationship between the elements of the two queues.
   1.108 +    for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
   1.109 +          n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
   1.110 +      EXPECT_EQ(2 * n1->element(), n2->element());
   1.111 +    }
   1.112 +
   1.113 +    delete new_q;
   1.114 +  }
   1.115 +
   1.116 +  // Declares the variables your tests want to use.
   1.117 +  Queue<int> q0_;
   1.118 +  Queue<int> q1_;
   1.119 +  Queue<int> q2_;
   1.120 +};
   1.121 +
   1.122 +// When you have a test fixture, you define a test using TEST_F
   1.123 +// instead of TEST.
   1.124 +
   1.125 +// Tests the default c'tor.
   1.126 +TEST_F(QueueTest, DefaultConstructor) {
   1.127 +  // You can access data in the test fixture here.
   1.128 +  EXPECT_EQ(0u, q0_.Size());
   1.129 +}
   1.130 +
   1.131 +// Tests Dequeue().
   1.132 +TEST_F(QueueTest, Dequeue) {
   1.133 +  int * n = q0_.Dequeue();
   1.134 +  EXPECT_TRUE(n == NULL);
   1.135 +
   1.136 +  n = q1_.Dequeue();
   1.137 +  ASSERT_TRUE(n != NULL);
   1.138 +  EXPECT_EQ(1, *n);
   1.139 +  EXPECT_EQ(0u, q1_.Size());
   1.140 +  delete n;
   1.141 +
   1.142 +  n = q2_.Dequeue();
   1.143 +  ASSERT_TRUE(n != NULL);
   1.144 +  EXPECT_EQ(2, *n);
   1.145 +  EXPECT_EQ(1u, q2_.Size());
   1.146 +  delete n;
   1.147 +}
   1.148 +
   1.149 +// Tests the Queue::Map() function.
   1.150 +TEST_F(QueueTest, Map) {
   1.151 +  MapTester(&q0_);
   1.152 +  MapTester(&q1_);
   1.153 +  MapTester(&q2_);
   1.154 +}

mercurial