1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // A sample program demonstrating using Google C++ testing framework.
32 // In this example, we use a more advanced feature of Google Test called
35 // A test fixture is a place to hold objects and functions shared by
36 // all tests in a test case. Using a test fixture avoids duplicating
37 // the test code necessary to initialize and cleanup those common
38 // objects for each test. It is also useful for defining sub-routines
39 // that your tests need to invoke a lot.
43 // The tests share the test fixture in the sense of code sharing, not
44 // data sharing. Each test is given its own fresh copy of the
45 // fixture. You cannot expect the data modified by one test to be
46 // passed on to another test, which is a bad idea.
48 // The reason for this design is that tests should be independent and
49 // repeatable. In particular, a test should not fail as the result of
50 // another test's failure. If one test depends on info produced by
51 // another test, then the two tests should really be one big test.
53 // The macros for indicating the success/failure of a test
54 // (EXPECT_TRUE, FAIL, etc) need to know what the current test is
55 // (when Google Test prints the test result, it tells you which test
56 // each failure belongs to). Technically, these macros invoke a
57 // member function of the Test class. Therefore, you cannot use them
58 // in a global function. That's why you should put test sub-routines
61 // </TechnicalDetails>
63 #include "sample3-inl.h"
64 #include "gtest/gtest.h"
66 // To use a test fixture, derive a class from testing::Test.
67 class QueueTestSmpl3 : public testing::Test {
68 protected: // You should make the members protected s.t. they can be
69 // accessed from sub-classes.
71 // virtual void SetUp() will be called before each test is run. You
72 // should define it if you need to initialize the variables.
73 // Otherwise, this can be skipped.
74 virtual void SetUp() {
80 // virtual void TearDown() will be called after each test is run.
81 // You should define it if there is cleanup work to do. Otherwise,
82 // you don't have to provide it.
84 // virtual void TearDown() {
87 // A helper function that some test uses.
88 static int Double(int n) {
92 // A helper function for testing Queue::Map().
93 void MapTester(const Queue<int> * q) {
94 // Creates a new queue, where each element is twice as big as the
95 // corresponding one in q.
96 const Queue<int> * const new_q = q->Map(Double);
98 // Verifies that the new queue has the same size as q.
99 ASSERT_EQ(q->Size(), new_q->Size());
101 // Verifies the relationship between the elements of the two queues.
102 for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
103 n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
104 EXPECT_EQ(2 * n1->element(), n2->element());
110 // Declares the variables your tests want to use.
116 // When you have a test fixture, you define a test using TEST_F
119 // Tests the default c'tor.
120 TEST_F(QueueTestSmpl3, DefaultConstructor) {
121 // You can access data in the test fixture here.
122 EXPECT_EQ(0u, q0_.Size());
126 TEST_F(QueueTestSmpl3, Dequeue) {
127 int * n = q0_.Dequeue();
128 EXPECT_TRUE(n == NULL);
131 ASSERT_TRUE(n != NULL);
133 EXPECT_EQ(0u, q1_.Size());
137 ASSERT_TRUE(n != NULL);
139 EXPECT_EQ(1u, q2_.Size());
143 // Tests the Queue::Map() function.
144 TEST_F(QueueTestSmpl3, Map) {