sample1_unittest.cc 5.0 KB

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