Blame googletest/samples/sample3_unittest.cc

Packit bd1cd8
// Copyright 2005, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
// A sample program demonstrating using Google C++ testing framework.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// In this example, we use a more advanced feature of Google Test called
Packit bd1cd8
// test fixture.
Packit bd1cd8
//
Packit bd1cd8
// A test fixture is a place to hold objects and functions shared by
Packit bd1cd8
// all tests in a test case.  Using a test fixture avoids duplicating
Packit bd1cd8
// the test code necessary to initialize and cleanup those common
Packit bd1cd8
// objects for each test.  It is also useful for defining sub-routines
Packit bd1cd8
// that your tests need to invoke a lot.
Packit bd1cd8
//
Packit bd1cd8
// <TechnicalDetails>
Packit bd1cd8
//
Packit bd1cd8
// The tests share the test fixture in the sense of code sharing, not
Packit bd1cd8
// data sharing.  Each test is given its own fresh copy of the
Packit bd1cd8
// fixture.  You cannot expect the data modified by one test to be
Packit bd1cd8
// passed on to another test, which is a bad idea.
Packit bd1cd8
//
Packit bd1cd8
// The reason for this design is that tests should be independent and
Packit bd1cd8
// repeatable.  In particular, a test should not fail as the result of
Packit bd1cd8
// another test's failure.  If one test depends on info produced by
Packit bd1cd8
// another test, then the two tests should really be one big test.
Packit bd1cd8
//
Packit bd1cd8
// The macros for indicating the success/failure of a test
Packit bd1cd8
// (EXPECT_TRUE, FAIL, etc) need to know what the current test is
Packit bd1cd8
// (when Google Test prints the test result, it tells you which test
Packit bd1cd8
// each failure belongs to).  Technically, these macros invoke a
Packit bd1cd8
// member function of the Test class.  Therefore, you cannot use them
Packit bd1cd8
// in a global function.  That's why you should put test sub-routines
Packit bd1cd8
// in a test fixture.
Packit bd1cd8
//
Packit bd1cd8
// </TechnicalDetails>
Packit bd1cd8
Packit bd1cd8
#include "sample3-inl.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
// To use a test fixture, derive a class from testing::Test.
Packit bd1cd8
class QueueTest : public testing::Test {
Packit bd1cd8
 protected:  // You should make the members protected s.t. they can be
Packit bd1cd8
             // accessed from sub-classes.
Packit bd1cd8
Packit bd1cd8
  // virtual void SetUp() will be called before each test is run.  You
Packit bd1cd8
  // should define it if you need to initialize the varaibles.
Packit bd1cd8
  // Otherwise, this can be skipped.
Packit bd1cd8
  virtual void SetUp() {
Packit bd1cd8
    q1_.Enqueue(1);
Packit bd1cd8
    q2_.Enqueue(2);
Packit bd1cd8
    q2_.Enqueue(3);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // virtual void TearDown() will be called after each test is run.
Packit bd1cd8
  // You should define it if there is cleanup work to do.  Otherwise,
Packit bd1cd8
  // you don't have to provide it.
Packit bd1cd8
  //
Packit bd1cd8
  // virtual void TearDown() {
Packit bd1cd8
  // }
Packit bd1cd8
Packit bd1cd8
  // A helper function that some test uses.
Packit bd1cd8
  static int Double(int n) {
Packit bd1cd8
    return 2*n;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // A helper function for testing Queue::Map().
Packit bd1cd8
  void MapTester(const Queue<int> * q) {
Packit bd1cd8
    // Creates a new queue, where each element is twice as big as the
Packit bd1cd8
    // corresponding one in q.
Packit bd1cd8
    const Queue<int> * const new_q = q->Map(Double);
Packit bd1cd8
Packit bd1cd8
    // Verifies that the new queue has the same size as q.
Packit bd1cd8
    ASSERT_EQ(q->Size(), new_q->Size());
Packit bd1cd8
Packit bd1cd8
    // Verifies the relationship between the elements of the two queues.
Packit bd1cd8
    for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
Packit bd1cd8
          n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
Packit bd1cd8
      EXPECT_EQ(2 * n1->element(), n2->element());
Packit bd1cd8
    }
Packit bd1cd8
Packit bd1cd8
    delete new_q;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Declares the variables your tests want to use.
Packit bd1cd8
  Queue<int> q0_;
Packit bd1cd8
  Queue<int> q1_;
Packit bd1cd8
  Queue<int> q2_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// When you have a test fixture, you define a test using TEST_F
Packit bd1cd8
// instead of TEST.
Packit bd1cd8
Packit bd1cd8
// Tests the default c'tor.
Packit bd1cd8
TEST_F(QueueTest, DefaultConstructor) {
Packit bd1cd8
  // You can access data in the test fixture here.
Packit bd1cd8
  EXPECT_EQ(0u, q0_.Size());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests Dequeue().
Packit bd1cd8
TEST_F(QueueTest, Dequeue) {
Packit bd1cd8
  int * n = q0_.Dequeue();
Packit bd1cd8
  EXPECT_TRUE(n == NULL);
Packit bd1cd8
Packit bd1cd8
  n = q1_.Dequeue();
Packit bd1cd8
  ASSERT_TRUE(n != NULL);
Packit bd1cd8
  EXPECT_EQ(1, *n);
Packit bd1cd8
  EXPECT_EQ(0u, q1_.Size());
Packit bd1cd8
  delete n;
Packit bd1cd8
Packit bd1cd8
  n = q2_.Dequeue();
Packit bd1cd8
  ASSERT_TRUE(n != NULL);
Packit bd1cd8
  EXPECT_EQ(2, *n);
Packit bd1cd8
  EXPECT_EQ(1u, q2_.Size());
Packit bd1cd8
  delete n;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests the Queue::Map() function.
Packit bd1cd8
TEST_F(QueueTest, Map) {
Packit bd1cd8
  MapTester(&q0_);
Packit bd1cd8
  MapTester(&q1_);
Packit bd1cd8
  MapTester(&q2_);
Packit bd1cd8
}