summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2018-11-15 16:11:41 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2018-11-15 16:32:35 +0100
commitd94f158aa6a1faa0f0825f333769617dd1269e9f (patch)
tree80954989318559133fd444ee7ee3be78cff90887 /samples
parent42552c3316326dfd84e8285672b33b78a1a6a4ee (diff)
Google C++ Testing Framework 1.8.1HEADmaster
Diffstat (limited to 'samples')
-rw-r--r--samples/prime_tables.h13
-rw-r--r--samples/sample1.cc4
-rw-r--r--samples/sample1.h2
-rw-r--r--samples/sample10_unittest.cc7
-rw-r--r--samples/sample1_unittest.cc6
-rw-r--r--samples/sample2.cc2
-rw-r--r--samples/sample2.h2
-rw-r--r--samples/sample2_unittest.cc6
-rw-r--r--samples/sample3-inl.h2
-rw-r--r--samples/sample3_unittest.cc16
-rw-r--r--samples/sample4.cc12
-rw-r--r--samples/sample4.h6
-rw-r--r--samples/sample4_unittest.cc14
-rw-r--r--samples/sample5_unittest.cc9
-rw-r--r--samples/sample6_unittest.cc6
-rw-r--r--samples/sample7_unittest.cc35
-rw-r--r--samples/sample8_unittest.cc6
-rw-r--r--samples/sample9_unittest.cc6
18 files changed, 67 insertions, 87 deletions
diff --git a/samples/prime_tables.h b/samples/prime_tables.h
index 92ce16a..523c50b 100644
--- a/samples/prime_tables.h
+++ b/samples/prime_tables.h
@@ -26,9 +26,8 @@
// 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: wan@google.com (Zhanyong Wan)
-// Author: vladl@google.com (Vlad Losev)
+
+
// This provides interface PrimeTable that determines whether a number is a
// prime and determines a next prime number. This interface is used
@@ -103,11 +102,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/samples/sample1.cc b/samples/sample1.cc
index f171e26..13cec1d 100644
--- a/samples/sample1.cc
+++ b/samples/sample1.cc
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#include "sample1.h"
@@ -55,7 +53,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/samples/sample1.h b/samples/sample1.h
index 3dfeb98..2c3e9f0 100644
--- a/samples/sample1.h
+++ b/samples/sample1.h
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_
diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc
index 0051cd5..7ce9550 100644
--- a/samples/sample10_unittest.cc
+++ b/samples/sample10_unittest.cc
@@ -25,8 +25,7 @@
// 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 use Google Test listener API to implement
// a primitive leak checker.
@@ -35,18 +34,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 +102,6 @@ TEST(ListenersTest, LeaksWater) {
Water* water = new Water;
EXPECT_TRUE(water != NULL);
}
-
} // namespace
int main(int argc, char **argv) {
diff --git a/samples/sample1_unittest.cc b/samples/sample1_unittest.cc
index aefc4f1..cb08b61 100644
--- a/samples/sample1_unittest.cc
+++ b/samples/sample1_unittest.cc
@@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
// This sample shows how to write a simple unit test for a function,
// using Google C++ testing framework.
@@ -46,7 +43,7 @@
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
-
+namespace {
// Step 2. Use the TEST macro to define your tests.
//
@@ -139,6 +136,7 @@ TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
}
+} // namespace
// Step 3. Call RUN_ALL_TESTS() in main().
//
diff --git a/samples/sample2.cc b/samples/sample2.cc
index 5f763b9..f3b722f 100644
--- a/samples/sample2.cc
+++ b/samples/sample2.cc
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#include "sample2.h"
diff --git a/samples/sample2.h b/samples/sample2.h
index cb485c7..58f360f 100644
--- a/samples/sample2.h
+++ b/samples/sample2.h
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#ifndef GTEST_SAMPLES_SAMPLE2_H_
#define GTEST_SAMPLES_SAMPLE2_H_
diff --git a/samples/sample2_unittest.cc b/samples/sample2_unittest.cc
index 4fa19b7..0848826 100644
--- a/samples/sample2_unittest.cc
+++ b/samples/sample2_unittest.cc
@@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
// This sample shows how to write a more complex unit test for a class
// that has multiple member functions.
@@ -42,7 +39,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 +104,4 @@ TEST(MyString, Set) {
s.Set(NULL);
EXPECT_STREQ(NULL, s.c_string());
}
+} // namespace
diff --git a/samples/sample3-inl.h b/samples/sample3-inl.h
index 7e3084d..1a29ce9 100644
--- a/samples/sample3-inl.h
+++ b/samples/sample3-inl.h
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
#define GTEST_SAMPLES_SAMPLE3_INL_H_
diff --git a/samples/sample3_unittest.cc b/samples/sample3_unittest.cc
index bf3877d..e093c25 100644
--- a/samples/sample3_unittest.cc
+++ b/samples/sample3_unittest.cc
@@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
// In this example, we use a more advanced feature of Google Test called
// test fixture.
@@ -65,14 +62,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 +117,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 +141,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/samples/sample4.cc b/samples/sample4.cc
index ae44bda..b0ee609 100644
--- a/samples/sample4.cc
+++ b/samples/sample4.cc
@@ -28,8 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
#include <stdio.h>
@@ -40,6 +38,16 @@ int Counter::Increment() {
return counter_++;
}
+// Returns the current counter value, and decrements it.
+// counter can not be less than 0, return 0 in this case
+int Counter::Decrement() {
+ if (counter_ == 0) {
+ return counter_;
+ } else {
+ return counter_--;
+ }
+}
+
// Prints the current counter value to STDOUT.
void Counter::Print() const {
printf("%d", counter_);
diff --git a/samples/sample4.h b/samples/sample4.h
index cd60f0d..e256f40 100644
--- a/samples/sample4.h
+++ b/samples/sample4.h
@@ -28,9 +28,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
#ifndef GTEST_SAMPLES_SAMPLE4_H_
#define GTEST_SAMPLES_SAMPLE4_H_
@@ -46,6 +43,9 @@ class Counter {
// Returns the current counter value, and increments it.
int Increment();
+ // Returns the current counter value, and decrements it.
+ int Decrement();
+
// Prints the current counter value to STDOUT.
void Print() const;
};
diff --git a/samples/sample4_unittest.cc b/samples/sample4_unittest.cc
index fa5afc7..d5144c0 100644
--- a/samples/sample4_unittest.cc
+++ b/samples/sample4_unittest.cc
@@ -26,20 +26,28 @@
// 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: 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;
+ // Test that counter 0 returns 0
+ EXPECT_EQ(0, c.Decrement());
+
// EXPECT_EQ() evaluates its arguments exactly once, so they
// can have side effects.
EXPECT_EQ(0, c.Increment());
EXPECT_EQ(1, c.Increment());
EXPECT_EQ(2, c.Increment());
+
+ EXPECT_EQ(3, c.Decrement());
}
+
+} // namespace
diff --git a/samples/sample5_unittest.cc b/samples/sample5_unittest.cc
index 43d8e57..d8a8788 100644
--- a/samples/sample5_unittest.cc
+++ b/samples/sample5_unittest.cc
@@ -26,8 +26,7 @@
// 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: wan@google.com (Zhanyong Wan)
+
// This sample teaches how to reuse a test fixture in multiple test
// cases by deriving sub-fixtures from it.
@@ -46,10 +45,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 +190,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/samples/sample6_unittest.cc b/samples/sample6_unittest.cc
index 8f2036a..ddf2f1c 100644
--- a/samples/sample6_unittest.cc
+++ b/samples/sample6_unittest.cc
@@ -26,8 +26,7 @@
// 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: wan@google.com (Zhanyong Wan)
+
// This sample shows how to test common properties of multiple
// implementations of the same interface (aka interface tests).
@@ -36,7 +35,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 +221,4 @@ INSTANTIATE_TYPED_TEST_CASE_P(OnTheFlyAndPreCalculated, // Instance name
PrimeTableImplementations); // Type list
#endif // GTEST_HAS_TYPED_TEST_P
+} // namespace
diff --git a/samples/sample7_unittest.cc b/samples/sample7_unittest.cc
index 1b651a2..c1ae8bd 100644
--- a/samples/sample7_unittest.cc
+++ b/samples/sample7_unittest.cc
@@ -26,8 +26,7 @@
// 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
@@ -39,8 +38,7 @@
#include "prime_tables.h"
#include "gtest/gtest.h"
-
-#if GTEST_HAS_PARAM_TEST
+namespace {
using ::testing::TestWithParam;
using ::testing::Values;
@@ -65,9 +63,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 +76,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 +85,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 +94,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 +110,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/samples/sample8_unittest.cc b/samples/sample8_unittest.cc
index 7274334..ce75cf0 100644
--- a/samples/sample8_unittest.cc
+++ b/samples/sample8_unittest.cc
@@ -26,8 +26,7 @@
// 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 code relying on some global flag variables.
// Combine() helps with generating all possible combinations of such flags,
@@ -37,7 +36,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 +170,4 @@ INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters,
TEST(DummyTest, CombineIsNotSupportedOnThisPlatform) {}
#endif // GTEST_HAS_COMBINE
+} // namespace
diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc
index b2e2079..53f9af5 100644
--- a/samples/sample9_unittest.cc
+++ b/samples/sample9_unittest.cc
@@ -25,8 +25,7 @@
// 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 use Google Test listener API to implement
// an alternative console output and how to use the UnitTest reflection API
@@ -44,9 +43,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 +99,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) {