sample5_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. virtual void SetUp() {
  64. start_time_ = time(NULL);
  65. }
  66. // TearDown() is invoked immediately after a test finishes. Here we
  67. // check if the test was too slow.
  68. virtual void TearDown() {
  69. // Gets the time when the test finishes
  70. const time_t end_time = time(NULL);
  71. // Asserts that the test took no more than ~5 seconds. Did you
  72. // know that you can use assertions in SetUp() and TearDown() as
  73. // well?
  74. EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
  75. }
  76. // The UTC time (in seconds) when the test starts
  77. time_t start_time_;
  78. };
  79. // We derive a fixture named IntegerFunctionTest from the QuickTest
  80. // fixture. All tests using this fixture will be automatically
  81. // required to be quick.
  82. class IntegerFunctionTest : public QuickTest {
  83. // We don't need any more logic than already in the QuickTest fixture.
  84. // Therefore the body is empty.
  85. };
  86. // Now we can write tests in the IntegerFunctionTest test case.
  87. // Tests Factorial()
  88. TEST_F(IntegerFunctionTest, Factorial) {
  89. // Tests factorial of negative numbers.
  90. EXPECT_EQ(1, Factorial(-5));
  91. EXPECT_EQ(1, Factorial(-1));
  92. EXPECT_GT(Factorial(-10), 0);
  93. // Tests factorial of 0.
  94. EXPECT_EQ(1, Factorial(0));
  95. // Tests factorial of positive numbers.
  96. EXPECT_EQ(1, Factorial(1));
  97. EXPECT_EQ(2, Factorial(2));
  98. EXPECT_EQ(6, Factorial(3));
  99. EXPECT_EQ(40320, Factorial(8));
  100. }
  101. // Tests IsPrime()
  102. TEST_F(IntegerFunctionTest, IsPrime) {
  103. // Tests negative input.
  104. EXPECT_FALSE(IsPrime(-1));
  105. EXPECT_FALSE(IsPrime(-2));
  106. EXPECT_FALSE(IsPrime(INT_MIN));
  107. // Tests some trivial cases.
  108. EXPECT_FALSE(IsPrime(0));
  109. EXPECT_FALSE(IsPrime(1));
  110. EXPECT_TRUE(IsPrime(2));
  111. EXPECT_TRUE(IsPrime(3));
  112. // Tests positive input.
  113. EXPECT_FALSE(IsPrime(4));
  114. EXPECT_TRUE(IsPrime(5));
  115. EXPECT_FALSE(IsPrime(6));
  116. EXPECT_TRUE(IsPrime(23));
  117. }
  118. // The next test case (named "QueueTest") also needs to be quick, so
  119. // we derive another fixture from QuickTest.
  120. //
  121. // The QueueTest test fixture has some logic and shared objects in
  122. // addition to what's in QuickTest already. We define the additional
  123. // stuff inside the body of the test fixture, as usual.
  124. class QueueTest : public QuickTest {
  125. protected:
  126. virtual void SetUp() {
  127. // First, we need to set up the super fixture (QuickTest).
  128. QuickTest::SetUp();
  129. // Second, some additional setup for this fixture.
  130. q1_.Enqueue(1);
  131. q2_.Enqueue(2);
  132. q2_.Enqueue(3);
  133. }
  134. // By default, TearDown() inherits the behavior of
  135. // QuickTest::TearDown(). As we have no additional cleaning work
  136. // for QueueTest, we omit it here.
  137. //
  138. // virtual void TearDown() {
  139. // QuickTest::TearDown();
  140. // }
  141. Queue<int> q0_;
  142. Queue<int> q1_;
  143. Queue<int> q2_;
  144. };
  145. // Now, let's write tests using the QueueTest fixture.
  146. // Tests the default constructor.
  147. TEST_F(QueueTest, DefaultConstructor) {
  148. EXPECT_EQ(0u, q0_.Size());
  149. }
  150. // Tests Dequeue().
  151. TEST_F(QueueTest, Dequeue) {
  152. int* n = q0_.Dequeue();
  153. EXPECT_TRUE(n == NULL);
  154. n = q1_.Dequeue();
  155. EXPECT_TRUE(n != NULL);
  156. EXPECT_EQ(1, *n);
  157. EXPECT_EQ(0u, q1_.Size());
  158. delete n;
  159. n = q2_.Dequeue();
  160. EXPECT_TRUE(n != NULL);
  161. EXPECT_EQ(2, *n);
  162. EXPECT_EQ(1u, q2_.Size());
  163. delete n;
  164. }
  165. } // namespace
  166. // If necessary, you can derive further test fixtures from a derived
  167. // fixture itself. For example, you can derive another fixture from
  168. // QueueTest. Google Test imposes no limit on how deep the hierarchy
  169. // can be. In practice, however, you probably don't want it to be too
  170. // deep as to be confusing.