?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  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.  
  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.  
  46. #include <limits.h>
  47. #include <time.h>
  48. #include "gtest/gtest.h"
  49. #include "sample1.h"
  50. #include "sample3-inl.h"
  51. namespace {
  52. // In this sample, we want to ensure that every test finishes within
  53. // ~5 seconds.  If a test takes longer to run, we consider it a
  54. // failure.
  55. //
  56. // We put the code for timing a test in a test fixture called
  57. // "QuickTest".  QuickTest is intended to be the super fixture that
  58. // other fixtures derive from, therefore there is no test case with
  59. // the name "QuickTest".  This is OK.
  60. //
  61. // Later, we will derive multiple test fixtures from QuickTest.
  62. class QuickTest : public testing::Test {
  63.  protected:
  64.   // Remember that SetUp() is run immediately before a test starts.
  65.   // This is a good place to record the start time.
  66.   virtual void SetUp() {
  67.     start_time_ = time(NULL);
  68.   }
  69.  
  70.   // TearDown() is invoked immediately after a test finishes.  Here we
  71.   // check if the test was too slow.
  72.   virtual void TearDown() {
  73.     // Gets the time when the test finishes
  74.     const time_t end_time = time(NULL);
  75.  
  76.     // Asserts that the test took no more than ~5 seconds.  Did you
  77.     // know that you can use assertions in SetUp() and TearDown() as
  78.     // well?
  79.     EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
  80.   }
  81.  
  82.   // The UTC time (in seconds) when the test starts
  83.   time_t start_time_;
  84. };
  85.  
  86.  
  87. // We derive a fixture named IntegerFunctionTest from the QuickTest
  88. // fixture.  All tests using this fixture will be automatically
  89. // required to be quick.
  90. class IntegerFunctionTest : public QuickTest {
  91.   // We don't need any more logic than already in the QuickTest fixture.
  92.   // Therefore the body is empty.
  93. };
  94.  
  95.  
  96. // Now we can write tests in the IntegerFunctionTest test case.
  97.  
  98. // Tests Factorial()
  99. TEST_F(IntegerFunctionTest, Factorial) {
  100.   // Tests factorial of negative numbers.
  101.   EXPECT_EQ(1, Factorial(-5));
  102.   EXPECT_EQ(1, Factorial(-1));
  103.   EXPECT_GT(Factorial(-10), 0);
  104.  
  105.   // Tests factorial of 0.
  106.   EXPECT_EQ(1, Factorial(0));
  107.  
  108.   // Tests factorial of positive numbers.
  109.   EXPECT_EQ(1, Factorial(1));
  110.   EXPECT_EQ(2, Factorial(2));
  111.   EXPECT_EQ(6, Factorial(3));
  112.   EXPECT_EQ(40320, Factorial(8));
  113. }
  114.  
  115.  
  116. // Tests IsPrime()
  117. TEST_F(IntegerFunctionTest, IsPrime) {
  118.   // Tests negative input.
  119.   EXPECT_FALSE(IsPrime(-1));
  120.   EXPECT_FALSE(IsPrime(-2));
  121.   EXPECT_FALSE(IsPrime(INT_MIN));
  122.  
  123.   // Tests some trivial cases.
  124.   EXPECT_FALSE(IsPrime(0));
  125.   EXPECT_FALSE(IsPrime(1));
  126.   EXPECT_TRUE(IsPrime(2));
  127.   EXPECT_TRUE(IsPrime(3));
  128.  
  129.   // Tests positive input.
  130.   EXPECT_FALSE(IsPrime(4));
  131.   EXPECT_TRUE(IsPrime(5));
  132.   EXPECT_FALSE(IsPrime(6));
  133.   EXPECT_TRUE(IsPrime(23));
  134. }
  135.  
  136.  
  137. // The next test case (named "QueueTest") also needs to be quick, so
  138. // we derive another fixture from QuickTest.
  139. //
  140. // The QueueTest test fixture has some logic and shared objects in
  141. // addition to what's in QuickTest already.  We define the additional
  142. // stuff inside the body of the test fixture, as usual.
  143. class QueueTest : public QuickTest {
  144.  protected:
  145.   virtual void SetUp() {
  146.     // First, we need to set up the super fixture (QuickTest).
  147.     QuickTest::SetUp();
  148.  
  149.     // Second, some additional setup for this fixture.
  150.     q1_.Enqueue(1);
  151.     q2_.Enqueue(2);
  152.     q2_.Enqueue(3);
  153.   }
  154.  
  155.   // By default, TearDown() inherits the behavior of
  156.   // QuickTest::TearDown().  As we have no additional cleaning work
  157.   // for QueueTest, we omit it here.
  158.   //
  159.   // virtual void TearDown() {
  160.   //   QuickTest::TearDown();
  161.   // }
  162.  
  163.   Queue<int> q0_;
  164.   Queue<int> q1_;
  165.   Queue<int> q2_;
  166. };
  167.  
  168.  
  169. // Now, let's write tests using the QueueTest fixture.
  170.  
  171. // Tests the default constructor.
  172. TEST_F(QueueTest, DefaultConstructor) {
  173.   EXPECT_EQ(0u, q0_.Size());
  174. }
  175.  
  176. // Tests Dequeue().
  177. TEST_F(QueueTest, Dequeue) {
  178.   int* n = q0_.Dequeue();
  179.   EXPECT_TRUE(n == NULL);
  180.  
  181.   n = q1_.Dequeue();
  182.   EXPECT_TRUE(n != NULL);
  183.   EXPECT_EQ(1, *n);
  184.   EXPECT_EQ(0u, q1_.Size());
  185.   delete n;
  186.  
  187.   n = q2_.Dequeue();
  188.   EXPECT_TRUE(n != NULL);
  189.   EXPECT_EQ(2, *n);
  190.   EXPECT_EQ(1u, q2_.Size());
  191.   delete n;
  192. }
  193. }  // namespace
  194. // If necessary, you can derive further test fixtures from a derived
  195. // fixture itself.  For example, you can derive another fixture from
  196. // QueueTest.  Google Test imposes no limit on how deep the hierarchy
  197. // can be.  In practice, however, you probably don't want it to be too
  198. // deep as to be confusing.
  199.