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