You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.0 KiB

  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. // A sample program demonstrating using Google C++ testing framework.
  30. // This sample shows how to write a simple unit test for a function,
  31. // using Google C++ testing framework.
  32. //
  33. // Writing a unit test using Google C++ testing framework is easy as 1-2-3:
  34. // Step 1. Include necessary header files such that the stuff your
  35. // test logic needs is declared.
  36. //
  37. // Don't forget gtest.h, which declares the testing framework.
  38. #include <limits.h>
  39. #include "sample1.h"
  40. #include "gtest/gtest.h"
  41. namespace {
  42. // Step 2. Use the TEST macro to define your tests.
  43. //
  44. // TEST has two parameters: the test case name and the test name.
  45. // After using the macro, you should define your test logic between a
  46. // pair of braces. You can use a bunch of macros to indicate the
  47. // success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
  48. // examples of such macros. For a complete list, see gtest.h.
  49. //
  50. // <TechnicalDetails>
  51. //
  52. // In Google Test, tests are grouped into test cases. This is how we
  53. // keep test code organized. You should put logically related tests
  54. // into the same test case.
  55. //
  56. // The test case name and the test name should both be valid C++
  57. // identifiers. And you should not use underscore (_) in the names.
  58. //
  59. // Google Test guarantees that each test you define is run exactly
  60. // once, but it makes no guarantee on the order the tests are
  61. // executed. Therefore, you should write your tests in such a way
  62. // that their results don't depend on their order.
  63. //
  64. // </TechnicalDetails>
  65. // Tests Factorial().
  66. // Tests factorial of negative numbers.
  67. TEST(FactorialTest, Negative) {
  68. // This test is named "Negative", and belongs to the "FactorialTest"
  69. // test case.
  70. EXPECT_EQ(1, Factorial(-5));
  71. EXPECT_EQ(1, Factorial(-1));
  72. EXPECT_GT(Factorial(-10), 0);
  73. // <TechnicalDetails>
  74. //
  75. // EXPECT_EQ(expected, actual) is the same as
  76. //
  77. // EXPECT_TRUE((expected) == (actual))
  78. //
  79. // except that it will print both the expected value and the actual
  80. // value when the assertion fails. This is very helpful for
  81. // debugging. Therefore in this case EXPECT_EQ is preferred.
  82. //
  83. // On the other hand, EXPECT_TRUE accepts any Boolean expression,
  84. // and is thus more general.
  85. //
  86. // </TechnicalDetails>
  87. }
  88. // Tests factorial of 0.
  89. TEST(FactorialTest, Zero) {
  90. EXPECT_EQ(1, Factorial(0));
  91. }
  92. // Tests factorial of positive numbers.
  93. TEST(FactorialTest, Positive) {
  94. EXPECT_EQ(1, Factorial(1));
  95. EXPECT_EQ(2, Factorial(2));
  96. EXPECT_EQ(6, Factorial(3));
  97. EXPECT_EQ(40320, Factorial(8));
  98. }
  99. // Tests IsPrime()
  100. // Tests negative input.
  101. TEST(IsPrimeTest, Negative) {
  102. // This test belongs to the IsPrimeTest test case.
  103. EXPECT_FALSE(IsPrime(-1));
  104. EXPECT_FALSE(IsPrime(-2));
  105. EXPECT_FALSE(IsPrime(INT_MIN));
  106. }
  107. // Tests some trivial cases.
  108. TEST(IsPrimeTest, Trivial) {
  109. EXPECT_FALSE(IsPrime(0));
  110. EXPECT_FALSE(IsPrime(1));
  111. EXPECT_TRUE(IsPrime(2));
  112. EXPECT_TRUE(IsPrime(3));
  113. }
  114. // Tests positive input.
  115. TEST(IsPrimeTest, Positive) {
  116. EXPECT_FALSE(IsPrime(4));
  117. EXPECT_TRUE(IsPrime(5));
  118. EXPECT_FALSE(IsPrime(6));
  119. EXPECT_TRUE(IsPrime(23));
  120. }
  121. } // namespace
  122. // Step 3. Call RUN_ALL_TESTS() in main().
  123. //
  124. // We do this by linking in src/gtest_main.cc file, which consists of
  125. // a main() function which calls RUN_ALL_TESTS() for us.
  126. //
  127. // This runs all the tests you've defined, prints the result, and
  128. // returns 0 if successful, or 1 otherwise.
  129. //
  130. // Did you notice that we didn't register the tests? The
  131. // RUN_ALL_TESTS() macro magically knows about all the tests we
  132. // defined. Isn't this convenient?