1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2005, Google Inc.
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A sample program demonstrating using Google C++ testing framework.
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: wan@google.com (Zhanyong Wan)
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This sample shows how to write a simple unit test for a function,
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// using Google C++ testing framework.
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Step 1. Include necessary header files such that the stuff your
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// test logic needs is declared.
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Don't forget gtest.h, which declares the testing framework.
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <limits.h>
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include "sample1.h"
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/gtest.h>
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Step 2. Use the TEST macro to define your tests.
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// TEST has two parameters: the test case name and the test name.
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// After using the macro, you should define your test logic between a
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// pair of braces.  You can use a bunch of macros to indicate the
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// examples of such macros.  For a complete list, see gtest.h.
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// <TechnicalDetails>
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// In Google Test, tests are grouped into test cases.  This is how we
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// keep test code organized.  You should put logically related tests
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// into the same test case.
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The test case name and the test name should both be valid C++
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// identifiers.  And you should not use underscore (_) in the names.
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test guarantees that each test you define is run exactly
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// once, but it makes no guarantee on the order the tests are
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// executed.  Therefore, you should write your tests in such a way
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// that their results don't depend on their order.
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// </TechnicalDetails>
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests Factorial().
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests factorial of negative numbers.
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(FactorialTest, Negative) {
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This test is named "Negative", and belongs to the "FactorialTest"
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // test case.
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(1, Factorial(-5));
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(1, Factorial(-1));
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_TRUE(Factorial(-10) > 0);
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // <TechnicalDetails>
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // EXPECT_EQ(expected, actual) is the same as
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   EXPECT_TRUE((expected) == (actual))
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // except that it will print both the expected value and the actual
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // value when the assertion fails.  This is very helpful for
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // debugging.  Therefore in this case EXPECT_EQ is preferred.
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On the other hand, EXPECT_TRUE accepts any Boolean expression,
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // and is thus more general.
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // </TechnicalDetails>
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests factorial of 0.
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(FactorialTest, Zero) {
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(1, Factorial(0));
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests factorial of positive numbers.
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(FactorialTest, Positive) {
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(1, Factorial(1));
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(2, Factorial(2));
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(6, Factorial(3));
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_EQ(40320, Factorial(8));
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests IsPrime()
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests negative input.
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(IsPrimeTest, Negative) {
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This test belongs to the IsPrimeTest test case.
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(-1));
123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(-2));
124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(INT_MIN));
125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests some trivial cases.
128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(IsPrimeTest, Trivial) {
129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(0));
130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(1));
131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_TRUE(IsPrime(2));
132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_TRUE(IsPrime(3));
133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Tests positive input.
136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTEST(IsPrimeTest, Positive) {
137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(4));
138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_TRUE(IsPrime(5));
139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_FALSE(IsPrime(6));
140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  EXPECT_TRUE(IsPrime(23));
141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Step 3. Call RUN_ALL_TESTS() in main().
144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// We do this by linking in src/gtest_main.cc file, which consists of
146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// a main() function which calls RUN_ALL_TESTS() for us.
147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This runs all the tests you've defined, prints the result, and
149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns 0 if successful, or 1 otherwise.
150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Did you notice that we didn't register the tests?  The
152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// RUN_ALL_TESTS() macro magically knows about all the tests we
153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// defined.  Isn't this convenient?
154