14b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Copyright 2005, Google Inc.
24b6829f0d28990dd645e16386eb226d0f10c8731shiqian// All rights reserved.
34b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
44b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Redistribution and use in source and binary forms, with or without
54b6829f0d28990dd645e16386eb226d0f10c8731shiqian// modification, are permitted provided that the following conditions are
64b6829f0d28990dd645e16386eb226d0f10c8731shiqian// met:
74b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
84b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions of source code must retain the above copyright
94b6829f0d28990dd645e16386eb226d0f10c8731shiqian// notice, this list of conditions and the following disclaimer.
104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions in binary form must reproduce the above
114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// copyright notice, this list of conditions and the following disclaimer
124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// in the documentation and/or other materials provided with the
134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// distribution.
144b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Neither the name of Google Inc. nor the names of its
154b6829f0d28990dd645e16386eb226d0f10c8731shiqian// contributors may be used to endorse or promote products derived from
164b6829f0d28990dd645e16386eb226d0f10c8731shiqian// this software without specific prior written permission.
174b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
184b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294b6829f0d28990dd645e16386eb226d0f10c8731shiqian
304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A sample program demonstrating using Google C++ testing framework.
314b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Author: wan@google.com (Zhanyong Wan)
334b6829f0d28990dd645e16386eb226d0f10c8731shiqian
344b6829f0d28990dd645e16386eb226d0f10c8731shiqian
354b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This sample shows how to write a simple unit test for a function,
364b6829f0d28990dd645e16386eb226d0f10c8731shiqian// using Google C++ testing framework.
374b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
384b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
394b6829f0d28990dd645e16386eb226d0f10c8731shiqian
404b6829f0d28990dd645e16386eb226d0f10c8731shiqian
414b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Step 1. Include necessary header files such that the stuff your
424b6829f0d28990dd645e16386eb226d0f10c8731shiqian// test logic needs is declared.
434b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
444b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Don't forget gtest.h, which declares the testing framework.
454b6829f0d28990dd645e16386eb226d0f10c8731shiqian
464b6829f0d28990dd645e16386eb226d0f10c8731shiqian#include <limits.h>
474b6829f0d28990dd645e16386eb226d0f10c8731shiqian#include "sample1.h"
482620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest.h"
494b6829f0d28990dd645e16386eb226d0f10c8731shiqian
504b6829f0d28990dd645e16386eb226d0f10c8731shiqian
514b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Step 2. Use the TEST macro to define your tests.
524b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
534b6829f0d28990dd645e16386eb226d0f10c8731shiqian// TEST has two parameters: the test case name and the test name.
544b6829f0d28990dd645e16386eb226d0f10c8731shiqian// After using the macro, you should define your test logic between a
554b6829f0d28990dd645e16386eb226d0f10c8731shiqian// pair of braces.  You can use a bunch of macros to indicate the
564b6829f0d28990dd645e16386eb226d0f10c8731shiqian// success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
574b6829f0d28990dd645e16386eb226d0f10c8731shiqian// examples of such macros.  For a complete list, see gtest.h.
584b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
594b6829f0d28990dd645e16386eb226d0f10c8731shiqian// <TechnicalDetails>
604b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// In Google Test, tests are grouped into test cases.  This is how we
624b6829f0d28990dd645e16386eb226d0f10c8731shiqian// keep test code organized.  You should put logically related tests
634b6829f0d28990dd645e16386eb226d0f10c8731shiqian// into the same test case.
644b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
654b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The test case name and the test name should both be valid C++
664b6829f0d28990dd645e16386eb226d0f10c8731shiqian// identifiers.  And you should not use underscore (_) in the names.
674b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
684b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Google Test guarantees that each test you define is run exactly
694b6829f0d28990dd645e16386eb226d0f10c8731shiqian// once, but it makes no guarantee on the order the tests are
704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// executed.  Therefore, you should write your tests in such a way
714b6829f0d28990dd645e16386eb226d0f10c8731shiqian// that their results don't depend on their order.
724b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
734b6829f0d28990dd645e16386eb226d0f10c8731shiqian// </TechnicalDetails>
744b6829f0d28990dd645e16386eb226d0f10c8731shiqian
754b6829f0d28990dd645e16386eb226d0f10c8731shiqian
764b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests Factorial().
774b6829f0d28990dd645e16386eb226d0f10c8731shiqian
784b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests factorial of negative numbers.
794b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(FactorialTest, Negative) {
804b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // This test is named "Negative", and belongs to the "FactorialTest"
814b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // test case.
824b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(1, Factorial(-5));
834b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(1, Factorial(-1));
8493fed47dbf8e6bc3d39d3f769cb5039551747257vladlosev  EXPECT_GT(Factorial(-10), 0);
854b6829f0d28990dd645e16386eb226d0f10c8731shiqian
864b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // <TechnicalDetails>
874b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
884b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // EXPECT_EQ(expected, actual) is the same as
894b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
904b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   EXPECT_TRUE((expected) == (actual))
914b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
924b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // except that it will print both the expected value and the actual
934b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // value when the assertion fails.  This is very helpful for
944b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // debugging.  Therefore in this case EXPECT_EQ is preferred.
954b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
964b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // On the other hand, EXPECT_TRUE accepts any Boolean expression,
974b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // and is thus more general.
984b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
994b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // </TechnicalDetails>
1004b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1014b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1024b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests factorial of 0.
1034b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(FactorialTest, Zero) {
1044b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(1, Factorial(0));
1054b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1064b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1074b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests factorial of positive numbers.
1084b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(FactorialTest, Positive) {
1094b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(1, Factorial(1));
1104b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(2, Factorial(2));
1114b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(6, Factorial(3));
1124b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_EQ(40320, Factorial(8));
1134b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1144b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1154b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1164b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests IsPrime()
1174b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1184b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests negative input.
1194b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(IsPrimeTest, Negative) {
1204b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // This test belongs to the IsPrimeTest test case.
1214b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1224b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(-1));
1234b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(-2));
1244b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(INT_MIN));
1254b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1264b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1274b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests some trivial cases.
1284b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(IsPrimeTest, Trivial) {
1294b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(0));
1304b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(1));
1314b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_TRUE(IsPrime(2));
1324b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_TRUE(IsPrime(3));
1334b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1344b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1354b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Tests positive input.
1364b6829f0d28990dd645e16386eb226d0f10c8731shiqianTEST(IsPrimeTest, Positive) {
1374b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(4));
1384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_TRUE(IsPrime(5));
1394b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_FALSE(IsPrime(6));
1404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_TRUE(IsPrime(23));
1414b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
1424b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1434b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Step 3. Call RUN_ALL_TESTS() in main().
1444b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
1454b6829f0d28990dd645e16386eb226d0f10c8731shiqian// We do this by linking in src/gtest_main.cc file, which consists of
1464b6829f0d28990dd645e16386eb226d0f10c8731shiqian// a main() function which calls RUN_ALL_TESTS() for us.
1474b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
1484b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This runs all the tests you've defined, prints the result, and
1494b6829f0d28990dd645e16386eb226d0f10c8731shiqian// returns 0 if successful, or 1 otherwise.
1504b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
1514b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Did you notice that we didn't register the tests?  The
1524b6829f0d28990dd645e16386eb226d0f10c8731shiqian// RUN_ALL_TESTS() macro magically knows about all the tests we
1534b6829f0d28990dd645e16386eb226d0f10c8731shiqian// defined.  Isn't this convenient?
154