sample6_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2008 Google Inc.
00002 // All Rights Reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // This sample shows how to test common properties of multiple
00033 // implementations of the same interface (aka interface tests).
00034 
00035 // The interface and its implementations are in this header.
00036 #include "prime_tables.h"
00037 
00038 #include "gtest/gtest.h"
00039 
00040 // First, we define some factory functions for creating instances of
00041 // the implementations.  You may be able to skip this step if all your
00042 // implementations can be constructed the same way.
00043 
00044 template <class T>
00045 PrimeTable* CreatePrimeTable();
00046 
00047 template <>
00048 PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
00049   return new OnTheFlyPrimeTable;
00050 }
00051 
00052 template <>
00053 PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
00054   return new PreCalculatedPrimeTable(10000);
00055 }
00056 
00057 // Then we define a test fixture class template.
00058 template <class T>
00059 class PrimeTableTest : public testing::Test {
00060  protected:
00061   // The ctor calls the factory function to create a prime table
00062   // implemented by T.
00063   PrimeTableTest() : table_(CreatePrimeTable<T>()) {}
00064 
00065   virtual ~PrimeTableTest() { delete table_; }
00066 
00067   // Note that we test an implementation via the base interface
00068   // instead of the actual implementation class.  This is important
00069   // for keeping the tests close to the real world scenario, where the
00070   // implementation is invoked via the base interface.  It avoids
00071   // got-yas where the implementation class has a method that shadows
00072   // a method with the same name (but slightly different argument
00073   // types) in the base interface, for example.
00074   PrimeTable* const table_;
00075 };
00076 
00077 #if GTEST_HAS_TYPED_TEST
00078 
00079 using testing::Types;
00080 
00081 // Google Test offers two ways for reusing tests for different types.
00082 // The first is called "typed tests".  You should use it if you
00083 // already know *all* the types you are gonna exercise when you write
00084 // the tests.
00085 
00086 // To write a typed test case, first use
00087 //
00088 //   TYPED_TEST_CASE(TestCaseName, TypeList);
00089 //
00090 // to declare it and specify the type parameters.  As with TEST_F,
00091 // TestCaseName must match the test fixture name.
00092 
00093 // The list of types we want to test.
00094 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
00095 
00096 TYPED_TEST_CASE(PrimeTableTest, Implementations);
00097 
00098 // Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
00099 // similar to TEST_F.
00100 TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
00101   // Inside the test body, you can refer to the type parameter by
00102   // TypeParam, and refer to the fixture class by TestFixture.  We
00103   // don't need them in this example.
00104 
00105   // Since we are in the template world, C++ requires explicitly
00106   // writing 'this->' when referring to members of the fixture class.
00107   // This is something you have to learn to live with.
00108   EXPECT_FALSE(this->table_->IsPrime(-5));
00109   EXPECT_FALSE(this->table_->IsPrime(0));
00110   EXPECT_FALSE(this->table_->IsPrime(1));
00111   EXPECT_FALSE(this->table_->IsPrime(4));
00112   EXPECT_FALSE(this->table_->IsPrime(6));
00113   EXPECT_FALSE(this->table_->IsPrime(100));
00114 }
00115 
00116 TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
00117   EXPECT_TRUE(this->table_->IsPrime(2));
00118   EXPECT_TRUE(this->table_->IsPrime(3));
00119   EXPECT_TRUE(this->table_->IsPrime(5));
00120   EXPECT_TRUE(this->table_->IsPrime(7));
00121   EXPECT_TRUE(this->table_->IsPrime(11));
00122   EXPECT_TRUE(this->table_->IsPrime(131));
00123 }
00124 
00125 TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
00126   EXPECT_EQ(2, this->table_->GetNextPrime(0));
00127   EXPECT_EQ(3, this->table_->GetNextPrime(2));
00128   EXPECT_EQ(5, this->table_->GetNextPrime(3));
00129   EXPECT_EQ(7, this->table_->GetNextPrime(5));
00130   EXPECT_EQ(11, this->table_->GetNextPrime(7));
00131   EXPECT_EQ(131, this->table_->GetNextPrime(128));
00132 }
00133 
00134 // That's it!  Google Test will repeat each TYPED_TEST for each type
00135 // in the type list specified in TYPED_TEST_CASE.  Sit back and be
00136 // happy that you don't have to define them multiple times.
00137 
00138 #endif  // GTEST_HAS_TYPED_TEST
00139 
00140 #if GTEST_HAS_TYPED_TEST_P
00141 
00142 using testing::Types;
00143 
00144 // Sometimes, however, you don't yet know all the types that you want
00145 // to test when you write the tests.  For example, if you are the
00146 // author of an interface and expect other people to implement it, you
00147 // might want to write a set of tests to make sure each implementation
00148 // conforms to some basic requirements, but you don't know what
00149 // implementations will be written in the future.
00150 //
00151 // How can you write the tests without committing to the type
00152 // parameters?  That's what "type-parameterized tests" can do for you.
00153 // It is a bit more involved than typed tests, but in return you get a
00154 // test pattern that can be reused in many contexts, which is a big
00155 // win.  Here's how you do it:
00156 
00157 // First, define a test fixture class template.  Here we just reuse
00158 // the PrimeTableTest fixture defined earlier:
00159 
00160 template <class T>
00161 class PrimeTableTest2 : public PrimeTableTest<T> {
00162 };
00163 
00164 // Then, declare the test case.  The argument is the name of the test
00165 // fixture, and also the name of the test case (as usual).  The _P
00166 // suffix is for "parameterized" or "pattern".
00167 TYPED_TEST_CASE_P(PrimeTableTest2);
00168 
00169 // Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
00170 // similar to what you do with TEST_F.
00171 TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
00172   EXPECT_FALSE(this->table_->IsPrime(-5));
00173   EXPECT_FALSE(this->table_->IsPrime(0));
00174   EXPECT_FALSE(this->table_->IsPrime(1));
00175   EXPECT_FALSE(this->table_->IsPrime(4));
00176   EXPECT_FALSE(this->table_->IsPrime(6));
00177   EXPECT_FALSE(this->table_->IsPrime(100));
00178 }
00179 
00180 TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
00181   EXPECT_TRUE(this->table_->IsPrime(2));
00182   EXPECT_TRUE(this->table_->IsPrime(3));
00183   EXPECT_TRUE(this->table_->IsPrime(5));
00184   EXPECT_TRUE(this->table_->IsPrime(7));
00185   EXPECT_TRUE(this->table_->IsPrime(11));
00186   EXPECT_TRUE(this->table_->IsPrime(131));
00187 }
00188 
00189 TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
00190   EXPECT_EQ(2, this->table_->GetNextPrime(0));
00191   EXPECT_EQ(3, this->table_->GetNextPrime(2));
00192   EXPECT_EQ(5, this->table_->GetNextPrime(3));
00193   EXPECT_EQ(7, this->table_->GetNextPrime(5));
00194   EXPECT_EQ(11, this->table_->GetNextPrime(7));
00195   EXPECT_EQ(131, this->table_->GetNextPrime(128));
00196 }
00197 
00198 // Type-parameterized tests involve one extra step: you have to
00199 // enumerate the tests you defined:
00200 REGISTER_TYPED_TEST_CASE_P(
00201     PrimeTableTest2,  // The first argument is the test case name.
00202     // The rest of the arguments are the test names.
00203     ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
00204 
00205 // At this point the test pattern is done.  However, you don't have
00206 // any real test yet as you haven't said which types you want to run
00207 // the tests with.
00208 
00209 // To turn the abstract test pattern into real tests, you instantiate
00210 // it with a list of types.  Usually the test pattern will be defined
00211 // in a .h file, and anyone can #include and instantiate it.  You can
00212 // even instantiate it more than once in the same program.  To tell
00213 // different instances apart, you give each of them a name, which will
00214 // become part of the test case name and can be used in test filters.
00215 
00216 // The list of types we want to test.  Note that it doesn't have to be
00217 // defined at the time we write the TYPED_TEST_P()s.
00218 typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
00219     PrimeTableImplementations;
00220 INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated,    // Instance name
00221                               PrimeTableTest2,             // Test case name
00222                               PrimeTableImplementations);  // Type list
00223 
00224 #endif  // GTEST_HAS_TYPED_TEST_P


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56