sample5_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // This sample teaches how to reuse a test fixture in multiple test
  30. // cases by deriving sub-fixtures from it.
  31. //
  32. // When you define a test fixture, you specify the name of the test
  33. // case that will use this fixture. Therefore, a test fixture can
  34. // be used by only one test case.
  35. //
  36. // Sometimes, more than one test cases may want to use the same or
  37. // slightly different test fixtures. For example, you may want to
  38. // make sure that all tests for a GUI library don't leak important
  39. // system resources like fonts and brushes. In Google Test, you do
  40. // this by putting the shared logic in a super (as in "super class")
  41. // test fixture, and then have each test case use a fixture derived
  42. // from this super fixture.
  43. #include <limits.h>
  44. #include <time.h>
  45. #include "gtest/gtest.h"
  46. #include "sample1.h"
  47. #include "sample3-inl.h"
  48. namespace {
  49. // In this sample, we want to ensure that every test finishes within
  50. // ~5 seconds. If a test takes longer to run, we consider it a
  51. // failure.
  52. //
  53. // We put the code for timing a test in a test fixture called
  54. // "QuickTest". QuickTest is intended to be the super fixture that
  55. // other fixtures derive from, therefore there is no test case with
  56. // the name "QuickTest". This is OK.
  57. //
  58. // Later, we will derive multiple test fixtures from QuickTest.
  59. class QuickTest : public testing::Test {
  60. protected:
  61. // Remember that SetUp() is run immediately before a test starts.
  62. // This is a good place to record the start time.
  63. void SetUp() override { start_time_ = time(nullptr); }
  64. // TearDown() is invoked immediately after a test finishes. Here we
  65. // check if the test was too slow.
  66. void TearDown() override {
  67. // Gets the time when the test finishes
  68. const time_t end_time = time(nullptr);
  69. // Asserts that the test took no more than ~5 seconds. Did you
  70. // know that you can use assertions in SetUp() and TearDown() as
  71. // well?
  72. EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
  73. }
  74. // The UTC time (in seconds) when the test starts
  75. time_t start_time_;
  76. };
  77. // We derive a fixture named IntegerFunctionTest from the QuickTest
  78. // fixture. All tests using this fixture will be automatically
  79. // required to be quick.
  80. class IntegerFunctionTest : public QuickTest {
  81. // We don't need any more logic than already in the QuickTest fixture.
  82. // Therefore the body is empty.
  83. };
  84. // Now we can write tests in the IntegerFunctionTest test case.
  85. // Tests Factorial()
  86. TEST_F(IntegerFunctionTest, Factorial) {
  87. // Tests factorial of negative numbers.
  88. EXPECT_EQ(1, Factorial(-5));
  89. EXPECT_EQ(1, Factorial(-1));
  90. EXPECT_GT(Factorial(-10), 0);
  91. // Tests factorial of 0.
  92. EXPECT_EQ(1, Factorial(0));
  93. // Tests factorial of positive numbers.
  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. TEST_F(IntegerFunctionTest, IsPrime) {
  101. // Tests negative input.
  102. EXPECT_FALSE(IsPrime(-1));
  103. EXPECT_FALSE(IsPrime(-2));
  104. EXPECT_FALSE(IsPrime(INT_MIN));
  105. // Tests some trivial cases.
  106. EXPECT_FALSE(IsPrime(0));
  107. EXPECT_FALSE(IsPrime(1));
  108. EXPECT_TRUE(IsPrime(2));
  109. EXPECT_TRUE(IsPrime(3));
  110. // Tests positive input.
  111. EXPECT_FALSE(IsPrime(4));
  112. EXPECT_TRUE(IsPrime(5));
  113. EXPECT_FALSE(IsPrime(6));
  114. EXPECT_TRUE(IsPrime(23));
  115. }
  116. // The next test case (named "QueueTest") also needs to be quick, so
  117. // we derive another fixture from QuickTest.
  118. //
  119. // The QueueTest test fixture has some logic and shared objects in
  120. // addition to what's in QuickTest already. We define the additional
  121. // stuff inside the body of the test fixture, as usual.
  122. class QueueTest : public QuickTest {
  123. protected:
  124. void SetUp() override {
  125. // First, we need to set up the super fixture (QuickTest).
  126. QuickTest::SetUp();
  127. // Second, some additional setup for this fixture.
  128. q1_.Enqueue(1);
  129. q2_.Enqueue(2);
  130. q2_.Enqueue(3);
  131. }
  132. // By default, TearDown() inherits the behavior of
  133. // QuickTest::TearDown(). As we have no additional cleaning work
  134. // for QueueTest, we omit it here.
  135. //
  136. // virtual void TearDown() {
  137. // QuickTest::TearDown();
  138. // }
  139. Queue<int> q0_;
  140. Queue<int> q1_;
  141. Queue<int> q2_;
  142. };
  143. // Now, let's write tests using the QueueTest fixture.
  144. // Tests the default constructor.
  145. TEST_F(QueueTest, DefaultConstructor) {
  146. EXPECT_EQ(0u, q0_.Size());
  147. }
  148. // Tests Dequeue().
  149. TEST_F(QueueTest, Dequeue) {
  150. int* n = q0_.Dequeue();
  151. EXPECT_TRUE(n == nullptr);
  152. n = q1_.Dequeue();
  153. EXPECT_TRUE(n != nullptr);
  154. EXPECT_EQ(1, *n);
  155. EXPECT_EQ(0u, q1_.Size());
  156. delete n;
  157. n = q2_.Dequeue();
  158. EXPECT_TRUE(n != nullptr);
  159. EXPECT_EQ(2, *n);
  160. EXPECT_EQ(1u, q2_.Size());
  161. delete n;
  162. }
  163. } // namespace
  164. // If necessary, you can derive further test fixtures from a derived
  165. // fixture itself. For example, you can derive another fixture from
  166. // QueueTest. Google Test imposes no limit on how deep the hierarchy
  167. // can be. In practice, however, you probably don't want it to be too
  168. // deep as to be confusing.