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