Wed, 31 Dec 2014 07:53:36 +0100
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 | // A sample program demonstrating using Google C++ testing framework. |
michael@0 | 31 | // |
michael@0 | 32 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | // In this example, we use a more advanced feature of Google Test called |
michael@0 | 36 | // test fixture. |
michael@0 | 37 | // |
michael@0 | 38 | // A test fixture is a place to hold objects and functions shared by |
michael@0 | 39 | // all tests in a test case. Using a test fixture avoids duplicating |
michael@0 | 40 | // the test code necessary to initialize and cleanup those common |
michael@0 | 41 | // objects for each test. It is also useful for defining sub-routines |
michael@0 | 42 | // that your tests need to invoke a lot. |
michael@0 | 43 | // |
michael@0 | 44 | // <TechnicalDetails> |
michael@0 | 45 | // |
michael@0 | 46 | // The tests share the test fixture in the sense of code sharing, not |
michael@0 | 47 | // data sharing. Each test is given its own fresh copy of the |
michael@0 | 48 | // fixture. You cannot expect the data modified by one test to be |
michael@0 | 49 | // passed on to another test, which is a bad idea. |
michael@0 | 50 | // |
michael@0 | 51 | // The reason for this design is that tests should be independent and |
michael@0 | 52 | // repeatable. In particular, a test should not fail as the result of |
michael@0 | 53 | // another test's failure. If one test depends on info produced by |
michael@0 | 54 | // another test, then the two tests should really be one big test. |
michael@0 | 55 | // |
michael@0 | 56 | // The macros for indicating the success/failure of a test |
michael@0 | 57 | // (EXPECT_TRUE, FAIL, etc) need to know what the current test is |
michael@0 | 58 | // (when Google Test prints the test result, it tells you which test |
michael@0 | 59 | // each failure belongs to). Technically, these macros invoke a |
michael@0 | 60 | // member function of the Test class. Therefore, you cannot use them |
michael@0 | 61 | // in a global function. That's why you should put test sub-routines |
michael@0 | 62 | // in a test fixture. |
michael@0 | 63 | // |
michael@0 | 64 | // </TechnicalDetails> |
michael@0 | 65 | |
michael@0 | 66 | #include "sample3-inl.h" |
michael@0 | 67 | #include "gtest/gtest.h" |
michael@0 | 68 | |
michael@0 | 69 | // To use a test fixture, derive a class from testing::Test. |
michael@0 | 70 | class QueueTest : public testing::Test { |
michael@0 | 71 | protected: // You should make the members protected s.t. they can be |
michael@0 | 72 | // accessed from sub-classes. |
michael@0 | 73 | |
michael@0 | 74 | // virtual void SetUp() will be called before each test is run. You |
michael@0 | 75 | // should define it if you need to initialize the varaibles. |
michael@0 | 76 | // Otherwise, this can be skipped. |
michael@0 | 77 | virtual void SetUp() { |
michael@0 | 78 | q1_.Enqueue(1); |
michael@0 | 79 | q2_.Enqueue(2); |
michael@0 | 80 | q2_.Enqueue(3); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // virtual void TearDown() will be called after each test is run. |
michael@0 | 84 | // You should define it if there is cleanup work to do. Otherwise, |
michael@0 | 85 | // you don't have to provide it. |
michael@0 | 86 | // |
michael@0 | 87 | // virtual void TearDown() { |
michael@0 | 88 | // } |
michael@0 | 89 | |
michael@0 | 90 | // A helper function that some test uses. |
michael@0 | 91 | static int Double(int n) { |
michael@0 | 92 | return 2*n; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // A helper function for testing Queue::Map(). |
michael@0 | 96 | void MapTester(const Queue<int> * q) { |
michael@0 | 97 | // Creates a new queue, where each element is twice as big as the |
michael@0 | 98 | // corresponding one in q. |
michael@0 | 99 | const Queue<int> * const new_q = q->Map(Double); |
michael@0 | 100 | |
michael@0 | 101 | // Verifies that the new queue has the same size as q. |
michael@0 | 102 | ASSERT_EQ(q->Size(), new_q->Size()); |
michael@0 | 103 | |
michael@0 | 104 | // Verifies the relationship between the elements of the two queues. |
michael@0 | 105 | for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head(); |
michael@0 | 106 | n1 != NULL; n1 = n1->next(), n2 = n2->next() ) { |
michael@0 | 107 | EXPECT_EQ(2 * n1->element(), n2->element()); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | delete new_q; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // Declares the variables your tests want to use. |
michael@0 | 114 | Queue<int> q0_; |
michael@0 | 115 | Queue<int> q1_; |
michael@0 | 116 | Queue<int> q2_; |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | // When you have a test fixture, you define a test using TEST_F |
michael@0 | 120 | // instead of TEST. |
michael@0 | 121 | |
michael@0 | 122 | // Tests the default c'tor. |
michael@0 | 123 | TEST_F(QueueTest, DefaultConstructor) { |
michael@0 | 124 | // You can access data in the test fixture here. |
michael@0 | 125 | EXPECT_EQ(0u, q0_.Size()); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // Tests Dequeue(). |
michael@0 | 129 | TEST_F(QueueTest, Dequeue) { |
michael@0 | 130 | int * n = q0_.Dequeue(); |
michael@0 | 131 | EXPECT_TRUE(n == NULL); |
michael@0 | 132 | |
michael@0 | 133 | n = q1_.Dequeue(); |
michael@0 | 134 | ASSERT_TRUE(n != NULL); |
michael@0 | 135 | EXPECT_EQ(1, *n); |
michael@0 | 136 | EXPECT_EQ(0u, q1_.Size()); |
michael@0 | 137 | delete n; |
michael@0 | 138 | |
michael@0 | 139 | n = q2_.Dequeue(); |
michael@0 | 140 | ASSERT_TRUE(n != NULL); |
michael@0 | 141 | EXPECT_EQ(2, *n); |
michael@0 | 142 | EXPECT_EQ(1u, q2_.Size()); |
michael@0 | 143 | delete n; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // Tests the Queue::Map() function. |
michael@0 | 147 | TEST_F(QueueTest, Map) { |
michael@0 | 148 | MapTester(&q0_); |
michael@0 | 149 | MapTester(&q1_); |
michael@0 | 150 | MapTester(&q2_); |
michael@0 | 151 | } |