aboutsummaryrefslogtreecommitdiffstats
path: root/googletest/samples
diff options
context:
space:
mode:
authorGennadiy Civil <gennadiycivil@users.noreply.github.com>2018-08-08 12:19:04 -0700
committerGitHub <noreply@github.com>2018-08-08 12:19:04 -0700
commit11f5a2740aa8a55d620fba6957e53ad08721cf54 (patch)
tree04f982e6158e57f68529ca47e37ec377574647dd /googletest/samples
parent008e54c1dd407b6edd680fccf78cd194365e0507 (diff)
parent66bd580b5bc054522f7fab13463a47aa156a2d4b (diff)
downloadgoogletest-11f5a2740aa8a55d620fba6957e53ad08721cf54.tar.gz
googletest-11f5a2740aa8a55d620fba6957e53ad08721cf54.tar.bz2
googletest-11f5a2740aa8a55d620fba6957e53ad08721cf54.zip
Merge branch 'master' into cross-testing-patch-1
Diffstat (limited to 'googletest/samples')
-rw-r--r--googletest/samples/prime_tables.h8
-rw-r--r--googletest/samples/sample1.cc2
-rw-r--r--googletest/samples/sample10_unittest.cc4
-rw-r--r--googletest/samples/sample1_unittest.cc3
-rw-r--r--googletest/samples/sample2_unittest.cc3
-rw-r--r--googletest/samples/sample3_unittest.cc13
-rw-r--r--googletest/samples/sample4_unittest.cc6
-rw-r--r--googletest/samples/sample5_unittest.cc6
-rw-r--r--googletest/samples/sample6_unittest.cc3
-rw-r--r--googletest/samples/sample7_unittest.cc32
-rw-r--r--googletest/samples/sample8_unittest.cc3
-rw-r--r--googletest/samples/sample9_unittest.cc3
12 files changed, 40 insertions, 46 deletions
diff --git a/googletest/samples/prime_tables.h b/googletest/samples/prime_tables.h
index 92ce16a0..55a3b44e 100644
--- a/googletest/samples/prime_tables.h
+++ b/googletest/samples/prime_tables.h
@@ -103,11 +103,15 @@ class PreCalculatedPrimeTable : public PrimeTable {
::std::fill(is_prime_, is_prime_ + is_prime_size_, true);
is_prime_[0] = is_prime_[1] = false;
- for (int i = 2; i <= max; i++) {
+ // Checks every candidate for prime number (we know that 2 is the only even
+ // prime).
+ for (int i = 2; i*i <= max; i += i%2+1) {
if (!is_prime_[i]) continue;
// Marks all multiples of i (except i itself) as non-prime.
- for (int j = 2*i; j <= max; j += i) {
+ // We are starting here from i-th multiplier, because all smaller
+ // complex numbers were already marked.
+ for (int j = i*i; j <= max; j += i) {
is_prime_[j] = false;
}
}
diff --git a/googletest/samples/sample1.cc b/googletest/samples/sample1.cc
index f171e260..7c08b28f 100644
--- a/googletest/samples/sample1.cc
+++ b/googletest/samples/sample1.cc
@@ -55,7 +55,7 @@ bool IsPrime(int n) {
// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
- // We only have to try i up to the squre root of n
+ // We only have to try i up to the square root of n
if (i > n/i) break;
// Now, we have i <= n/i < n.
diff --git a/googletest/samples/sample10_unittest.cc b/googletest/samples/sample10_unittest.cc
index 0051cd5d..10aa7620 100644
--- a/googletest/samples/sample10_unittest.cc
+++ b/googletest/samples/sample10_unittest.cc
@@ -35,18 +35,15 @@
#include <stdlib.h>
#include "gtest/gtest.h"
-
using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
-using ::testing::TestCase;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
namespace {
-
// We will track memory used by this class.
class Water {
public:
@@ -106,7 +103,6 @@ TEST(ListenersTest, LeaksWater) {
Water* water = new Water;
EXPECT_TRUE(water != NULL);
}
-
} // namespace
int main(int argc, char **argv) {
diff --git a/googletest/samples/sample1_unittest.cc b/googletest/samples/sample1_unittest.cc
index aefc4f1d..8376bb43 100644
--- a/googletest/samples/sample1_unittest.cc
+++ b/googletest/samples/sample1_unittest.cc
@@ -46,7 +46,7 @@
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
-
+namespace {
// Step 2. Use the TEST macro to define your tests.
//
@@ -139,6 +139,7 @@ TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
+} // namespace
// Step 3. Call RUN_ALL_TESTS() in main().
//
diff --git a/googletest/samples/sample2_unittest.cc b/googletest/samples/sample2_unittest.cc
index 4fa19b71..df522da5 100644
--- a/googletest/samples/sample2_unittest.cc
+++ b/googletest/samples/sample2_unittest.cc
@@ -42,7 +42,7 @@
#include "sample2.h"
#include "gtest/gtest.h"
-
+namespace {
// In this example, we test the MyString class (a simple string).
// Tests the default c'tor.
@@ -107,3 +107,4 @@ TEST(MyString, Set) {
s.Set(NULL);
EXPECT_STREQ(NULL, s.c_string());
}
+} // namespace
diff --git a/googletest/samples/sample3_unittest.cc b/googletest/samples/sample3_unittest.cc
index bf3877d0..7f51fd89 100644
--- a/googletest/samples/sample3_unittest.cc
+++ b/googletest/samples/sample3_unittest.cc
@@ -65,14 +65,14 @@
#include "sample3-inl.h"
#include "gtest/gtest.h"
-
+namespace {
// To use a test fixture, derive a class from testing::Test.
-class QueueTest : public testing::Test {
+class QueueTestSmpl3 : public testing::Test {
protected: // You should make the members protected s.t. they can be
// accessed from sub-classes.
// virtual void SetUp() will be called before each test is run. You
- // should define it if you need to initialize the varaibles.
+ // should define it if you need to initialize the variables.
// Otherwise, this can be skipped.
virtual void SetUp() {
q1_.Enqueue(1);
@@ -120,13 +120,13 @@ class QueueTest : public testing::Test {
// instead of TEST.
// Tests the default c'tor.
-TEST_F(QueueTest, DefaultConstructor) {
+TEST_F(QueueTestSmpl3, DefaultConstructor) {
// You can access data in the test fixture here.
EXPECT_EQ(0u, q0_.Size());
}
// Tests Dequeue().
-TEST_F(QueueTest, Dequeue) {
+TEST_F(QueueTestSmpl3, Dequeue) {
int * n = q0_.Dequeue();
EXPECT_TRUE(n == NULL);
@@ -144,8 +144,9 @@ TEST_F(QueueTest, Dequeue) {
}
// Tests the Queue::Map() function.
-TEST_F(QueueTest, Map) {
+TEST_F(QueueTestSmpl3, Map) {
MapTester(&q0_);
MapTester(&q1_);
MapTester(&q2_);
}
+} // namespace
diff --git a/googletest/samples/sample4_unittest.cc b/googletest/samples/sample4_unittest.cc
index fa5afc7d..7bf9ea34 100644
--- a/googletest/samples/sample4_unittest.cc
+++ b/googletest/samples/sample4_unittest.cc
@@ -29,10 +29,12 @@
//
// Author: wan@google.com (Zhanyong Wan)
-#include "gtest/gtest.h"
#include "sample4.h"
+#include "gtest/gtest.h"
+namespace {
// Tests the Increment() method.
+
TEST(Counter, Increment) {
Counter c;
@@ -43,3 +45,5 @@ TEST(Counter, Increment) {
EXPECT_EQ(1, c.Increment());
EXPECT_EQ(2, c.Increment());
}
+
+} // namespace
diff --git a/googletest/samples/sample5_unittest.cc b/googletest/samples/sample5_unittest.cc
index 43d8e577..401a58a5 100644
--- a/googletest/samples/sample5_unittest.cc
+++ b/googletest/samples/sample5_unittest.cc
@@ -46,10 +46,10 @@
#include <limits.h>
#include <time.h>
-#include "sample3-inl.h"
#include "gtest/gtest.h"
#include "sample1.h"
-
+#include "sample3-inl.h"
+namespace {
// In this sample, we want to ensure that every test finishes within
// ~5 seconds. If a test takes longer to run, we consider it a
// failure.
@@ -191,7 +191,7 @@ TEST_F(QueueTest, Dequeue) {
EXPECT_EQ(1u, q2_.Size());
delete n;
}
-
+} // namespace
// If necessary, you can derive further test fixtures from a derived
// fixture itself. For example, you can derive another fixture from
// QueueTest. Google Test imposes no limit on how deep the hierarchy
diff --git a/googletest/samples/sample6_unittest.cc b/googletest/samples/sample6_unittest.cc
index 8f2036a5..1faf0c3d 100644
--- a/googletest/samples/sample6_unittest.cc
+++ b/googletest/samples/sample6_unittest.cc
@@ -36,7 +36,7 @@
#include "prime_tables.h"
#include "gtest/gtest.h"
-
+namespace {
// First, we define some factory functions for creating instances of
// the implementations. You may be able to skip this step if all your
// implementations can be constructed the same way.
@@ -222,3 +222,4 @@ INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name
PrimeTableImplementations); // Type list
#endif // GTEST_HAS_TYPED_TEST_P
+} // namespace
diff --git a/googletest/samples/sample7_unittest.cc b/googletest/samples/sample7_unittest.cc
index 1b651a21..efa97289 100644
--- a/googletest/samples/sample7_unittest.cc
+++ b/googletest/samples/sample7_unittest.cc
@@ -39,8 +39,7 @@
#include "prime_tables.h"
#include "gtest/gtest.h"
-
-#if GTEST_HAS_PARAM_TEST
+namespace {
using ::testing::TestWithParam;
using ::testing::Values;
@@ -65,9 +64,9 @@ PrimeTable* CreatePreCalculatedPrimeTable() {
// 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<CreatePrimeTableFunc*> {
+class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
public:
- virtual ~PrimeTableTest() { delete table_; }
+ virtual ~PrimeTableTestSmpl7() { delete table_; }
virtual void SetUp() { table_ = (*GetParam())(); }
virtual void TearDown() {
delete table_;
@@ -78,7 +77,7 @@ class PrimeTableTest : public TestWithParam<CreatePrimeTableFunc*> {
PrimeTable* table_;
};
-TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
+TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
EXPECT_FALSE(table_->IsPrime(-5));
EXPECT_FALSE(table_->IsPrime(0));
EXPECT_FALSE(table_->IsPrime(1));
@@ -87,7 +86,7 @@ TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
EXPECT_FALSE(table_->IsPrime(100));
}
-TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
+TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
EXPECT_TRUE(table_->IsPrime(2));
EXPECT_TRUE(table_->IsPrime(3));
EXPECT_TRUE(table_->IsPrime(5));
@@ -96,7 +95,7 @@ TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
EXPECT_TRUE(table_->IsPrime(131));
}
-TEST_P(PrimeTableTest, CanGetNextPrime) {
+TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
EXPECT_EQ(2, table_->GetNextPrime(0));
EXPECT_EQ(3, table_->GetNextPrime(2));
EXPECT_EQ(5, table_->GetNextPrime(3));
@@ -112,19 +111,8 @@ TEST_P(PrimeTableTest, CanGetNextPrime) {
//
// 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 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) {}
+INSTANTIATE_TEST_CASE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
+ Values(&CreateOnTheFlyPrimeTable,
+ &CreatePreCalculatedPrimeTable<1000>));
-#endif // GTEST_HAS_PARAM_TEST
+} // namespace
diff --git a/googletest/samples/sample8_unittest.cc b/googletest/samples/sample8_unittest.cc
index 72743340..b0ff2d1f 100644
--- a/googletest/samples/sample8_unittest.cc
+++ b/googletest/samples/sample8_unittest.cc
@@ -37,7 +37,7 @@
#include "prime_tables.h"
#include "gtest/gtest.h"
-
+namespace {
#if GTEST_HAS_COMBINE
// Suppose we want to introduce a new, improved implementation of PrimeTable
@@ -171,3 +171,4 @@ INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
#endif // GTEST_HAS_COMBINE
+} // namespace
diff --git a/googletest/samples/sample9_unittest.cc b/googletest/samples/sample9_unittest.cc
index b2e2079b..75584bb0 100644
--- a/googletest/samples/sample9_unittest.cc
+++ b/googletest/samples/sample9_unittest.cc
@@ -44,9 +44,7 @@ using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
-
namespace {
-
// Provides alternative output mode which produces minimal amount of
// information about tests.
class TersePrinter : public EmptyTestEventListener {
@@ -102,7 +100,6 @@ TEST(CustomOutputTest, Fails) {
EXPECT_EQ(1, 2)
<< "This test fails in order to demonstrate alternative failure messages";
}
-
} // namespace
int main(int argc, char **argv) {