googletest-linked-ptr-test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2003, 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. #include <stdlib.h>
  30. #include "gtest/internal/gtest-linked_ptr.h"
  31. #include "gtest/gtest.h"
  32. namespace {
  33. using testing::Message;
  34. using testing::internal::linked_ptr;
  35. int num;
  36. Message* history = NULL;
  37. // Class which tracks allocation/deallocation
  38. class A {
  39. public:
  40. A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; }
  41. virtual ~A() { *history << "A" << mynum << " dtor\n"; }
  42. virtual void Use() { *history << "A" << mynum << " use\n"; }
  43. protected:
  44. int mynum;
  45. };
  46. // Subclass
  47. class B : public A {
  48. public:
  49. B() { *history << "B" << mynum << " ctor\n"; }
  50. ~B() { *history << "B" << mynum << " dtor\n"; }
  51. virtual void Use() { *history << "B" << mynum << " use\n"; }
  52. };
  53. class LinkedPtrTest : public testing::Test {
  54. public:
  55. LinkedPtrTest() {
  56. num = 0;
  57. history = new Message;
  58. }
  59. virtual ~LinkedPtrTest() {
  60. delete history;
  61. history = NULL;
  62. }
  63. };
  64. TEST_F(LinkedPtrTest, GeneralTest) {
  65. {
  66. linked_ptr<A> a0, a1, a2;
  67. // Use explicit function call notation here to suppress self-assign warning.
  68. a0.operator=(a0);
  69. a1 = a2;
  70. ASSERT_EQ(a0.get(), static_cast<A*>(NULL));
  71. ASSERT_EQ(a1.get(), static_cast<A*>(NULL));
  72. ASSERT_EQ(a2.get(), static_cast<A*>(NULL));
  73. ASSERT_TRUE(a0 == NULL);
  74. ASSERT_TRUE(a1 == NULL);
  75. ASSERT_TRUE(a2 == NULL);
  76. {
  77. linked_ptr<A> a3(new A);
  78. a0 = a3;
  79. ASSERT_TRUE(a0 == a3);
  80. ASSERT_TRUE(a0 != NULL);
  81. ASSERT_TRUE(a0.get() == a3);
  82. ASSERT_TRUE(a0 == a3.get());
  83. linked_ptr<A> a4(a0);
  84. a1 = a4;
  85. linked_ptr<A> a5(new A);
  86. ASSERT_TRUE(a5.get() != a3);
  87. ASSERT_TRUE(a5 != a3.get());
  88. a2 = a5;
  89. linked_ptr<B> b0(new B);
  90. linked_ptr<A> a6(b0);
  91. ASSERT_TRUE(b0 == a6);
  92. ASSERT_TRUE(a6 == b0);
  93. ASSERT_TRUE(b0 != NULL);
  94. a5 = b0;
  95. a5 = b0;
  96. a3->Use();
  97. a4->Use();
  98. a5->Use();
  99. a6->Use();
  100. b0->Use();
  101. (*b0).Use();
  102. b0.get()->Use();
  103. }
  104. a0->Use();
  105. a1->Use();
  106. a2->Use();
  107. a1 = a2;
  108. a2.reset(new A);
  109. a0.reset();
  110. linked_ptr<A> a7;
  111. }
  112. ASSERT_STREQ(
  113. "A0 ctor\n"
  114. "A1 ctor\n"
  115. "A2 ctor\n"
  116. "B2 ctor\n"
  117. "A0 use\n"
  118. "A0 use\n"
  119. "B2 use\n"
  120. "B2 use\n"
  121. "B2 use\n"
  122. "B2 use\n"
  123. "B2 use\n"
  124. "B2 dtor\n"
  125. "A2 dtor\n"
  126. "A0 use\n"
  127. "A0 use\n"
  128. "A1 use\n"
  129. "A3 ctor\n"
  130. "A0 dtor\n"
  131. "A3 dtor\n"
  132. "A1 dtor\n",
  133. history->GetString().c_str());
  134. }
  135. } // Unnamed namespace