smart_ptr.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Smart Pointer Template Class Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. **
  7. ** This package is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License as
  9. ** published by the Free Software Foundation; either version 2 of
  10. ** the License, or (at your option) any later version.
  11. **
  12. ** This package is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ** General Public License for more details.
  16. **
  17. ** === N O T E S ===========================================================
  18. **
  19. ** ========================================================================= */
  20. #define DEBUGPOINT()
  21. #include <ETL/smart_ptr>
  22. #include <list>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string>
  26. #include <map>
  27. #define NUMBER_OF_OBJECTS 40000
  28. using namespace std;
  29. struct my_test_obj {
  30. static int instance_count;
  31. int my_id;
  32. my_test_obj(int my_id = 0): my_id(my_id)
  33. {
  34. instance_count++;
  35. }
  36. virtual ~my_test_obj()
  37. {
  38. if (instance_count == 0) {
  39. printf("Error, instance count is going past zero!\n");
  40. }
  41. instance_count--;
  42. }
  43. bool operator<(const my_test_obj &rhs)const
  44. {
  45. return my_id < rhs.my_id;
  46. }
  47. };
  48. struct my_other_test_obj : public my_test_obj {
  49. static int instance_count;
  50. my_other_test_obj(int my_id = 0): my_test_obj(my_id)
  51. {
  52. instance_count++;
  53. }
  54. virtual ~my_other_test_obj()
  55. {
  56. if (instance_count == 0) {
  57. printf("Error, instance count is going past zero!\n");
  58. }
  59. instance_count--;
  60. }
  61. };
  62. int my_test_obj::instance_count = 0;
  63. int my_other_test_obj::instance_count = 0;
  64. typedef etl::smart_ptr<my_test_obj> obj_smart_ptr;
  65. typedef etl::smart_ptr<my_other_test_obj> other_obj_smart_ptr;
  66. typedef list< obj_smart_ptr > obj_list;
  67. typedef list< other_obj_smart_ptr > other_obj_list;
  68. int smart_ptr_basic_test(void)
  69. {
  70. printf("smart_ptr: Size of a smart_ptr: %u\n", (unsigned int)sizeof(obj_smart_ptr));
  71. printf("smart_ptr: Size of a reference_counter: %u\n", (unsigned int)sizeof(etl::reference_counter));
  72. printf("smart_ptr: Basic test: ");
  73. my_test_obj::instance_count = 0;
  74. {
  75. etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
  76. }
  77. if (my_test_obj::instance_count != 0) {
  78. printf("FAILED!\n");
  79. printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n", __LINE__, my_test_obj::instance_count);
  80. return 1;
  81. }
  82. {
  83. DEBUGPOINT();
  84. map<string, etl::smart_ptr<my_test_obj> > my_map;
  85. DEBUGPOINT();
  86. etl::smart_ptr<my_test_obj> temp;
  87. temp.spawn();
  88. DEBUGPOINT();
  89. temp.reset();
  90. DEBUGPOINT();
  91. my_map["bleh"] = temp;
  92. DEBUGPOINT();
  93. }
  94. if (my_test_obj::instance_count != 0) {
  95. printf("FAILED!\n");
  96. printf(__FILE__":%d: on create/destroy, instance count=%d, should be zero.\n", __LINE__, my_test_obj::instance_count);
  97. return 1;
  98. }
  99. etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
  100. if (obj_smart_ptr != obj_smart_ptr.constant()) {
  101. printf("FAILED!\n");
  102. printf(__FILE__":%d: on call to smart_ptr<>::constant().\n", __LINE__);
  103. return 1;
  104. }
  105. printf("PASSED\n");
  106. return 0;
  107. }
  108. int smart_ptr_general_use_test(void)
  109. {
  110. printf("smart_ptr: General-use test: ");
  111. my_test_obj::instance_count = 0;
  112. obj_list my_list, my_other_list;
  113. int i;
  114. for (i = 0; i < NUMBER_OF_OBJECTS; i++) {
  115. my_list.push_back(obj_smart_ptr(new my_test_obj(rand())));
  116. }
  117. my_other_list = my_list;
  118. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS) {
  119. printf("FAILED!\n");
  120. printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS);
  121. return 1;
  122. }
  123. my_list.sort();
  124. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS) {
  125. printf("FAILED!\n");
  126. printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS);
  127. return 1;
  128. }
  129. my_list.clear();
  130. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS) {
  131. printf("FAILED!\n");
  132. printf(__FILE__":%d: On copy's clear, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS);
  133. return 1;
  134. }
  135. my_other_list.clear();
  136. if (my_test_obj::instance_count) {
  137. printf("FAILED!\n");
  138. printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n", __LINE__, my_test_obj::instance_count);
  139. return 1;
  140. }
  141. printf("PASSED\n");
  142. return 0;
  143. }
  144. int smart_ptr_inheritance_test(void)
  145. {
  146. printf("smart_ptr: Inheritance test: ");
  147. my_test_obj::instance_count = 0;
  148. my_other_test_obj::instance_count = 0;
  149. other_obj_list my_other_list;
  150. int i;
  151. for (i = 0; i < NUMBER_OF_OBJECTS; i++) {
  152. my_other_list.push_back(other_obj_smart_ptr(new my_other_test_obj(rand())));
  153. }
  154. obj_list my_list(my_other_list.begin(), my_other_list.end());
  155. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS) {
  156. printf("FAILED!\n");
  157. printf(__FILE__":%d: On copy, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS);
  158. return 1;
  159. }
  160. for (i = 0; i < NUMBER_OF_OBJECTS; i++) {
  161. my_list.push_back(other_obj_smart_ptr(new my_other_test_obj(rand())));
  162. }
  163. if (my_other_test_obj::instance_count != NUMBER_OF_OBJECTS * 2 ||
  164. my_test_obj::instance_count != my_other_test_obj::instance_count) {
  165. printf("FAILED!\n");
  166. printf(__FILE__":%d: On inherited copy, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS * 2);
  167. return 1;
  168. }
  169. my_list.sort();
  170. my_other_list.sort();
  171. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS * 2) {
  172. printf("FAILED!\n");
  173. printf(__FILE__":%d: On sort, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS * 2);
  174. return 1;
  175. }
  176. my_list.clear();
  177. if (my_test_obj::instance_count != NUMBER_OF_OBJECTS) {
  178. printf("FAILED!\n");
  179. printf(__FILE__":%d: On clear, instance count=%d, should be %d.\n", __LINE__, my_test_obj::instance_count, NUMBER_OF_OBJECTS);
  180. return 1;
  181. }
  182. my_other_list.clear();
  183. if (my_test_obj::instance_count) {
  184. printf("FAILED!\n");
  185. printf(__FILE__":%d: On clear, instance count=%d, should be zero.\n", __LINE__, my_test_obj::instance_count);
  186. return 1;
  187. }
  188. printf("PASSED\n");
  189. return 0;
  190. }
  191. void test_func(etl::smart_ptr<my_test_obj> smart_ptr __attribute__((unused)))
  192. {
  193. }
  194. int loose_smart_ptr_test(void)
  195. {
  196. printf("smart_ptr: loose_smart_ptr test: ");
  197. my_test_obj::instance_count = 0;
  198. etl::loose_smart_ptr<my_test_obj> obj_smart_ptr_loose;
  199. etl::smart_ptr<my_test_obj> obj_smart_ptr2;
  200. {
  201. etl::smart_ptr<my_test_obj> obj_smart_ptr(new my_test_obj(rand()));
  202. if (my_test_obj::instance_count != 1) {
  203. printf("FAILED!\n");
  204. printf(__FILE__":%d: on smart_ptr assignment from new object, instance count=%d, should be 1.\n", __LINE__, my_test_obj::instance_count);
  205. return 1;
  206. }
  207. obj_smart_ptr_loose = obj_smart_ptr;
  208. if (obj_smart_ptr != obj_smart_ptr_loose) {
  209. printf("FAILED!\n");
  210. printf(__FILE__":%d: on loose_smart_ptr assignment\n", __LINE__);
  211. return 1;
  212. }
  213. obj_smart_ptr2 = obj_smart_ptr_loose;
  214. if (my_test_obj::instance_count != 1) {
  215. printf("FAILED!\n");
  216. printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n", __LINE__, my_test_obj::instance_count);
  217. return 1;
  218. }
  219. test_func(obj_smart_ptr_loose);
  220. if (my_test_obj::instance_count != 1) {
  221. printf("FAILED!\n");
  222. printf(__FILE__":%d: on smart_ptr assignment from loose_smart_ptr, instance count=%d, should be 1.\n", __LINE__, my_test_obj::instance_count);
  223. return 1;
  224. }
  225. }
  226. if (my_test_obj::instance_count != 1) {
  227. printf("FAILED!\n");
  228. printf(__FILE__":%d: on create/destroy, instance count=%d, should be 1.\n", __LINE__, my_test_obj::instance_count);
  229. return 1;
  230. }
  231. printf("PASSED\n");
  232. return 0;
  233. }
  234. int main()
  235. {
  236. int error = 0;
  237. error += smart_ptr_basic_test();
  238. error += smart_ptr_general_use_test();
  239. error += smart_ptr_inheritance_test();
  240. error += loose_smart_ptr_test();
  241. return error;
  242. }