source: gtest-1.7.0/samples/sample3_unittest.cc@ 12747

Last change on this file since 12747 was 12746, checked in by hock@…, 11 years ago

integrated the Google Testing Framework (gtest)

and wrote an Hello World test, to ensure the framework is working..

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