1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample1_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +// Copyright 2005, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// A sample program demonstrating using Google C++ testing framework. 1.34 +// 1.35 +// Author: wan@google.com (Zhanyong Wan) 1.36 + 1.37 + 1.38 +// This sample shows how to write a simple unit test for a function, 1.39 +// using Google C++ testing framework. 1.40 +// 1.41 +// Writing a unit test using Google C++ testing framework is easy as 1-2-3: 1.42 + 1.43 + 1.44 +// Step 1. Include necessary header files such that the stuff your 1.45 +// test logic needs is declared. 1.46 +// 1.47 +// Don't forget gtest.h, which declares the testing framework. 1.48 + 1.49 +#include <limits.h> 1.50 +#include "sample1.h" 1.51 +#include "gtest/gtest.h" 1.52 + 1.53 + 1.54 +// Step 2. Use the TEST macro to define your tests. 1.55 +// 1.56 +// TEST has two parameters: the test case name and the test name. 1.57 +// After using the macro, you should define your test logic between a 1.58 +// pair of braces. You can use a bunch of macros to indicate the 1.59 +// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are 1.60 +// examples of such macros. For a complete list, see gtest.h. 1.61 +// 1.62 +// <TechnicalDetails> 1.63 +// 1.64 +// In Google Test, tests are grouped into test cases. This is how we 1.65 +// keep test code organized. You should put logically related tests 1.66 +// into the same test case. 1.67 +// 1.68 +// The test case name and the test name should both be valid C++ 1.69 +// identifiers. And you should not use underscore (_) in the names. 1.70 +// 1.71 +// Google Test guarantees that each test you define is run exactly 1.72 +// once, but it makes no guarantee on the order the tests are 1.73 +// executed. Therefore, you should write your tests in such a way 1.74 +// that their results don't depend on their order. 1.75 +// 1.76 +// </TechnicalDetails> 1.77 + 1.78 + 1.79 +// Tests Factorial(). 1.80 + 1.81 +// Tests factorial of negative numbers. 1.82 +TEST(FactorialTest, Negative) { 1.83 + // This test is named "Negative", and belongs to the "FactorialTest" 1.84 + // test case. 1.85 + EXPECT_EQ(1, Factorial(-5)); 1.86 + EXPECT_EQ(1, Factorial(-1)); 1.87 + EXPECT_GT(Factorial(-10), 0); 1.88 + 1.89 + // <TechnicalDetails> 1.90 + // 1.91 + // EXPECT_EQ(expected, actual) is the same as 1.92 + // 1.93 + // EXPECT_TRUE((expected) == (actual)) 1.94 + // 1.95 + // except that it will print both the expected value and the actual 1.96 + // value when the assertion fails. This is very helpful for 1.97 + // debugging. Therefore in this case EXPECT_EQ is preferred. 1.98 + // 1.99 + // On the other hand, EXPECT_TRUE accepts any Boolean expression, 1.100 + // and is thus more general. 1.101 + // 1.102 + // </TechnicalDetails> 1.103 +} 1.104 + 1.105 +// Tests factorial of 0. 1.106 +TEST(FactorialTest, Zero) { 1.107 + EXPECT_EQ(1, Factorial(0)); 1.108 +} 1.109 + 1.110 +// Tests factorial of positive numbers. 1.111 +TEST(FactorialTest, Positive) { 1.112 + EXPECT_EQ(1, Factorial(1)); 1.113 + EXPECT_EQ(2, Factorial(2)); 1.114 + EXPECT_EQ(6, Factorial(3)); 1.115 + EXPECT_EQ(40320, Factorial(8)); 1.116 +} 1.117 + 1.118 + 1.119 +// Tests IsPrime() 1.120 + 1.121 +// Tests negative input. 1.122 +TEST(IsPrimeTest, Negative) { 1.123 + // This test belongs to the IsPrimeTest test case. 1.124 + 1.125 + EXPECT_FALSE(IsPrime(-1)); 1.126 + EXPECT_FALSE(IsPrime(-2)); 1.127 + EXPECT_FALSE(IsPrime(INT_MIN)); 1.128 +} 1.129 + 1.130 +// Tests some trivial cases. 1.131 +TEST(IsPrimeTest, Trivial) { 1.132 + EXPECT_FALSE(IsPrime(0)); 1.133 + EXPECT_FALSE(IsPrime(1)); 1.134 + EXPECT_TRUE(IsPrime(2)); 1.135 + EXPECT_TRUE(IsPrime(3)); 1.136 +} 1.137 + 1.138 +// Tests positive input. 1.139 +TEST(IsPrimeTest, Positive) { 1.140 + EXPECT_FALSE(IsPrime(4)); 1.141 + EXPECT_TRUE(IsPrime(5)); 1.142 + EXPECT_FALSE(IsPrime(6)); 1.143 + EXPECT_TRUE(IsPrime(23)); 1.144 +} 1.145 + 1.146 +// Step 3. Call RUN_ALL_TESTS() in main(). 1.147 +// 1.148 +// We do this by linking in src/gtest_main.cc file, which consists of 1.149 +// a main() function which calls RUN_ALL_TESTS() for us. 1.150 +// 1.151 +// This runs all the tests you've defined, prints the result, and 1.152 +// returns 0 if successful, or 1 otherwise. 1.153 +// 1.154 +// Did you notice that we didn't register the tests? The 1.155 +// RUN_ALL_TESTS() macro magically knows about all the tests we 1.156 +// defined. Isn't this convenient?