sample3_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // In this example, we use a more advanced feature of Google Test called
  31. // test fixture.
  32. //
  33. // A test fixture is a place to hold objects and functions shared by
  34. // all tests in a test case. Using a test fixture avoids duplicating
  35. // the test code necessary to initialize and cleanup those common
  36. // objects for each test. It is also useful for defining sub-routines
  37. // that your tests need to invoke a lot.
  38. //
  39. // <TechnicalDetails>
  40. //
  41. // The tests share the test fixture in the sense of code sharing, not
  42. // data sharing. Each test is given its own fresh copy of the
  43. // fixture. You cannot expect the data modified by one test to be
  44. // passed on to another test, which is a bad idea.
  45. //
  46. // The reason for this design is that tests should be independent and
  47. // repeatable. In particular, a test should not fail as the result of
  48. // another test's failure. If one test depends on info produced by
  49. // another test, then the two tests should really be one big test.
  50. //
  51. // The macros for indicating the success/failure of a test
  52. // (EXPECT_TRUE, FAIL, etc) need to know what the current test is
  53. // (when Google Test prints the test result, it tells you which test
  54. // each failure belongs to). Technically, these macros invoke a
  55. // member function of the Test class. Therefore, you cannot use them
  56. // in a global function. That's why you should put test sub-routines
  57. // in a test fixture.
  58. //
  59. // </TechnicalDetails>
  60. #include "sample3-inl.h"
  61. #include "gtest/gtest.h"
  62. namespace {
  63. // To use a test fixture, derive a class from testing::Test.
  64. class QueueTestSmpl3 : public testing::Test {
  65. protected: // You should make the members protected s.t. they can be
  66. // accessed from sub-classes.
  67. // virtual void SetUp() will be called before each test is run. You
  68. // should define it if you need to initialize the variables.
  69. // Otherwise, this can be skipped.
  70. virtual void SetUp() {
  71. q1_.Enqueue(1);
  72. q2_.Enqueue(2);
  73. q2_.Enqueue(3);
  74. }
  75. // virtual void TearDown() will be called after each test is run.
  76. // You should define it if there is cleanup work to do. Otherwise,
  77. // you don't have to provide it.
  78. //
  79. // virtual void TearDown() {
  80. // }
  81. // A helper function that some test uses.
  82. static int Double(int n) {
  83. return 2*n;
  84. }
  85. // A helper function for testing Queue::Map().
  86. void MapTester(const Queue<int> * q) {
  87. // Creates a new queue, where each element is twice as big as the
  88. // corresponding one in q.
  89. const Queue<int> * const new_q = q->Map(Double);
  90. // Verifies that the new queue has the same size as q.
  91. ASSERT_EQ(q->Size(), new_q->Size());
  92. // Verifies the relationship between the elements of the two queues.
  93. for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
  94. n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
  95. EXPECT_EQ(2 * n1->element(), n2->element());
  96. }
  97. delete new_q;
  98. }
  99. // Declares the variables your tests want to use.
  100. Queue<int> q0_;
  101. Queue<int> q1_;
  102. Queue<int> q2_;
  103. };
  104. // When you have a test fixture, you define a test using TEST_F
  105. // instead of TEST.
  106. // Tests the default c'tor.
  107. TEST_F(QueueTestSmpl3, DefaultConstructor) {
  108. // You can access data in the test fixture here.
  109. EXPECT_EQ(0u, q0_.Size());
  110. }
  111. // Tests Dequeue().
  112. TEST_F(QueueTestSmpl3, Dequeue) {
  113. int * n = q0_.Dequeue();
  114. EXPECT_TRUE(n == NULL);
  115. n = q1_.Dequeue();
  116. ASSERT_TRUE(n != NULL);
  117. EXPECT_EQ(1, *n);
  118. EXPECT_EQ(0u, q1_.Size());
  119. delete n;
  120. n = q2_.Dequeue();
  121. ASSERT_TRUE(n != NULL);
  122. EXPECT_EQ(2, *n);
  123. EXPECT_EQ(1u, q2_.Size());
  124. delete n;
  125. }
  126. // Tests the Queue::Map() function.
  127. TEST_F(QueueTestSmpl3, Map) {
  128. MapTester(&q0_);
  129. MapTester(&q1_);
  130. MapTester(&q2_);
  131. }
  132. } // namespace