sample5_unittest.cc 6.4 KB

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