source: gtest-1.7.0/samples/sample8_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: 6.8 KB
Line 
1// Copyright 2008 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// Author: vladl@google.com (Vlad Losev)
31
32// This sample shows how to test code relying on some global flag variables.
33// Combine() helps with generating all possible combinations of such flags,
34// and each test is given one combination as a parameter.
35
36// Use class definitions to test from this header.
37#include "prime_tables.h"
38
39#include "gtest/gtest.h"
40
41#if GTEST_HAS_COMBINE
42
43// Suppose we want to introduce a new, improved implementation of PrimeTable
44// which combines speed of PrecalcPrimeTable and versatility of
45// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
46// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
47// appropriate under the circumstances. But in low memory conditions, it can be
48// told to instantiate without PrecalcPrimeTable instance at all and use only
49// OnTheFlyPrimeTable.
50class HybridPrimeTable : public PrimeTable {
51 public:
52 HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
53 : on_the_fly_impl_(new OnTheFlyPrimeTable),
54 precalc_impl_(force_on_the_fly ? NULL :
55 new PreCalculatedPrimeTable(max_precalculated)),
56 max_precalculated_(max_precalculated) {}
57 virtual ~HybridPrimeTable() {
58 delete on_the_fly_impl_;
59 delete precalc_impl_;
60 }
61
62 virtual bool IsPrime(int n) const {
63 if (precalc_impl_ != NULL && n < max_precalculated_)
64 return precalc_impl_->IsPrime(n);
65 else
66 return on_the_fly_impl_->IsPrime(n);
67 }
68
69 virtual int GetNextPrime(int p) const {
70 int next_prime = -1;
71 if (precalc_impl_ != NULL && p < max_precalculated_)
72 next_prime = precalc_impl_->GetNextPrime(p);
73
74 return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
75 }
76
77 private:
78 OnTheFlyPrimeTable* on_the_fly_impl_;
79 PreCalculatedPrimeTable* precalc_impl_;
80 int max_precalculated_;
81};
82
83using ::testing::TestWithParam;
84using ::testing::Bool;
85using ::testing::Values;
86using ::testing::Combine;
87
88// To test all code paths for HybridPrimeTable we must test it with numbers
89// both within and outside PreCalculatedPrimeTable's capacity and also with
90// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
91// accept different combinations of parameters for instantiating a
92// HybridPrimeTable instance.
93class PrimeTableTest : public TestWithParam< ::std::tr1::tuple<bool, int> > {
94 protected:
95 virtual void SetUp() {
96 // This can be written as
97 //
98 // bool force_on_the_fly;
99 // int max_precalculated;
100 // tie(force_on_the_fly, max_precalculated) = GetParam();
101 //
102 // once the Google C++ Style Guide allows use of ::std::tr1::tie.
103 //
104 bool force_on_the_fly = ::std::tr1::get<0>(GetParam());
105 int max_precalculated = ::std::tr1::get<1>(GetParam());
106 table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
107 }
108 virtual void TearDown() {
109 delete table_;
110 table_ = NULL;
111 }
112 HybridPrimeTable* table_;
113};
114
115TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
116 // Inside the test body, you can refer to the test parameter by GetParam().
117 // In this case, the test parameter is a PrimeTable interface pointer which
118 // we can use directly.
119 // Please note that you can also save it in the fixture's SetUp() method
120 // or constructor and use saved copy in the tests.
121
122 EXPECT_FALSE(table_->IsPrime(-5));
123 EXPECT_FALSE(table_->IsPrime(0));
124 EXPECT_FALSE(table_->IsPrime(1));
125 EXPECT_FALSE(table_->IsPrime(4));
126 EXPECT_FALSE(table_->IsPrime(6));
127 EXPECT_FALSE(table_->IsPrime(100));
128}
129
130TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
131 EXPECT_TRUE(table_->IsPrime(2));
132 EXPECT_TRUE(table_->IsPrime(3));
133 EXPECT_TRUE(table_->IsPrime(5));
134 EXPECT_TRUE(table_->IsPrime(7));
135 EXPECT_TRUE(table_->IsPrime(11));
136 EXPECT_TRUE(table_->IsPrime(131));
137}
138
139TEST_P(PrimeTableTest, CanGetNextPrime) {
140 EXPECT_EQ(2, table_->GetNextPrime(0));
141 EXPECT_EQ(3, table_->GetNextPrime(2));
142 EXPECT_EQ(5, table_->GetNextPrime(3));
143 EXPECT_EQ(7, table_->GetNextPrime(5));
144 EXPECT_EQ(11, table_->GetNextPrime(7));
145 EXPECT_EQ(131, table_->GetNextPrime(128));
146}
147
148// In order to run value-parameterized tests, you need to instantiate them,
149// or bind them to a list of values which will be used as test parameters.
150// You can instantiate them in a different translation module, or even
151// instantiate them several times.
152//
153// Here, we instantiate our tests with a list of parameters. We must combine
154// all variations of the boolean flag suppressing PrecalcPrimeTable and some
155// meaningful values for tests. We choose a small value (1), and a value that
156// will put some of the tested numbers beyond the capability of the
157// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
158// possible combinations.
159INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
160 PrimeTableTest,
161 Combine(Bool(), Values(1, 10)));
162
163#else
164
165// Google Test may not support Combine() with some compilers. If we
166// use conditional compilation to compile out all code referring to
167// the gtest_main library, MSVC linker will not link that library at
168// all and consequently complain about missing entry point defined in
169// that library (fatal error LNK1561: entry point must be
170// defined). This dummy test keeps gtest_main linked in.
171TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
172
173#endif // GTEST_HAS_COMBINE
Note: See TracBrowser for help on using the repository browser.