You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.0 KiB

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