michael@0: // Copyright 2005, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // A sample program demonstrating using Google C++ testing framework. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: michael@0: // This sample shows how to write a simple unit test for a function, michael@0: // using Google C++ testing framework. michael@0: // michael@0: // Writing a unit test using Google C++ testing framework is easy as 1-2-3: michael@0: michael@0: michael@0: // Step 1. Include necessary header files such that the stuff your michael@0: // test logic needs is declared. michael@0: // michael@0: // Don't forget gtest.h, which declares the testing framework. michael@0: michael@0: #include michael@0: #include "sample1.h" michael@0: #include "gtest/gtest.h" michael@0: michael@0: michael@0: // Step 2. Use the TEST macro to define your tests. michael@0: // michael@0: // TEST has two parameters: the test case name and the test name. michael@0: // After using the macro, you should define your test logic between a michael@0: // pair of braces. You can use a bunch of macros to indicate the michael@0: // success or failure of a test. EXPECT_TRUE and EXPECT_EQ are michael@0: // examples of such macros. For a complete list, see gtest.h. michael@0: // michael@0: // michael@0: // michael@0: // In Google Test, tests are grouped into test cases. This is how we michael@0: // keep test code organized. You should put logically related tests michael@0: // into the same test case. michael@0: // michael@0: // The test case name and the test name should both be valid C++ michael@0: // identifiers. And you should not use underscore (_) in the names. michael@0: // michael@0: // Google Test guarantees that each test you define is run exactly michael@0: // once, but it makes no guarantee on the order the tests are michael@0: // executed. Therefore, you should write your tests in such a way michael@0: // that their results don't depend on their order. michael@0: // michael@0: // michael@0: michael@0: michael@0: // Tests Factorial(). michael@0: michael@0: // Tests factorial of negative numbers. michael@0: TEST(FactorialTest, Negative) { michael@0: // This test is named "Negative", and belongs to the "FactorialTest" michael@0: // test case. michael@0: EXPECT_EQ(1, Factorial(-5)); michael@0: EXPECT_EQ(1, Factorial(-1)); michael@0: EXPECT_GT(Factorial(-10), 0); michael@0: michael@0: // michael@0: // michael@0: // EXPECT_EQ(expected, actual) is the same as michael@0: // michael@0: // EXPECT_TRUE((expected) == (actual)) michael@0: // michael@0: // except that it will print both the expected value and the actual michael@0: // value when the assertion fails. This is very helpful for michael@0: // debugging. Therefore in this case EXPECT_EQ is preferred. michael@0: // michael@0: // On the other hand, EXPECT_TRUE accepts any Boolean expression, michael@0: // and is thus more general. michael@0: // michael@0: // michael@0: } michael@0: michael@0: // Tests factorial of 0. michael@0: TEST(FactorialTest, Zero) { michael@0: EXPECT_EQ(1, Factorial(0)); michael@0: } michael@0: michael@0: // Tests factorial of positive numbers. michael@0: TEST(FactorialTest, Positive) { michael@0: EXPECT_EQ(1, Factorial(1)); michael@0: EXPECT_EQ(2, Factorial(2)); michael@0: EXPECT_EQ(6, Factorial(3)); michael@0: EXPECT_EQ(40320, Factorial(8)); michael@0: } michael@0: michael@0: michael@0: // Tests IsPrime() michael@0: michael@0: // Tests negative input. michael@0: TEST(IsPrimeTest, Negative) { michael@0: // This test belongs to the IsPrimeTest test case. michael@0: michael@0: EXPECT_FALSE(IsPrime(-1)); michael@0: EXPECT_FALSE(IsPrime(-2)); michael@0: EXPECT_FALSE(IsPrime(INT_MIN)); michael@0: } michael@0: michael@0: // Tests some trivial cases. michael@0: TEST(IsPrimeTest, Trivial) { michael@0: EXPECT_FALSE(IsPrime(0)); michael@0: EXPECT_FALSE(IsPrime(1)); michael@0: EXPECT_TRUE(IsPrime(2)); michael@0: EXPECT_TRUE(IsPrime(3)); michael@0: } michael@0: michael@0: // Tests positive input. michael@0: TEST(IsPrimeTest, Positive) { michael@0: EXPECT_FALSE(IsPrime(4)); michael@0: EXPECT_TRUE(IsPrime(5)); michael@0: EXPECT_FALSE(IsPrime(6)); michael@0: EXPECT_TRUE(IsPrime(23)); michael@0: } michael@0: michael@0: // Step 3. Call RUN_ALL_TESTS() in main(). michael@0: // michael@0: // We do this by linking in src/gtest_main.cc file, which consists of michael@0: // a main() function which calls RUN_ALL_TESTS() for us. michael@0: // michael@0: // This runs all the tests you've defined, prints the result, and michael@0: // returns 0 if successful, or 1 otherwise. michael@0: // michael@0: // Did you notice that we didn't register the tests? The michael@0: // RUN_ALL_TESTS() macro magically knows about all the tests we michael@0: // defined. Isn't this convenient?