From 3d7042176307f0d7700a3640f3b3bcc8790b8fcd Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 20 Nov 2008 01:40:35 +0000 Subject: Value-parameterized tests and many bugfixes --- samples/sample7_unittest.cc | 132 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 samples/sample7_unittest.cc (limited to 'samples/sample7_unittest.cc') diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc new file mode 100644 index 00000000..7f555c4f --- /dev/null +++ b/samples/sample7_unittest.cc @@ -0,0 +1,132 @@ +// Copyright 2008 Google Inc. +// All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: vladl@google.com (Vlad Losev) + +// This sample shows how to test common properties of multiple +// implementations of an interface (aka interface tests) using +// value-parameterized tests. Each test in the test case has +// a parameter that is an interface pointer to an implementation +// tested. + +// The interface and its implementations are in this header. +#include "prime_tables.h" + +#include + +#ifdef GTEST_HAS_PARAM_TEST + +using ::testing::TestWithParam; +using ::testing::Values; + +// As a general rule, tested objects should not be reused between tests. +// Also, their constructors and destructors of tested objects can have +// side effects. Thus you should create and destroy them for each test. +// In this sample we will define a simple factory function for PrimeTable +// objects. We will instantiate objects in test's SetUp() method and +// delete them in TearDown() method. +typedef PrimeTable* CreatePrimeTableFunc(); + +PrimeTable* CreateOnTheFlyPrimeTable() { + return new OnTheFlyPrimeTable(); +} + +template +PrimeTable* CreatePreCalculatedPrimeTable() { + return new PreCalculatedPrimeTable(max_precalculated); +} + +// Inside the test body, fixture constructor, SetUp(), and TearDown() +// you can refer to the test parameter by GetParam(). +// In this case, the test parameter is a PrimeTableFactory interface pointer +// which we use in fixture's SetUp() to create and store an instance of +// PrimeTable. +class PrimeTableTest : public TestWithParam { + public: + virtual ~PrimeTableTest() { delete table_; } + virtual void SetUp() { table_ = (*GetParam())(); } + virtual void TearDown() { + delete table_; + table_ = NULL; + } + + protected: + PrimeTable* table_; +}; + +TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) { + EXPECT_FALSE(table_->IsPrime(-5)); + EXPECT_FALSE(table_->IsPrime(0)); + EXPECT_FALSE(table_->IsPrime(1)); + EXPECT_FALSE(table_->IsPrime(4)); + EXPECT_FALSE(table_->IsPrime(6)); + EXPECT_FALSE(table_->IsPrime(100)); +} + +TEST_P(PrimeTableTest, ReturnsTrueForPrimes) { + EXPECT_TRUE(table_->IsPrime(2)); + EXPECT_TRUE(table_->IsPrime(3)); + EXPECT_TRUE(table_->IsPrime(5)); + EXPECT_TRUE(table_->IsPrime(7)); + EXPECT_TRUE(table_->IsPrime(11)); + EXPECT_TRUE(table_->IsPrime(131)); +} + +TEST_P(PrimeTableTest, CanGetNextPrime) { + EXPECT_EQ(2, table_->GetNextPrime(0)); + EXPECT_EQ(3, table_->GetNextPrime(2)); + EXPECT_EQ(5, table_->GetNextPrime(3)); + EXPECT_EQ(7, table_->GetNextPrime(5)); + EXPECT_EQ(11, table_->GetNextPrime(7)); + EXPECT_EQ(131, table_->GetNextPrime(128)); +} + +// In order to run value-parameterized tests, you need to instantiate them, +// or bind them to a list of values which will be used as test parameters. +// You can instantiate them in a different translation module, or even +// instantiate them several times. +// +// Here, we instantiate our tests with a list of two PrimeTable object +// factory functions: +INSTANTIATE_TEST_CASE_P( + OnTheFlyAndPreCalculated, + PrimeTableTest, + Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>)); + +#else + +// Google Test doesn't support value-parameterized tests on some platforms +// and compilers, such as MSVC 7.1. If we use conditional compilation to +// compile out all code referring to the gtest_main library, MSVC linker +// will not link that library at all and consequently complain about +// missing entry point defined in that library (fatal error LNK1561: +// entry point must be defined). This dummy test keeps gtest_main linked in. +TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {} + +#endif // GTEST_HAS_PARAM_TEST -- cgit v1.2.3 From 0af0709b02899f9177db55eba7929e65e5834b29 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 23 Feb 2009 23:21:55 +0000 Subject: Cleans up macro definitions. --- samples/sample7_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample7_unittest.cc') diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc index 7f555c4f..b5d507a9 100644 --- a/samples/sample7_unittest.cc +++ b/samples/sample7_unittest.cc @@ -40,7 +40,7 @@ #include -#ifdef GTEST_HAS_PARAM_TEST +#if GTEST_HAS_PARAM_TEST using ::testing::TestWithParam; using ::testing::Values; -- cgit v1.2.3 From 12a92c26fc0e0de81f687dbe739a6aa24f37f9dd Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 4 Mar 2010 22:15:53 +0000 Subject: Renames ThreadStartSempahore to Notificaton (by Vlad Losev); adds threading tests for SCOPED_TRACE() (by Vlad Losev); replaces native pthread calls with gtest's threading constructs (by Vlad Losev); fixes flakiness in CountedDestructor (by Vlad Losev); minor MSVC 7.1 clean-up (by Zhanyong Wan). --- samples/sample7_unittest.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'samples/sample7_unittest.cc') diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc index b5d507a9..f4552827 100644 --- a/samples/sample7_unittest.cc +++ b/samples/sample7_unittest.cc @@ -121,12 +121,12 @@ INSTANTIATE_TEST_CASE_P( #else -// Google Test doesn't support value-parameterized tests on some platforms -// and compilers, such as MSVC 7.1. If we use conditional compilation to -// compile out all code referring to the gtest_main library, MSVC linker -// will not link that library at all and consequently complain about -// missing entry point defined in that library (fatal error LNK1561: -// entry point must be defined). This dummy test keeps gtest_main linked in. +// Google Test may not support value-parameterized tests with some +// compilers. If we use conditional compilation to compile out all +// code referring to the gtest_main library, MSVC linker will not link +// that library at all and consequently complain about missing entry +// point defined in that library (fatal error LNK1561: entry point +// must be defined). This dummy test keeps gtest_main linked in. TEST(DummyTest, ValueParameterizedTestsAreNotSupportedOnThisPlatform) {} #endif // GTEST_HAS_PARAM_TEST -- cgit v1.2.3 From dac3e879c56a50696a36f53e1e5e353e48fa665f Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 14 Sep 2010 05:35:59 +0000 Subject: Include gtest headers as user headers instead of system headers. --- samples/sample7_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample7_unittest.cc') diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc index f4552827..441acf97 100644 --- a/samples/sample7_unittest.cc +++ b/samples/sample7_unittest.cc @@ -38,7 +38,7 @@ // The interface and its implementations are in this header. #include "prime_tables.h" -#include +#include "gtest/gtest.h" #if GTEST_HAS_PARAM_TEST -- cgit v1.2.3 From b6c141fe2ad9665e975dfcc9ebf238cac4e77242 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 17 Nov 2010 23:33:18 +0000 Subject: Fixes comments in sample7_unittest.cc. --- samples/sample7_unittest.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'samples/sample7_unittest.cc') diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc index 441acf97..1b651a21 100644 --- a/samples/sample7_unittest.cc +++ b/samples/sample7_unittest.cc @@ -45,12 +45,11 @@ using ::testing::TestWithParam; using ::testing::Values; -// As a general rule, tested objects should not be reused between tests. -// Also, their constructors and destructors of tested objects can have -// side effects. Thus you should create and destroy them for each test. -// In this sample we will define a simple factory function for PrimeTable -// objects. We will instantiate objects in test's SetUp() method and -// delete them in TearDown() method. +// As a general rule, to prevent a test from affecting the tests that come +// after it, you should create and destroy the tested objects for each test +// instead of reusing them. In this sample we will define a simple factory +// function for PrimeTable objects. We will instantiate objects in test's +// SetUp() method and delete them in TearDown() method. typedef PrimeTable* CreatePrimeTableFunc(); PrimeTable* CreateOnTheFlyPrimeTable() { @@ -62,11 +61,10 @@ PrimeTable* CreatePreCalculatedPrimeTable() { return new PreCalculatedPrimeTable(max_precalculated); } -// Inside the test body, fixture constructor, SetUp(), and TearDown() -// you can refer to the test parameter by GetParam(). -// In this case, the test parameter is a PrimeTableFactory interface pointer -// which we use in fixture's SetUp() to create and store an instance of -// PrimeTable. +// Inside the test body, fixture constructor, SetUp(), and TearDown() you +// can refer to the test parameter by GetParam(). In this case, the test +// parameter is a factory function which we call in fixture's SetUp() to +// create and store an instance of PrimeTable. class PrimeTableTest : public TestWithParam { public: virtual ~PrimeTableTest() { delete table_; } -- cgit v1.2.3