sample1_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // A sample program demonstrating using Google C++ testing framework.
00031 //
00032 // Author: wan@google.com (Zhanyong Wan)
00033 
00034 
00035 // This sample shows how to write a simple unit test for a function,
00036 // using Google C++ testing framework.
00037 //
00038 // Writing a unit test using Google C++ testing framework is easy as 1-2-3:
00039 
00040 
00041 // Step 1. Include necessary header files such that the stuff your
00042 // test logic needs is declared.
00043 //
00044 // Don't forget gtest.h, which declares the testing framework.
00045 
00046 #include <limits.h>
00047 #include "sample1.h"
00048 #include "gtest/gtest.h"
00049 
00050 
00051 // Step 2. Use the TEST macro to define your tests.
00052 //
00053 // TEST has two parameters: the test case name and the test name.
00054 // After using the macro, you should define your test logic between a
00055 // pair of braces.  You can use a bunch of macros to indicate the
00056 // success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
00057 // examples of such macros.  For a complete list, see gtest.h.
00058 //
00059 // <TechnicalDetails>
00060 //
00061 // In Google Test, tests are grouped into test cases.  This is how we
00062 // keep test code organized.  You should put logically related tests
00063 // into the same test case.
00064 //
00065 // The test case name and the test name should both be valid C++
00066 // identifiers.  And you should not use underscore (_) in the names.
00067 //
00068 // Google Test guarantees that each test you define is run exactly
00069 // once, but it makes no guarantee on the order the tests are
00070 // executed.  Therefore, you should write your tests in such a way
00071 // that their results don't depend on their order.
00072 //
00073 // </TechnicalDetails>
00074 
00075 
00076 // Tests Factorial().
00077 
00078 // Tests factorial of negative numbers.
00079 TEST(FactorialTest, Negative) {
00080   // This test is named "Negative", and belongs to the "FactorialTest"
00081   // test case.
00082   EXPECT_EQ(1, Factorial(-5));
00083   EXPECT_EQ(1, Factorial(-1));
00084   EXPECT_GT(Factorial(-10), 0);
00085 
00086   // <TechnicalDetails>
00087   //
00088   // EXPECT_EQ(expected, actual) is the same as
00089   //
00090   //   EXPECT_TRUE((expected) == (actual))
00091   //
00092   // except that it will print both the expected value and the actual
00093   // value when the assertion fails.  This is very helpful for
00094   // debugging.  Therefore in this case EXPECT_EQ is preferred.
00095   //
00096   // On the other hand, EXPECT_TRUE accepts any Boolean expression,
00097   // and is thus more general.
00098   //
00099   // </TechnicalDetails>
00100 }
00101 
00102 // Tests factorial of 0.
00103 TEST(FactorialTest, Zero) {
00104   EXPECT_EQ(1, Factorial(0));
00105 }
00106 
00107 // Tests factorial of positive numbers.
00108 TEST(FactorialTest, Positive) {
00109   EXPECT_EQ(1, Factorial(1));
00110   EXPECT_EQ(2, Factorial(2));
00111   EXPECT_EQ(6, Factorial(3));
00112   EXPECT_EQ(40320, Factorial(8));
00113 }
00114 
00115 
00116 // Tests IsPrime()
00117 
00118 // Tests negative input.
00119 TEST(IsPrimeTest, Negative) {
00120   // This test belongs to the IsPrimeTest test case.
00121 
00122   EXPECT_FALSE(IsPrime(-1));
00123   EXPECT_FALSE(IsPrime(-2));
00124   EXPECT_FALSE(IsPrime(INT_MIN));
00125 }
00126 
00127 // Tests some trivial cases.
00128 TEST(IsPrimeTest, Trivial) {
00129   EXPECT_FALSE(IsPrime(0));
00130   EXPECT_FALSE(IsPrime(1));
00131   EXPECT_TRUE(IsPrime(2));
00132   EXPECT_TRUE(IsPrime(3));
00133 }
00134 
00135 // Tests positive input.
00136 TEST(IsPrimeTest, Positive) {
00137   EXPECT_FALSE(IsPrime(4));
00138   EXPECT_TRUE(IsPrime(5));
00139   EXPECT_FALSE(IsPrime(6));
00140   EXPECT_TRUE(IsPrime(23));
00141 }
00142 
00143 // Step 3. Call RUN_ALL_TESTS() in main().
00144 //
00145 // We do this by linking in src/gtest_main.cc file, which consists of
00146 // a main() function which calls RUN_ALL_TESTS() for us.
00147 //
00148 // This runs all the tests you've defined, prints the result, and
00149 // returns 0 if successful, or 1 otherwise.
00150 //
00151 // Did you notice that we didn't register the tests?  The
00152 // RUN_ALL_TESTS() macro magically knows about all the tests we
00153 // defined.  Isn't this convenient?


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06