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