testAutoPtr.cxx 4.2 KB

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