testAutoPtr.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifdef __BORLANDC__
  11. # pragma warn -8027 /* 'for' not inlined. */
  12. # pragma warn -8026 /* exception not inlined. */
  13. #endif
  14. #include "kwsysPrivate.h"
  15. #include KWSYS_HEADER(auto_ptr.hxx)
  16. // Work-around CMake dependency scanning limitation. This must
  17. // duplicate the above list of headers.
  18. #if 0
  19. # include "auto_ptr.hxx.in"
  20. #endif
  21. #include <stdio.h>
  22. #define ASSERT(x,y) if (!(x)) { printf("FAIL: " y "\n"); status = 1; }
  23. int instances = 0; // don't declare as static
  24. struct A
  25. {
  26. A() { ++instances; }
  27. ~A() { --instances; }
  28. A* self() {return this; }
  29. };
  30. struct B: public A {};
  31. static int function_call(kwsys::auto_ptr<A> a)
  32. {
  33. return a.get()? 1:0;
  34. }
  35. static A* get_A(A& a) { return &a; }
  36. static kwsys::auto_ptr<A> generate_auto_ptr_A()
  37. {
  38. return kwsys::auto_ptr<A>(new A);
  39. }
  40. static kwsys::auto_ptr<B> generate_auto_ptr_B()
  41. {
  42. return kwsys::auto_ptr<B>(new B);
  43. }
  44. int testAutoPtr(int, char*[])
  45. {
  46. int status = 0;
  47. // Keep everything in a subscope so we can detect leaks.
  48. {
  49. kwsys::auto_ptr<A> pa0;
  50. kwsys::auto_ptr<A> pa1(new A());
  51. kwsys::auto_ptr<B> pb1(new B());
  52. kwsys::auto_ptr<B> pb2(new B());
  53. kwsys::auto_ptr<A> pa2(new B());
  54. A* ptr = get_A(*pa1);
  55. ASSERT(ptr == pa1.get(),
  56. "auto_ptr does not return correct object when dereferenced");
  57. ptr = pa1->self();
  58. ASSERT(ptr == pa1.get(),
  59. "auto_ptr does not return correct pointer from operator->");
  60. A* before = pa0.get();
  61. pa0.reset(new A());
  62. ASSERT(pa0.get() && pa0.get() != before,
  63. "auto_ptr empty after reset(new A())");
  64. before = pa0.get();
  65. pa0.reset(new B());
  66. ASSERT(pa0.get() && pa0.get() != before,
  67. "auto_ptr empty after reset(new B())");
  68. delete pa0.release();
  69. ASSERT(!pa0.get(), "auto_ptr holds an object after release()");
  70. kwsys::auto_ptr<A> pa3(pb1);
  71. ASSERT(!pb1.get(),
  72. "auto_ptr full after being used to construct another");
  73. ASSERT(pa3.get(),
  74. "auto_ptr empty after construction from another");
  75. {
  76. kwsys::auto_ptr<A> pa;
  77. pa = pa3;
  78. ASSERT(!pa3.get(),
  79. "auto_ptr full after assignment to another");
  80. ASSERT(pa.get(),
  81. "auto_ptr empty after assignment from another");
  82. }
  83. {
  84. kwsys::auto_ptr<A> pa;
  85. pa = pb2;
  86. ASSERT(!pb2.get(),
  87. "auto_ptr full after assignment to compatible");
  88. ASSERT(pa.get(),
  89. "auto_ptr empty after assignment from compatible");
  90. }
  91. {
  92. int receive = function_call(pa2);
  93. ASSERT(receive,
  94. "auto_ptr did not receive ownership in called function");
  95. ASSERT(!pa2.get(),
  96. "auto_ptr did not release ownership to called function");
  97. }
  98. {
  99. int received = function_call(generate_auto_ptr_A());
  100. ASSERT(received,
  101. "auto_ptr in called function did not take ownership "
  102. "from factory function");
  103. }
  104. #if 0
  105. // Is this allowed by the standard?
  106. {
  107. int received = function_call(generate_auto_ptr_B());
  108. ASSERT(received,
  109. "auto_ptr in called function did not take ownership "
  110. "from factory function with conversion");
  111. }
  112. #endif
  113. {
  114. kwsys::auto_ptr<A> pa(generate_auto_ptr_A());
  115. ASSERT(pa.get(),
  116. "auto_ptr empty after construction from factory function");
  117. }
  118. {
  119. kwsys::auto_ptr<A> pa;
  120. pa = generate_auto_ptr_A();
  121. ASSERT(pa.get(),
  122. "auto_ptr empty after assignment from factory function");
  123. }
  124. {
  125. kwsys::auto_ptr<A> pa(generate_auto_ptr_B());
  126. ASSERT(pa.get(),
  127. "auto_ptr empty after construction from compatible factory function");
  128. }
  129. {
  130. kwsys::auto_ptr<A> pa;
  131. pa = generate_auto_ptr_B();
  132. ASSERT(pa.get(),
  133. "auto_ptr empty after assignment from compatible factory function");
  134. }
  135. }
  136. ASSERT(instances == 0, "auto_ptr leaked an object");
  137. return status;
  138. }