mirror of
https://github.com/google/googletest.git
synced 2025-11-10 15:05:24 -05:00
Initial import.
This commit is contained in:
68
samples/sample1.cc
Normal file
68
samples/sample1.cc
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// A sample program demonstrating using Google C++ testing framework.
|
||||
//
|
||||
// Author: wan@google.com (Zhanyong Wan)
|
||||
|
||||
#include "sample1.h"
|
||||
|
||||
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
|
||||
int Factorial(int n) {
|
||||
int result = 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns true iff n is a prime number.
|
||||
bool IsPrime(int n) {
|
||||
// Trivial case 1: small numbers
|
||||
if (n <= 1) return false;
|
||||
|
||||
// Trivial case 2: even numbers
|
||||
if (n % 2 == 0) return n == 2;
|
||||
|
||||
// Now, we have that n is odd and n >= 3.
|
||||
|
||||
// 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
|
||||
if (i > n/i) break;
|
||||
|
||||
// Now, we have i <= n/i < n.
|
||||
// If n is divisible by i, n is not prime.
|
||||
if (n % i == 0) return false;
|
||||
}
|
||||
|
||||
// n has no integer factor in the range (1, n), and thus is prime.
|
||||
return true;
|
||||
}
|
||||
43
samples/sample1.h
Normal file
43
samples/sample1.h
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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_
|
||||
|
||||
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
|
||||
int Factorial(int n);
|
||||
|
||||
// Returns true iff n is a prime number.
|
||||
bool IsPrime(int n);
|
||||
|
||||
#endif // GTEST_SAMPLES_SAMPLE1_H_
|
||||
153
samples/sample1_unittest.cc
Normal file
153
samples/sample1_unittest.cc
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
|
||||
|
||||
|
||||
// Step 1. Include necessary header files such that the stuff your
|
||||
// test logic needs is declared.
|
||||
//
|
||||
// Don't forget gtest.h, which declares the testing framework.
|
||||
|
||||
#include <limits.h>
|
||||
#include "sample1.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
// Step 2. Use the TEST macro to define your tests.
|
||||
//
|
||||
// TEST has two parameters: the test case name and the test name.
|
||||
// After using the macro, you should define your test logic between a
|
||||
// pair of braces. You can use a bunch of macros to indicate the
|
||||
// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
|
||||
// examples of such macros. For a complete list, see gtest.h.
|
||||
//
|
||||
// <TechnicalDetails>
|
||||
//
|
||||
// In Google Test, tests are grouped into test cases. This is how we
|
||||
// keep test code organized. You should put logically related tests
|
||||
// into the same test case.
|
||||
//
|
||||
// The test case name and the test name should both be valid C++
|
||||
// identifiers. And you should not use underscore (_) in the names.
|
||||
//
|
||||
// Google Test guarantees that each test you define is run exactly
|
||||
// once, but it makes no guarantee on the order the tests are
|
||||
// executed. Therefore, you should write your tests in such a way
|
||||
// that their results don't depend on their order.
|
||||
//
|
||||
// </TechnicalDetails>
|
||||
|
||||
|
||||
// Tests Factorial().
|
||||
|
||||
// Tests factorial of negative numbers.
|
||||
TEST(FactorialTest, Negative) {
|
||||
// This test is named "Negative", and belongs to the "FactorialTest"
|
||||
// test case.
|
||||
EXPECT_EQ(1, Factorial(-5));
|
||||
EXPECT_EQ(1, Factorial(-1));
|
||||
EXPECT_TRUE(Factorial(-10) > 0);
|
||||
|
||||
// <TechnicalDetails>
|
||||
//
|
||||
// EXPECT_EQ(expected, actual) is the same as
|
||||
//
|
||||
// EXPECT_TRUE((expected) == (actual))
|
||||
//
|
||||
// except that it will print both the expected value and the actual
|
||||
// value when the assertion fails. This is very helpful for
|
||||
// debugging. Therefore in this case EXPECT_EQ is preferred.
|
||||
//
|
||||
// On the other hand, EXPECT_TRUE accepts any Boolean expression,
|
||||
// and is thus more general.
|
||||
//
|
||||
// </TechnicalDetails>
|
||||
}
|
||||
|
||||
// Tests factorial of 0.
|
||||
TEST(FactorialTest, Zero) {
|
||||
EXPECT_EQ(1, Factorial(0));
|
||||
}
|
||||
|
||||
// Tests factorial of positive numbers.
|
||||
TEST(FactorialTest, Positive) {
|
||||
EXPECT_EQ(1, Factorial(1));
|
||||
EXPECT_EQ(2, Factorial(2));
|
||||
EXPECT_EQ(6, Factorial(3));
|
||||
EXPECT_EQ(40320, Factorial(8));
|
||||
}
|
||||
|
||||
|
||||
// Tests IsPrime()
|
||||
|
||||
// Tests negative input.
|
||||
TEST(IsPrimeTest, Negative) {
|
||||
// This test belongs to the IsPrimeTest test case.
|
||||
|
||||
EXPECT_FALSE(IsPrime(-1));
|
||||
EXPECT_FALSE(IsPrime(-2));
|
||||
EXPECT_FALSE(IsPrime(INT_MIN));
|
||||
}
|
||||
|
||||
// Tests some trivial cases.
|
||||
TEST(IsPrimeTest, Trivial) {
|
||||
EXPECT_FALSE(IsPrime(0));
|
||||
EXPECT_FALSE(IsPrime(1));
|
||||
EXPECT_TRUE(IsPrime(2));
|
||||
EXPECT_TRUE(IsPrime(3));
|
||||
}
|
||||
|
||||
// Tests positive input.
|
||||
TEST(IsPrimeTest, Positive) {
|
||||
EXPECT_FALSE(IsPrime(4));
|
||||
EXPECT_TRUE(IsPrime(5));
|
||||
EXPECT_FALSE(IsPrime(6));
|
||||
EXPECT_TRUE(IsPrime(23));
|
||||
}
|
||||
|
||||
// Step 3. Call RUN_ALL_TESTS() in main().
|
||||
//
|
||||
// We do this by linking in src/gtest_main.cc file, which consists of
|
||||
// a main() function which calls RUN_ALL_TESTS() for us.
|
||||
//
|
||||
// This runs all the tests you've defined, prints the result, and
|
||||
// returns 0 if successful, or 1 otherwise.
|
||||
//
|
||||
// Did you notice that we didn't register the tests? The
|
||||
// RUN_ALL_TESTS() macro magically knows about all the tests we
|
||||
// defined. Isn't this convenient?
|
||||
54
samples/sample2.cc
Normal file
54
samples/sample2.cc
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// A sample program demonstrating using Google C++ testing framework.
|
||||
//
|
||||
// Author: wan@google.com (Zhanyong Wan)
|
||||
|
||||
#include "sample2.h"
|
||||
|
||||
// Clones a 0-terminated C string, allocating memory using new.
|
||||
const char * MyString::CloneCString(const char * c_string) {
|
||||
if (c_string == NULL) return NULL;
|
||||
|
||||
const size_t len = strlen(c_string);
|
||||
char * const clone = new char[ len + 1 ];
|
||||
strcpy(clone, c_string);
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
// Sets the 0-terminated C string this MyString object
|
||||
// represents.
|
||||
void MyString::Set(const char * c_string) {
|
||||
// Makes sure this works when c_string == c_string_
|
||||
const char * const temp = MyString::CloneCString(c_string);
|
||||
delete[] c_string_;
|
||||
c_string_ = temp;
|
||||
}
|
||||
86
samples/sample2.h
Normal file
86
samples/sample2.h
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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_
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// A simple string class.
|
||||
class MyString {
|
||||
private:
|
||||
const char * c_string_;
|
||||
const MyString& operator=(const MyString& rhs);
|
||||
|
||||
public:
|
||||
|
||||
// Clones a 0-terminated C string, allocating memory using new.
|
||||
static const char * CloneCString(const char * c_string);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// C'tors
|
||||
|
||||
// The default c'tor constructs a NULL string.
|
||||
MyString() : c_string_(NULL) {}
|
||||
|
||||
// Constructs a MyString by cloning a 0-terminated C string.
|
||||
explicit MyString(const char * c_string) : c_string_(NULL) {
|
||||
Set(c_string);
|
||||
}
|
||||
|
||||
// Copy c'tor
|
||||
MyString(const MyString& string) : c_string_(NULL) {
|
||||
Set(string.c_string_);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// D'tor. MyString is intended to be a final class, so the d'tor
|
||||
// doesn't need to be virtual.
|
||||
~MyString() { delete[] c_string_; }
|
||||
|
||||
// Gets the 0-terminated C string this MyString object represents.
|
||||
const char * c_string() const { return c_string_; }
|
||||
|
||||
size_t Length() const {
|
||||
return c_string_ == NULL ? 0 : strlen(c_string_);
|
||||
}
|
||||
|
||||
// Sets the 0-terminated C string this MyString object represents.
|
||||
void Set(const char * c_string);
|
||||
};
|
||||
|
||||
|
||||
#endif // GTEST_SAMPLES_SAMPLE2_H_
|
||||
109
samples/sample2_unittest.cc
Normal file
109
samples/sample2_unittest.cc
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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.
|
||||
//
|
||||
// Usually, it's a good idea to have one test for each method in your
|
||||
// class. You don't have to do that exactly, but it helps to keep
|
||||
// your tests organized. You may also throw in additional tests as
|
||||
// needed.
|
||||
|
||||
#include "sample2.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
// In this example, we test the MyString class (a simple string).
|
||||
|
||||
// Tests the default c'tor.
|
||||
TEST(MyString, DefaultConstructor) {
|
||||
const MyString s;
|
||||
|
||||
// Asserts that s.c_string() returns NULL.
|
||||
//
|
||||
// <TechnicalDetails>
|
||||
//
|
||||
// If we write NULL instead of
|
||||
//
|
||||
// static_cast<const char *>(NULL)
|
||||
//
|
||||
// in this assertion, it will generate a warning on gcc 3.4. The
|
||||
// reason is that EXPECT_EQ needs to know the types of its
|
||||
// arguments in order to print them when it fails. Since NULL is
|
||||
// #defined as 0, the compiler will use the formatter function for
|
||||
// int to print it. However, gcc thinks that NULL should be used as
|
||||
// a pointer, not an int, and therefore complains.
|
||||
//
|
||||
// The root of the problem is C++'s lack of distinction between the
|
||||
// integer number 0 and the null pointer constant. Unfortunately,
|
||||
// we have to live with this fact.
|
||||
//
|
||||
// </TechnicalDetails>
|
||||
EXPECT_STREQ(NULL, s.c_string());
|
||||
|
||||
EXPECT_EQ(0, s.Length());
|
||||
}
|
||||
|
||||
const char kHelloString[] = "Hello, world!";
|
||||
|
||||
// Tests the c'tor that accepts a C string.
|
||||
TEST(MyString, ConstructorFromCString) {
|
||||
const MyString s(kHelloString);
|
||||
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
|
||||
EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
|
||||
s.Length());
|
||||
}
|
||||
|
||||
// Tests the copy c'tor.
|
||||
TEST(MyString, CopyConstructor) {
|
||||
const MyString s1(kHelloString);
|
||||
const MyString s2 = s1;
|
||||
EXPECT_TRUE(strcmp(s2.c_string(), kHelloString) == 0);
|
||||
}
|
||||
|
||||
// Tests the Set method.
|
||||
TEST(MyString, Set) {
|
||||
MyString s;
|
||||
|
||||
s.Set(kHelloString);
|
||||
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
|
||||
|
||||
// Set should work when the input pointer is the same as the one
|
||||
// already in the MyString object.
|
||||
s.Set(s.c_string());
|
||||
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
|
||||
|
||||
// Can we set the MyString to NULL?
|
||||
s.Set(NULL);
|
||||
EXPECT_STREQ(NULL, s.c_string());
|
||||
}
|
||||
173
samples/sample3-inl.h
Normal file
173
samples/sample3-inl.h
Normal file
@@ -0,0 +1,173 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
// Queue is a simple queue implemented as a singled-linked list.
|
||||
//
|
||||
// The element type must support copy constructor.
|
||||
template <typename E> // E is the element type
|
||||
class Queue;
|
||||
|
||||
// QueueNode is a node in a Queue, which consists of an element of
|
||||
// type E and a pointer to the next node.
|
||||
template <typename E> // E is the element type
|
||||
class QueueNode {
|
||||
friend class Queue<E>;
|
||||
|
||||
public:
|
||||
// Gets the element in this node.
|
||||
const E & element() const { return element_; }
|
||||
|
||||
// Gets the next node in the queue.
|
||||
QueueNode * next() { return next_; }
|
||||
const QueueNode * next() const { return next_; }
|
||||
|
||||
private:
|
||||
// Creates a node with a given element value. The next pointer is
|
||||
// set to NULL.
|
||||
QueueNode(const E & element) : element_(element), next_(NULL) {}
|
||||
|
||||
// We disable the default assignment operator and copy c'tor.
|
||||
const QueueNode & operator = (const QueueNode &);
|
||||
QueueNode(const QueueNode &);
|
||||
|
||||
E element_;
|
||||
QueueNode * next_;
|
||||
};
|
||||
|
||||
template <typename E> // E is the element type.
|
||||
class Queue {
|
||||
public:
|
||||
|
||||
// Creates an empty queue.
|
||||
Queue() : head_(NULL), last_(NULL), size_(0) {}
|
||||
|
||||
// D'tor. Clears the queue.
|
||||
~Queue() { Clear(); }
|
||||
|
||||
// Clears the queue.
|
||||
void Clear() {
|
||||
if (size_ > 0) {
|
||||
// 1. Deletes every node.
|
||||
QueueNode<E> * node = head_;
|
||||
QueueNode<E> * next = node->next();
|
||||
for (; ;) {
|
||||
delete node;
|
||||
node = next;
|
||||
if (node == NULL) break;
|
||||
next = node->next();
|
||||
}
|
||||
|
||||
// 2. Resets the member variables.
|
||||
head_ = last_ = NULL;
|
||||
size_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the number of elements.
|
||||
size_t Size() const { return size_; }
|
||||
|
||||
// Gets the first element of the queue, or NULL if the queue is empty.
|
||||
QueueNode<E> * Head() { return head_; }
|
||||
const QueueNode<E> * Head() const { return head_; }
|
||||
|
||||
// Gets the last element of the queue, or NULL if the queue is empty.
|
||||
QueueNode<E> * Last() { return last_; }
|
||||
const QueueNode<E> * Last() const { return last_; }
|
||||
|
||||
// Adds an element to the end of the queue. A copy of the element is
|
||||
// created using the copy constructor, and then stored in the queue.
|
||||
// Changes made to the element in the queue doesn't affect the source
|
||||
// object, and vice versa.
|
||||
void Enqueue(const E & element) {
|
||||
QueueNode<E> * new_node = new QueueNode<E>(element);
|
||||
|
||||
if (size_ == 0) {
|
||||
head_ = last_ = new_node;
|
||||
size_ = 1;
|
||||
} else {
|
||||
last_->next_ = new_node;
|
||||
last_ = new_node;
|
||||
size_++;
|
||||
}
|
||||
}
|
||||
|
||||
// Removes the head of the queue and returns it. Returns NULL if
|
||||
// the queue is empty.
|
||||
E * Dequeue() {
|
||||
if (size_ == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const QueueNode<E> * const old_head = head_;
|
||||
head_ = head_->next_;
|
||||
size_--;
|
||||
if (size_ == 0) {
|
||||
last_ = NULL;
|
||||
}
|
||||
|
||||
E * element = new E(old_head->element());
|
||||
delete old_head;
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
// Applies a function/functor on each element of the queue, and
|
||||
// returns the result in a new queue. The original queue is not
|
||||
// affected.
|
||||
template <typename F>
|
||||
Queue * Map(F function) const {
|
||||
Queue * new_queue = new Queue();
|
||||
for (const QueueNode<E> * node = head_; node != NULL; node = node->next_) {
|
||||
new_queue->Enqueue(function(node->element()));
|
||||
}
|
||||
|
||||
return new_queue;
|
||||
}
|
||||
|
||||
private:
|
||||
QueueNode<E> * head_; // The first node of the queue.
|
||||
QueueNode<E> * last_; // The last node of the queue.
|
||||
size_t size_; // The number of elements in the queue.
|
||||
|
||||
// We disallow copying a queue.
|
||||
Queue(const Queue &);
|
||||
const Queue & operator = (const Queue &);
|
||||
};
|
||||
|
||||
#endif // GTEST_SAMPLES_SAMPLE3_INL_H_
|
||||
151
samples/sample3_unittest.cc
Normal file
151
samples/sample3_unittest.cc
Normal file
@@ -0,0 +1,151 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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.
|
||||
//
|
||||
// A test fixture is a place to hold objects and functions shared by
|
||||
// all tests in a test case. Using a test fixture avoids duplicating
|
||||
// the test code necessary to initialize and cleanup those common
|
||||
// objects for each test. It is also useful for defining sub-routines
|
||||
// that your tests need to invoke a lot.
|
||||
//
|
||||
// <TechnicalDetails>
|
||||
//
|
||||
// The tests share the test fixture in the sense of code sharing, not
|
||||
// data sharing. Each test is given its own fresh copy of the
|
||||
// fixture. You cannot expect the data modified by one test to be
|
||||
// passed on to another test, which is a bad idea.
|
||||
//
|
||||
// The reason for this design is that tests should be independent and
|
||||
// repeatable. In particular, a test should not fail as the result of
|
||||
// another test's failure. If one test depends on info produced by
|
||||
// another test, then the two tests should really be one big test.
|
||||
//
|
||||
// The macros for indicating the success/failure of a test
|
||||
// (EXPECT_TRUE, FAIL, etc) need to know what the current test is
|
||||
// (when Google Test prints the test result, it tells you which test
|
||||
// each failure belongs to). Technically, these macros invoke a
|
||||
// member function of the Test class. Therefore, you cannot use them
|
||||
// in a global function. That's why you should put test sub-routines
|
||||
// in a test fixture.
|
||||
//
|
||||
// </TechnicalDetails>
|
||||
|
||||
#include "sample3-inl.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
// To use a test fixture, derive a class from testing::Test.
|
||||
class QueueTest : 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.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp() {
|
||||
q1_.Enqueue(1);
|
||||
q2_.Enqueue(2);
|
||||
q2_.Enqueue(3);
|
||||
}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
// virtual void TearDown() {
|
||||
// }
|
||||
|
||||
// A helper function that some test uses.
|
||||
static int Double(int n) {
|
||||
return 2*n;
|
||||
}
|
||||
|
||||
// A helper function for testing Queue::Map().
|
||||
void MapTester(const Queue<int> * q) {
|
||||
// Creates a new queue, where each element is twice as big as the
|
||||
// corresponding one in q.
|
||||
const Queue<int> * const new_q = q->Map(Double);
|
||||
|
||||
// Verifies that the new queue has the same size as q.
|
||||
ASSERT_EQ(q->Size(), new_q->Size());
|
||||
|
||||
// Verifies the relationship between the elements of the two queues.
|
||||
for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
|
||||
n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
|
||||
EXPECT_EQ(2 * n1->element(), n2->element());
|
||||
}
|
||||
|
||||
delete new_q;
|
||||
}
|
||||
|
||||
// Declares the variables your tests want to use.
|
||||
Queue<int> q0_;
|
||||
Queue<int> q1_;
|
||||
Queue<int> q2_;
|
||||
};
|
||||
|
||||
// When you have a test fixture, you define a test using TEST_F
|
||||
// instead of TEST.
|
||||
|
||||
// Tests the default c'tor.
|
||||
TEST_F(QueueTest, DefaultConstructor) {
|
||||
// You can access data in the test fixture here.
|
||||
EXPECT_EQ(0, q0_.Size());
|
||||
}
|
||||
|
||||
// Tests Dequeue().
|
||||
TEST_F(QueueTest, Dequeue) {
|
||||
int * n = q0_.Dequeue();
|
||||
EXPECT_TRUE(n == NULL);
|
||||
|
||||
n = q1_.Dequeue();
|
||||
ASSERT_TRUE(n != NULL);
|
||||
EXPECT_EQ(1, *n);
|
||||
EXPECT_EQ(0, q1_.Size());
|
||||
delete n;
|
||||
|
||||
n = q2_.Dequeue();
|
||||
ASSERT_TRUE(n != NULL);
|
||||
EXPECT_EQ(2, *n);
|
||||
EXPECT_EQ(1, q2_.Size());
|
||||
delete n;
|
||||
}
|
||||
|
||||
// Tests the Queue::Map() function.
|
||||
TEST_F(QueueTest, Map) {
|
||||
MapTester(&q0_);
|
||||
MapTester(&q1_);
|
||||
MapTester(&q2_);
|
||||
}
|
||||
46
samples/sample4.cc
Normal file
46
samples/sample4.cc
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// A sample program demonstrating using Google C++ testing framework.
|
||||
//
|
||||
// Author: wan@google.com (Zhanyong Wan)
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sample4.h"
|
||||
|
||||
// Returns the current counter value, and increments it.
|
||||
int Counter::Increment() {
|
||||
return counter_++;
|
||||
}
|
||||
|
||||
// Prints the current counter value to STDOUT.
|
||||
void Counter::Print() const {
|
||||
printf("%d", counter_);
|
||||
}
|
||||
53
samples/sample4.h
Normal file
53
samples/sample4.h
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2005, 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.
|
||||
|
||||
// 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_
|
||||
|
||||
// A simple monotonic counter.
|
||||
class Counter {
|
||||
private:
|
||||
int counter_;
|
||||
|
||||
public:
|
||||
// Creates a counter that starts at 0.
|
||||
Counter() : counter_(0) {}
|
||||
|
||||
// Returns the current counter value, and increments it.
|
||||
int Increment();
|
||||
|
||||
// Prints the current counter value to STDOUT.
|
||||
void Print() const;
|
||||
};
|
||||
|
||||
#endif // GTEST_SAMPLES_SAMPLE4_H_
|
||||
45
samples/sample4_unittest.cc
Normal file
45
samples/sample4_unittest.cc
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2005, 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: wan@google.com (Zhanyong Wan)
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "sample4.h"
|
||||
|
||||
// Tests the Increment() method.
|
||||
TEST(Counter, Increment) {
|
||||
Counter c;
|
||||
|
||||
// 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());
|
||||
}
|
||||
199
samples/sample5_unittest.cc
Normal file
199
samples/sample5_unittest.cc
Normal file
@@ -0,0 +1,199 @@
|
||||
// Copyright 2005, 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: wan@google.com (Zhanyong Wan)
|
||||
|
||||
// This sample teaches how to reuse a test fixture in multiple test
|
||||
// cases by deriving sub-fixtures from it.
|
||||
//
|
||||
// When you define a test fixture, you specify the name of the test
|
||||
// case that will use this fixture. Therefore, a test fixture can
|
||||
// be used by only one test case.
|
||||
//
|
||||
// Sometimes, more than one test cases may want to use the same or
|
||||
// slightly different test fixtures. For example, you may want to
|
||||
// make sure that all tests for a GUI library don't leak important
|
||||
// system resources like fonts and brushes. In Google Test, you do
|
||||
// this by putting the shared logic in a super (as in "super class")
|
||||
// test fixture, and then have each test case use a fixture derived
|
||||
// from this super fixture.
|
||||
|
||||
#include <limits.h>
|
||||
#include <time.h>
|
||||
#include "sample3-inl.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include "sample1.h"
|
||||
|
||||
// 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.
|
||||
//
|
||||
// We put the code for timing a test in a test fixture called
|
||||
// "QuickTest". QuickTest is intended to be the super fixture that
|
||||
// other fixtures derive from, therefore there is no test case with
|
||||
// the name "QuickTest". This is OK.
|
||||
//
|
||||
// Later, we will derive multiple test fixtures from QuickTest.
|
||||
class QuickTest : public testing::Test {
|
||||
protected:
|
||||
// Remember that SetUp() is run immediately before a test starts.
|
||||
// This is a good place to record the start time.
|
||||
virtual void SetUp() {
|
||||
start_time_ = time(NULL);
|
||||
}
|
||||
|
||||
// TearDown() is invoked immediately after a test finishes. Here we
|
||||
// check if the test was too slow.
|
||||
virtual void TearDown() {
|
||||
// Gets the time when the test finishes
|
||||
const time_t end_time = time(NULL);
|
||||
|
||||
// Asserts that the test took no more than ~5 seconds. Did you
|
||||
// know that you can use assertions in SetUp() and TearDown() as
|
||||
// well?
|
||||
EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
|
||||
}
|
||||
|
||||
// The UTC time (in seconds) when the test starts
|
||||
time_t start_time_;
|
||||
};
|
||||
|
||||
|
||||
// We derive a fixture named IntegerFunctionTest from the QuickTest
|
||||
// fixture. All tests using this fixture will be automatically
|
||||
// required to be quick.
|
||||
class IntegerFunctionTest : public QuickTest {
|
||||
// We don't need any more logic than already in the QuickTest fixture.
|
||||
// Therefore the body is empty.
|
||||
};
|
||||
|
||||
|
||||
// Now we can write tests in the IntegerFunctionTest test case.
|
||||
|
||||
// Tests Factorial()
|
||||
TEST_F(IntegerFunctionTest, Factorial) {
|
||||
// Tests factorial of negative numbers.
|
||||
EXPECT_EQ(1, Factorial(-5));
|
||||
EXPECT_EQ(1, Factorial(-1));
|
||||
EXPECT_TRUE(Factorial(-10) > 0);
|
||||
|
||||
// Tests factorial of 0.
|
||||
EXPECT_EQ(1, Factorial(0));
|
||||
|
||||
// Tests factorial of positive numbers.
|
||||
EXPECT_EQ(1, Factorial(1));
|
||||
EXPECT_EQ(2, Factorial(2));
|
||||
EXPECT_EQ(6, Factorial(3));
|
||||
EXPECT_EQ(40320, Factorial(8));
|
||||
}
|
||||
|
||||
|
||||
// Tests IsPrime()
|
||||
TEST_F(IntegerFunctionTest, IsPrime) {
|
||||
// Tests negative input.
|
||||
EXPECT_TRUE(!IsPrime(-1));
|
||||
EXPECT_TRUE(!IsPrime(-2));
|
||||
EXPECT_TRUE(!IsPrime(INT_MIN));
|
||||
|
||||
// Tests some trivial cases.
|
||||
EXPECT_TRUE(!IsPrime(0));
|
||||
EXPECT_TRUE(!IsPrime(1));
|
||||
EXPECT_TRUE(IsPrime(2));
|
||||
EXPECT_TRUE(IsPrime(3));
|
||||
|
||||
// Tests positive input.
|
||||
EXPECT_TRUE(!IsPrime(4));
|
||||
EXPECT_TRUE(IsPrime(5));
|
||||
EXPECT_TRUE(!IsPrime(6));
|
||||
EXPECT_TRUE(IsPrime(23));
|
||||
}
|
||||
|
||||
|
||||
// The next test case (named "QueueTest") also needs to be quick, so
|
||||
// we derive another fixture from QuickTest.
|
||||
//
|
||||
// The QueueTest test fixture has some logic and shared objects in
|
||||
// addition to what's in QuickTest already. We define the additional
|
||||
// stuff inside the body of the test fixture, as usual.
|
||||
class QueueTest : public QuickTest {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
// First, we need to set up the super fixture (QuickTest).
|
||||
QuickTest::SetUp();
|
||||
|
||||
// Second, some additional setup for this fixture.
|
||||
q1_.Enqueue(1);
|
||||
q2_.Enqueue(2);
|
||||
q2_.Enqueue(3);
|
||||
}
|
||||
|
||||
// By default, TearDown() inherits the behavior of
|
||||
// QuickTest::TearDown(). As we have no additional cleaning work
|
||||
// for QueueTest, we omit it here.
|
||||
//
|
||||
// virtual void TearDown() {
|
||||
// QuickTest::TearDown();
|
||||
// }
|
||||
|
||||
Queue<int> q0_;
|
||||
Queue<int> q1_;
|
||||
Queue<int> q2_;
|
||||
};
|
||||
|
||||
|
||||
// Now, let's write tests using the QueueTest fixture.
|
||||
|
||||
// Tests the default constructor.
|
||||
TEST_F(QueueTest, DefaultConstructor) {
|
||||
EXPECT_EQ(0, q0_.Size());
|
||||
}
|
||||
|
||||
// Tests Dequeue().
|
||||
TEST_F(QueueTest, Dequeue) {
|
||||
int * n = q0_.Dequeue();
|
||||
EXPECT_TRUE(n == NULL);
|
||||
|
||||
n = q1_.Dequeue();
|
||||
EXPECT_TRUE(n != NULL);
|
||||
EXPECT_EQ(1, *n);
|
||||
EXPECT_EQ(0, q1_.Size());
|
||||
delete n;
|
||||
|
||||
n = q2_.Dequeue();
|
||||
EXPECT_TRUE(n != NULL);
|
||||
EXPECT_EQ(2, *n);
|
||||
EXPECT_EQ(1, q2_.Size());
|
||||
delete n;
|
||||
}
|
||||
|
||||
// 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
|
||||
// can be. In practice, however, you probably don't want it to be too
|
||||
// deep as to be confusing.
|
||||
Reference in New Issue
Block a user