]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/googletest/googletest/samples/sample7_unittest.cc
MFC r345203,r345205,r345353,r345645,r345708,r345709,r345735,r345770,r346574,r346576:
[FreeBSD/FreeBSD.git] / contrib / googletest / googletest / samples / sample7_unittest.cc
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
31 // This sample shows how to test common properties of multiple
32 // implementations of an interface (aka interface tests) using
33 // value-parameterized tests. Each test in the test case has
34 // a parameter that is an interface pointer to an implementation
35 // tested.
36
37 // The interface and its implementations are in this header.
38 #include "prime_tables.h"
39
40 #include "gtest/gtest.h"
41 namespace {
42
43 using ::testing::TestWithParam;
44 using ::testing::Values;
45
46 // As a general rule, to prevent a test from affecting the tests that come
47 // after it, you should create and destroy the tested objects for each test
48 // instead of reusing them.  In this sample we will define a simple factory
49 // function for PrimeTable objects.  We will instantiate objects in test's
50 // SetUp() method and delete them in TearDown() method.
51 typedef PrimeTable* CreatePrimeTableFunc();
52
53 PrimeTable* CreateOnTheFlyPrimeTable() {
54   return new OnTheFlyPrimeTable();
55 }
56
57 template <size_t max_precalculated>
58 PrimeTable* CreatePreCalculatedPrimeTable() {
59   return new PreCalculatedPrimeTable(max_precalculated);
60 }
61
62 // Inside the test body, fixture constructor, SetUp(), and TearDown() you
63 // can refer to the test parameter by GetParam().  In this case, the test
64 // parameter is a factory function which we call in fixture's SetUp() to
65 // create and store an instance of PrimeTable.
66 class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
67  public:
68   virtual ~PrimeTableTestSmpl7() { delete table_; }
69   virtual void SetUp() { table_ = (*GetParam())(); }
70   virtual void TearDown() {
71     delete table_;
72     table_ = NULL;
73   }
74
75  protected:
76   PrimeTable* table_;
77 };
78
79 TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
80   EXPECT_FALSE(table_->IsPrime(-5));
81   EXPECT_FALSE(table_->IsPrime(0));
82   EXPECT_FALSE(table_->IsPrime(1));
83   EXPECT_FALSE(table_->IsPrime(4));
84   EXPECT_FALSE(table_->IsPrime(6));
85   EXPECT_FALSE(table_->IsPrime(100));
86 }
87
88 TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
89   EXPECT_TRUE(table_->IsPrime(2));
90   EXPECT_TRUE(table_->IsPrime(3));
91   EXPECT_TRUE(table_->IsPrime(5));
92   EXPECT_TRUE(table_->IsPrime(7));
93   EXPECT_TRUE(table_->IsPrime(11));
94   EXPECT_TRUE(table_->IsPrime(131));
95 }
96
97 TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
98   EXPECT_EQ(2, table_->GetNextPrime(0));
99   EXPECT_EQ(3, table_->GetNextPrime(2));
100   EXPECT_EQ(5, table_->GetNextPrime(3));
101   EXPECT_EQ(7, table_->GetNextPrime(5));
102   EXPECT_EQ(11, table_->GetNextPrime(7));
103   EXPECT_EQ(131, table_->GetNextPrime(128));
104 }
105
106 // In order to run value-parameterized tests, you need to instantiate them,
107 // or bind them to a list of values which will be used as test parameters.
108 // You can instantiate them in a different translation module, or even
109 // instantiate them several times.
110 //
111 // Here, we instantiate our tests with a list of two PrimeTable object
112 // factory functions:
113 INSTANTIATE_TEST_CASE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
114                         Values(&CreateOnTheFlyPrimeTable,
115                                &CreatePreCalculatedPrimeTable<1000>));
116
117 }  // namespace