media/webrtc/trunk/testing/gtest/samples/sample7_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/sample7_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     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: vladl@google.com (Vlad Losev)
    1.34 +
    1.35 +// This sample shows how to test common properties of multiple
    1.36 +// implementations of an interface (aka interface tests) using
    1.37 +// value-parameterized tests. Each test in the test case has
    1.38 +// a parameter that is an interface pointer to an implementation
    1.39 +// tested.
    1.40 +
    1.41 +// The interface and its implementations are in this header.
    1.42 +#include "prime_tables.h"
    1.43 +
    1.44 +#include "gtest/gtest.h"
    1.45 +
    1.46 +#if GTEST_HAS_PARAM_TEST
    1.47 +
    1.48 +using ::testing::TestWithParam;
    1.49 +using ::testing::Values;
    1.50 +
    1.51 +// As a general rule, to prevent a test from affecting the tests that come
    1.52 +// after it, you should create and destroy the tested objects for each test
    1.53 +// instead of reusing them.  In this sample we will define a simple factory
    1.54 +// function for PrimeTable objects.  We will instantiate objects in test's
    1.55 +// SetUp() method and delete them in TearDown() method.
    1.56 +typedef PrimeTable* CreatePrimeTableFunc();
    1.57 +
    1.58 +PrimeTable* CreateOnTheFlyPrimeTable() {
    1.59 +  return new OnTheFlyPrimeTable();
    1.60 +}
    1.61 +
    1.62 +template <size_t max_precalculated>
    1.63 +PrimeTable* CreatePreCalculatedPrimeTable() {
    1.64 +  return new PreCalculatedPrimeTable(max_precalculated);
    1.65 +}
    1.66 +
    1.67 +// Inside the test body, fixture constructor, SetUp(), and TearDown() you
    1.68 +// can refer to the test parameter by GetParam().  In this case, the test
    1.69 +// parameter is a factory function which we call in fixture's SetUp() to
    1.70 +// create and store an instance of PrimeTable.
    1.71 +class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
    1.72 + public:
    1.73 +  virtual ~PrimeTableTest() { delete table_; }
    1.74 +  virtual void SetUp() { table_ = (*GetParam())(); }
    1.75 +  virtual void TearDown() {
    1.76 +    delete table_;
    1.77 +    table_ = NULL;
    1.78 +  }
    1.79 +
    1.80 + protected:
    1.81 +  PrimeTable* table_;
    1.82 +};
    1.83 +
    1.84 +TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
    1.85 +  EXPECT_FALSE(table_->IsPrime(-5));
    1.86 +  EXPECT_FALSE(table_->IsPrime(0));
    1.87 +  EXPECT_FALSE(table_->IsPrime(1));
    1.88 +  EXPECT_FALSE(table_->IsPrime(4));
    1.89 +  EXPECT_FALSE(table_->IsPrime(6));
    1.90 +  EXPECT_FALSE(table_->IsPrime(100));
    1.91 +}
    1.92 +
    1.93 +TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
    1.94 +  EXPECT_TRUE(table_->IsPrime(2));
    1.95 +  EXPECT_TRUE(table_->IsPrime(3));
    1.96 +  EXPECT_TRUE(table_->IsPrime(5));
    1.97 +  EXPECT_TRUE(table_->IsPrime(7));
    1.98 +  EXPECT_TRUE(table_->IsPrime(11));
    1.99 +  EXPECT_TRUE(table_->IsPrime(131));
   1.100 +}
   1.101 +
   1.102 +TEST_P(PrimeTableTest, CanGetNextPrime) {
   1.103 +  EXPECT_EQ(2, table_->GetNextPrime(0));
   1.104 +  EXPECT_EQ(3, table_->GetNextPrime(2));
   1.105 +  EXPECT_EQ(5, table_->GetNextPrime(3));
   1.106 +  EXPECT_EQ(7, table_->GetNextPrime(5));
   1.107 +  EXPECT_EQ(11, table_->GetNextPrime(7));
   1.108 +  EXPECT_EQ(131, table_->GetNextPrime(128));
   1.109 +}
   1.110 +
   1.111 +// In order to run value-parameterized tests, you need to instantiate them,
   1.112 +// or bind them to a list of values which will be used as test parameters.
   1.113 +// You can instantiate them in a different translation module, or even
   1.114 +// instantiate them several times.
   1.115 +//
   1.116 +// Here, we instantiate our tests with a list of two PrimeTable object
   1.117 +// factory functions:
   1.118 +INSTANTIATE_TEST_CASE_P(
   1.119 +    OnTheFlyAndPreCalculated,
   1.120 +    PrimeTableTest,
   1.121 +    Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>));
   1.122 +
   1.123 +#else
   1.124 +
   1.125 +// Google Test may not support value-parameterized tests with some
   1.126 +// compilers. If we use conditional compilation to compile out all
   1.127 +// code referring to the gtest_main library, MSVC linker will not link
   1.128 +// that library at all and consequently complain about missing entry
   1.129 +// point defined in that library (fatal error LNK1561: entry point
   1.130 +// must be defined). This dummy test keeps gtest_main linked in.
   1.131 +TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {}
   1.132 +
   1.133 +#endif  // GTEST_HAS_PARAM_TEST

mercurial