media/webrtc/trunk/testing/gtest/samples/sample6_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample6_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +// Copyright 2008 Google Inc.
     1.5 +// All Rights Reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: wan@google.com (Zhanyong Wan)
    1.34 +
    1.35 +// This sample shows how to test common properties of multiple
    1.36 +// implementations of the same interface (aka interface tests).
    1.37 +
    1.38 +// The interface and its implementations are in this header.
    1.39 +#include "prime_tables.h"
    1.40 +
    1.41 +#include "gtest/gtest.h"
    1.42 +
    1.43 +// First, we define some factory functions for creating instances of
    1.44 +// the implementations.  You may be able to skip this step if all your
    1.45 +// implementations can be constructed the same way.
    1.46 +
    1.47 +template <class T>
    1.48 +PrimeTable* CreatePrimeTable();
    1.49 +
    1.50 +template <>
    1.51 +PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
    1.52 +  return new OnTheFlyPrimeTable;
    1.53 +}
    1.54 +
    1.55 +template <>
    1.56 +PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
    1.57 +  return new PreCalculatedPrimeTable(10000);
    1.58 +}
    1.59 +
    1.60 +// Then we define a test fixture class template.
    1.61 +template <class T>
    1.62 +class PrimeTableTest : public testing::Test {
    1.63 + protected:
    1.64 +  // The ctor calls the factory function to create a prime table
    1.65 +  // implemented by T.
    1.66 +  PrimeTableTest() : table_(CreatePrimeTable<T>()) {}
    1.67 +
    1.68 +  virtual ~PrimeTableTest() { delete table_; }
    1.69 +
    1.70 +  // Note that we test an implementation via the base interface
    1.71 +  // instead of the actual implementation class.  This is important
    1.72 +  // for keeping the tests close to the real world scenario, where the
    1.73 +  // implementation is invoked via the base interface.  It avoids
    1.74 +  // got-yas where the implementation class has a method that shadows
    1.75 +  // a method with the same name (but slightly different argument
    1.76 +  // types) in the base interface, for example.
    1.77 +  PrimeTable* const table_;
    1.78 +};
    1.79 +
    1.80 +#if GTEST_HAS_TYPED_TEST
    1.81 +
    1.82 +using testing::Types;
    1.83 +
    1.84 +// Google Test offers two ways for reusing tests for different types.
    1.85 +// The first is called "typed tests".  You should use it if you
    1.86 +// already know *all* the types you are gonna exercise when you write
    1.87 +// the tests.
    1.88 +
    1.89 +// To write a typed test case, first use
    1.90 +//
    1.91 +//   TYPED_TEST_CASE(TestCaseName, TypeList);
    1.92 +//
    1.93 +// to declare it and specify the type parameters.  As with TEST_F,
    1.94 +// TestCaseName must match the test fixture name.
    1.95 +
    1.96 +// The list of types we want to test.
    1.97 +typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
    1.98 +
    1.99 +TYPED_TEST_CASE(PrimeTableTest, Implementations);
   1.100 +
   1.101 +// Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
   1.102 +// similar to TEST_F.
   1.103 +TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
   1.104 +  // Inside the test body, you can refer to the type parameter by
   1.105 +  // TypeParam, and refer to the fixture class by TestFixture.  We
   1.106 +  // don't need them in this example.
   1.107 +
   1.108 +  // Since we are in the template world, C++ requires explicitly
   1.109 +  // writing 'this->' when referring to members of the fixture class.
   1.110 +  // This is something you have to learn to live with.
   1.111 +  EXPECT_FALSE(this->table_->IsPrime(-5));
   1.112 +  EXPECT_FALSE(this->table_->IsPrime(0));
   1.113 +  EXPECT_FALSE(this->table_->IsPrime(1));
   1.114 +  EXPECT_FALSE(this->table_->IsPrime(4));
   1.115 +  EXPECT_FALSE(this->table_->IsPrime(6));
   1.116 +  EXPECT_FALSE(this->table_->IsPrime(100));
   1.117 +}
   1.118 +
   1.119 +TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
   1.120 +  EXPECT_TRUE(this->table_->IsPrime(2));
   1.121 +  EXPECT_TRUE(this->table_->IsPrime(3));
   1.122 +  EXPECT_TRUE(this->table_->IsPrime(5));
   1.123 +  EXPECT_TRUE(this->table_->IsPrime(7));
   1.124 +  EXPECT_TRUE(this->table_->IsPrime(11));
   1.125 +  EXPECT_TRUE(this->table_->IsPrime(131));
   1.126 +}
   1.127 +
   1.128 +TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
   1.129 +  EXPECT_EQ(2, this->table_->GetNextPrime(0));
   1.130 +  EXPECT_EQ(3, this->table_->GetNextPrime(2));
   1.131 +  EXPECT_EQ(5, this->table_->GetNextPrime(3));
   1.132 +  EXPECT_EQ(7, this->table_->GetNextPrime(5));
   1.133 +  EXPECT_EQ(11, this->table_->GetNextPrime(7));
   1.134 +  EXPECT_EQ(131, this->table_->GetNextPrime(128));
   1.135 +}
   1.136 +
   1.137 +// That's it!  Google Test will repeat each TYPED_TEST for each type
   1.138 +// in the type list specified in TYPED_TEST_CASE.  Sit back and be
   1.139 +// happy that you don't have to define them multiple times.
   1.140 +
   1.141 +#endif  // GTEST_HAS_TYPED_TEST
   1.142 +
   1.143 +#if GTEST_HAS_TYPED_TEST_P
   1.144 +
   1.145 +using testing::Types;
   1.146 +
   1.147 +// Sometimes, however, you don't yet know all the types that you want
   1.148 +// to test when you write the tests.  For example, if you are the
   1.149 +// author of an interface and expect other people to implement it, you
   1.150 +// might want to write a set of tests to make sure each implementation
   1.151 +// conforms to some basic requirements, but you don't know what
   1.152 +// implementations will be written in the future.
   1.153 +//
   1.154 +// How can you write the tests without committing to the type
   1.155 +// parameters?  That's what "type-parameterized tests" can do for you.
   1.156 +// It is a bit more involved than typed tests, but in return you get a
   1.157 +// test pattern that can be reused in many contexts, which is a big
   1.158 +// win.  Here's how you do it:
   1.159 +
   1.160 +// First, define a test fixture class template.  Here we just reuse
   1.161 +// the PrimeTableTest fixture defined earlier:
   1.162 +
   1.163 +template <class T>
   1.164 +class PrimeTableTest2 : public PrimeTableTest<T> {
   1.165 +};
   1.166 +
   1.167 +// Then, declare the test case.  The argument is the name of the test
   1.168 +// fixture, and also the name of the test case (as usual).  The _P
   1.169 +// suffix is for "parameterized" or "pattern".
   1.170 +TYPED_TEST_CASE_P(PrimeTableTest2);
   1.171 +
   1.172 +// Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
   1.173 +// similar to what you do with TEST_F.
   1.174 +TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
   1.175 +  EXPECT_FALSE(this->table_->IsPrime(-5));
   1.176 +  EXPECT_FALSE(this->table_->IsPrime(0));
   1.177 +  EXPECT_FALSE(this->table_->IsPrime(1));
   1.178 +  EXPECT_FALSE(this->table_->IsPrime(4));
   1.179 +  EXPECT_FALSE(this->table_->IsPrime(6));
   1.180 +  EXPECT_FALSE(this->table_->IsPrime(100));
   1.181 +}
   1.182 +
   1.183 +TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
   1.184 +  EXPECT_TRUE(this->table_->IsPrime(2));
   1.185 +  EXPECT_TRUE(this->table_->IsPrime(3));
   1.186 +  EXPECT_TRUE(this->table_->IsPrime(5));
   1.187 +  EXPECT_TRUE(this->table_->IsPrime(7));
   1.188 +  EXPECT_TRUE(this->table_->IsPrime(11));
   1.189 +  EXPECT_TRUE(this->table_->IsPrime(131));
   1.190 +}
   1.191 +
   1.192 +TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
   1.193 +  EXPECT_EQ(2, this->table_->GetNextPrime(0));
   1.194 +  EXPECT_EQ(3, this->table_->GetNextPrime(2));
   1.195 +  EXPECT_EQ(5, this->table_->GetNextPrime(3));
   1.196 +  EXPECT_EQ(7, this->table_->GetNextPrime(5));
   1.197 +  EXPECT_EQ(11, this->table_->GetNextPrime(7));
   1.198 +  EXPECT_EQ(131, this->table_->GetNextPrime(128));
   1.199 +}
   1.200 +
   1.201 +// Type-parameterized tests involve one extra step: you have to
   1.202 +// enumerate the tests you defined:
   1.203 +REGISTER_TYPED_TEST_CASE_P(
   1.204 +    PrimeTableTest2,  // The first argument is the test case name.
   1.205 +    // The rest of the arguments are the test names.
   1.206 +    ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
   1.207 +
   1.208 +// At this point the test pattern is done.  However, you don't have
   1.209 +// any real test yet as you haven't said which types you want to run
   1.210 +// the tests with.
   1.211 +
   1.212 +// To turn the abstract test pattern into real tests, you instantiate
   1.213 +// it with a list of types.  Usually the test pattern will be defined
   1.214 +// in a .h file, and anyone can #include and instantiate it.  You can
   1.215 +// even instantiate it more than once in the same program.  To tell
   1.216 +// different instances apart, you give each of them a name, which will
   1.217 +// become part of the test case name and can be used in test filters.
   1.218 +
   1.219 +// The list of types we want to test.  Note that it doesn't have to be
   1.220 +// defined at the time we write the TYPED_TEST_P()s.
   1.221 +typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
   1.222 +    PrimeTableImplementations;
   1.223 +INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated,    // Instance name
   1.224 +                              PrimeTableTest2,             // Test case name
   1.225 +                              PrimeTableImplementations);  // Type list
   1.226 +
   1.227 +#endif  // GTEST_HAS_TYPED_TEST_P

mercurial