diff options
author | vladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925> | 2008-11-20 01:40:35 (GMT) |
---|---|---|
committer | vladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925> | 2008-11-20 01:40:35 (GMT) |
commit | 3d7042176307f0d7700a3640f3b3bcc8790b8fcd (patch) | |
tree | ec4a9020570acc6d09366e5b305b9d162c1a6026 /samples/sample6_unittest.cc | |
parent | b6a296d0f7caff7140f422e49f5398c9ef17504d (diff) | |
download | googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.zip googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.tar.gz googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.tar.bz2 |
Value-parameterized tests and many bugfixes
Diffstat (limited to 'samples/sample6_unittest.cc')
-rw-r--r-- | samples/sample6_unittest.cc | 93 |
1 files changed, 8 insertions, 85 deletions
diff --git a/samples/sample6_unittest.cc b/samples/sample6_unittest.cc index dba52f0..3616672 100644 --- a/samples/sample6_unittest.cc +++ b/samples/sample6_unittest.cc @@ -30,91 +30,12 @@ // Author: wan@google.com (Zhanyong Wan) // This sample shows how to test common properties of multiple -// implementations of the same interface (aka interface tests). We -// put the code to be tested and the tests in the same file for -// simplicity. +// implementations of the same interface (aka interface tests). -#include <vector> -#include <gtest/gtest.h> - -// Section 1. the interface and its implementations. - -// The prime table interface. -class PrimeTable { - public: - virtual ~PrimeTable() {} - - // Returns true iff n is a prime number. - virtual bool IsPrime(int n) const = 0; - - // Returns the smallest prime number greater than p; or returns -1 - // if the next prime is beyond the capacity of the table. - virtual int GetNextPrime(int p) const = 0; -}; +// The interface and its implementations are in this header. +#include "prime_tables.h" -// Implementation #1 calculates the primes on-the-fly. -class OnTheFlyPrimeTable : public PrimeTable { - public: - virtual bool IsPrime(int n) const { - if (n <= 1) return false; - - for (int i = 2; i*i <= n; i++) { - // n is divisible by an integer other than 1 and itself. - if ((n % i) == 0) return false; - } - - return true; - } - - virtual int GetNextPrime(int p) const { - for (int n = p + 1; n > 0; n++) { - if (IsPrime(n)) return n; - } - - return -1; - } -}; - -// Implementation #2 pre-calculates the primes and stores the result -// in a vector. -class PreCalculatedPrimeTable : public PrimeTable { - public: - // 'max' specifies the maximum number the prime table holds. - explicit PreCalculatedPrimeTable(int max) : is_prime_(max + 1) { - CalculatePrimesUpTo(max); - } - - virtual bool IsPrime(int n) const { - return 0 <= n && n < is_prime_.size() && is_prime_[n]; - } - - virtual int GetNextPrime(int p) const { - for (int n = p + 1; n < is_prime_.size(); n++) { - if (is_prime_[n]) return n; - } - - return -1; - } - - private: - void CalculatePrimesUpTo(int max) { - fill(is_prime_.begin(), is_prime_.end(), true); - is_prime_[0] = is_prime_[1] = false; - - for (int i = 2; i <= max; i++) { - 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) { - is_prime_[j] = false; - } - } - } - - std::vector<bool> is_prime_; -}; - -// Sections 2. the tests. +#include <gtest/gtest.h> // First, we define some factory functions for creating instances of // the implementations. You may be able to skip this step if all your @@ -153,10 +74,10 @@ class PrimeTableTest : public testing::Test { PrimeTable* const table_; }; -using testing::Types; - #ifdef GTEST_HAS_TYPED_TEST +using testing::Types; + // Google Test offers two ways for reusing tests for different types. // The first is called "typed tests". You should use it if you // already know *all* the types you are gonna exercise when you write @@ -218,6 +139,8 @@ TYPED_TEST(PrimeTableTest, CanGetNextPrime) { #ifdef GTEST_HAS_TYPED_TEST_P +using testing::Types; + // Sometimes, however, you don't yet know all the types that you want // to test when you write the tests. For example, if you are the // author of an interface and expect other people to implement it, you |