00001 // Copyright 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // A sample program demonstrating using Google C++ testing framework. 00031 // 00032 // Author: wan@google.com (Zhanyong Wan) 00033 00034 00035 // In this example, we use a more advanced feature of Google Test called 00036 // test fixture. 00037 // 00038 // A test fixture is a place to hold objects and functions shared by 00039 // all tests in a test case. Using a test fixture avoids duplicating 00040 // the test code necessary to initialize and cleanup those common 00041 // objects for each test. It is also useful for defining sub-routines 00042 // that your tests need to invoke a lot. 00043 // 00044 // <TechnicalDetails> 00045 // 00046 // The tests share the test fixture in the sense of code sharing, not 00047 // data sharing. Each test is given its own fresh copy of the 00048 // fixture. You cannot expect the data modified by one test to be 00049 // passed on to another test, which is a bad idea. 00050 // 00051 // The reason for this design is that tests should be independent and 00052 // repeatable. In particular, a test should not fail as the result of 00053 // another test's failure. If one test depends on info produced by 00054 // another test, then the two tests should really be one big test. 00055 // 00056 // The macros for indicating the success/failure of a test 00057 // (EXPECT_TRUE, FAIL, etc) need to know what the current test is 00058 // (when Google Test prints the test result, it tells you which test 00059 // each failure belongs to). Technically, these macros invoke a 00060 // member function of the Test class. Therefore, you cannot use them 00061 // in a global function. That's why you should put test sub-routines 00062 // in a test fixture. 00063 // 00064 // </TechnicalDetails> 00065 00066 #include "sample3-inl.h" 00067 #include "gtest/gtest.h" 00068 00069 // To use a test fixture, derive a class from testing::Test. 00070 class QueueTest : public testing::Test { 00071 protected: // You should make the members protected s.t. they can be 00072 // accessed from sub-classes. 00073 00074 // virtual void SetUp() will be called before each test is run. You 00075 // should define it if you need to initialize the varaibles. 00076 // Otherwise, this can be skipped. 00077 virtual void SetUp() { 00078 q1_.Enqueue(1); 00079 q2_.Enqueue(2); 00080 q2_.Enqueue(3); 00081 } 00082 00083 // virtual void TearDown() will be called after each test is run. 00084 // You should define it if there is cleanup work to do. Otherwise, 00085 // you don't have to provide it. 00086 // 00087 // virtual void TearDown() { 00088 // } 00089 00090 // A helper function that some test uses. 00091 static int Double(int n) { 00092 return 2*n; 00093 } 00094 00095 // A helper function for testing Queue::Map(). 00096 void MapTester(const Queue<int> * q) { 00097 // Creates a new queue, where each element is twice as big as the 00098 // corresponding one in q. 00099 const Queue<int> * const new_q = q->Map(Double); 00100 00101 // Verifies that the new queue has the same size as q. 00102 ASSERT_EQ(q->Size(), new_q->Size()); 00103 00104 // Verifies the relationship between the elements of the two queues. 00105 for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head(); 00106 n1 != NULL; n1 = n1->next(), n2 = n2->next() ) { 00107 EXPECT_EQ(2 * n1->element(), n2->element()); 00108 } 00109 00110 delete new_q; 00111 } 00112 00113 // Declares the variables your tests want to use. 00114 Queue<int> q0_; 00115 Queue<int> q1_; 00116 Queue<int> q2_; 00117 }; 00118 00119 // When you have a test fixture, you define a test using TEST_F 00120 // instead of TEST. 00121 00122 // Tests the default c'tor. 00123 TEST_F(QueueTest, DefaultConstructor) { 00124 // You can access data in the test fixture here. 00125 EXPECT_EQ(0u, q0_.Size()); 00126 } 00127 00128 // Tests Dequeue(). 00129 TEST_F(QueueTest, Dequeue) { 00130 int * n = q0_.Dequeue(); 00131 EXPECT_TRUE(n == NULL); 00132 00133 n = q1_.Dequeue(); 00134 ASSERT_TRUE(n != NULL); 00135 EXPECT_EQ(1, *n); 00136 EXPECT_EQ(0u, q1_.Size()); 00137 delete n; 00138 00139 n = q2_.Dequeue(); 00140 ASSERT_TRUE(n != NULL); 00141 EXPECT_EQ(2, *n); 00142 EXPECT_EQ(1u, q2_.Size()); 00143 delete n; 00144 } 00145 00146 // Tests the Queue::Map() function. 00147 TEST_F(QueueTest, Map) { 00148 MapTester(&q0_); 00149 MapTester(&q1_); 00150 MapTester(&q2_); 00151 }