constructor_tests.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // constructor_tests.cpp -- The Boost Lambda Library ------------------
  2. //
  3. // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  4. // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // For more information, see www.boost.org
  11. // -----------------------------------------------------------------------
  12. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. #include "boost/lambda/lambda.hpp"
  14. #include "boost/lambda/bind.hpp"
  15. #include "boost/lambda/construct.hpp"
  16. #include <iostream>
  17. #include <algorithm>
  18. #include <vector>
  19. #ifdef BOOST_MSVC
  20. #pragma warning(disable:4512)
  21. #endif
  22. using namespace boost::lambda;
  23. namespace bl = boost::lambda;
  24. template<class T>
  25. bool check_tuple(int n, const T& t)
  26. {
  27. return (t.get_head() == n) && check_tuple(n+1, t.get_tail());
  28. }
  29. template <>
  30. bool check_tuple(int /*n*/, const null_type& ) { return true; }
  31. void constructor_all_lengths()
  32. {
  33. bool ok;
  34. ok = check_tuple(
  35. 1,
  36. bind(constructor<tuple<int> >(),
  37. 1)()
  38. );
  39. BOOST_CHECK(ok);
  40. ok = check_tuple(
  41. 1,
  42. bind(constructor<tuple<int, int> >(),
  43. 1, 2)()
  44. );
  45. BOOST_CHECK(ok);
  46. ok = check_tuple(
  47. 1,
  48. bind(constructor<tuple<int, int, int> >(),
  49. 1, 2, 3)()
  50. );
  51. BOOST_CHECK(ok);
  52. ok = check_tuple(
  53. 1,
  54. bind(constructor<tuple<int, int, int, int> >(),
  55. 1, 2, 3, 4)()
  56. );
  57. BOOST_CHECK(ok);
  58. ok = check_tuple(
  59. 1,
  60. bind(constructor<tuple<int, int, int, int, int> >(),
  61. 1, 2, 3, 4, 5)()
  62. );
  63. BOOST_CHECK(ok);
  64. ok = check_tuple(
  65. 1,
  66. bind(constructor<tuple<int, int, int, int, int, int> >(),
  67. 1, 2, 3, 4, 5, 6)()
  68. );
  69. BOOST_CHECK(ok);
  70. ok = check_tuple(
  71. 1,
  72. bind(constructor<tuple<int, int, int, int, int, int, int> >(),
  73. 1, 2, 3, 4, 5, 6, 7)()
  74. );
  75. BOOST_CHECK(ok);
  76. ok = check_tuple(
  77. 1,
  78. bind(constructor<tuple<int, int, int, int, int, int, int, int> >(),
  79. 1, 2, 3, 4, 5, 6, 7, 8)()
  80. );
  81. BOOST_CHECK(ok);
  82. ok = check_tuple(
  83. 1,
  84. bind(constructor<tuple<int, int, int, int, int, int, int, int, int> >(),
  85. 1, 2, 3, 4, 5, 6, 7, 8, 9)()
  86. );
  87. BOOST_CHECK(ok);
  88. }
  89. void new_ptr_all_lengths()
  90. {
  91. bool ok;
  92. ok = check_tuple(
  93. 1,
  94. *(bind(new_ptr<tuple<int> >(),
  95. 1))()
  96. );
  97. BOOST_CHECK(ok);
  98. ok = check_tuple(
  99. 1,
  100. *(bind(new_ptr<tuple<int, int> >(),
  101. 1, 2))()
  102. );
  103. BOOST_CHECK(ok);
  104. ok = check_tuple(
  105. 1,
  106. *(bind(new_ptr<tuple<int, int, int> >(),
  107. 1, 2, 3))()
  108. );
  109. BOOST_CHECK(ok);
  110. ok = check_tuple(
  111. 1,
  112. *(bind(new_ptr<tuple<int, int, int, int> >(),
  113. 1, 2, 3, 4))()
  114. );
  115. BOOST_CHECK(ok);
  116. ok = check_tuple(
  117. 1,
  118. *(bind(new_ptr<tuple<int, int, int, int, int> >(),
  119. 1, 2, 3, 4, 5))()
  120. );
  121. BOOST_CHECK(ok);
  122. ok = check_tuple(
  123. 1,
  124. *(bind(new_ptr<tuple<int, int, int, int, int, int> >(),
  125. 1, 2, 3, 4, 5, 6))()
  126. );
  127. BOOST_CHECK(ok);
  128. ok = check_tuple(
  129. 1,
  130. *(bind(new_ptr<tuple<int, int, int, int, int, int, int> >(),
  131. 1, 2, 3, 4, 5, 6, 7))()
  132. );
  133. BOOST_CHECK(ok);
  134. ok = check_tuple(
  135. 1,
  136. *(bind(new_ptr<tuple<int, int, int, int, int, int, int, int> >(),
  137. 1, 2, 3, 4, 5, 6, 7, 8))()
  138. );
  139. BOOST_CHECK(ok);
  140. ok = check_tuple(
  141. 1,
  142. *(bind(new_ptr<tuple<int, int, int, int, int, int, int, int, int> >(),
  143. 1, 2, 3, 4, 5, 6, 7, 8, 9))()
  144. );
  145. BOOST_CHECK(ok);
  146. }
  147. class is_destructor_called {
  148. bool& b;
  149. public:
  150. is_destructor_called(bool& bb) : b(bb) { b = false; }
  151. ~is_destructor_called() { b = true; }
  152. };
  153. void test_destructor ()
  154. {
  155. char space[sizeof(is_destructor_called)];
  156. bool flag = false;
  157. is_destructor_called* idc = new(space) is_destructor_called(flag);
  158. BOOST_CHECK(flag == false);
  159. bind(destructor(), _1)(idc);
  160. BOOST_CHECK(flag == true);
  161. idc = new(space) is_destructor_called(flag);
  162. BOOST_CHECK(flag == false);
  163. bind(destructor(), _1)(*idc);
  164. BOOST_CHECK(flag == true);
  165. }
  166. class count_deletes {
  167. public:
  168. static int count;
  169. ~count_deletes() { ++count; }
  170. };
  171. int count_deletes::count = 0;
  172. void test_news_and_deletes ()
  173. {
  174. int* i[10];
  175. std::for_each(i, i+10, _1 = bind(new_ptr<int>(), 2));
  176. int count_errors = 0;
  177. std::for_each(i, i+10, (*_1 == 2) || ++var(count_errors));
  178. BOOST_CHECK(count_errors == 0);
  179. count_deletes* ct[10];
  180. std::for_each(ct, ct+10, _1 = bind(new_ptr<count_deletes>()));
  181. count_deletes::count = 0;
  182. std::for_each(ct, ct+10, bind(delete_ptr(), _1));
  183. BOOST_CHECK(count_deletes::count == 10);
  184. }
  185. void test_array_new_and_delete()
  186. {
  187. count_deletes* c;
  188. (_1 = bind(new_array<count_deletes>(), 5))(c);
  189. count_deletes::count = 0;
  190. bind(delete_array(), _1)(c);
  191. BOOST_CHECK(count_deletes::count == 5);
  192. }
  193. void delayed_construction()
  194. {
  195. std::vector<int> x(3);
  196. std::vector<int> y(3);
  197. std::fill(x.begin(), x.end(), 0);
  198. std::fill(y.begin(), y.end(), 1);
  199. std::vector<std::pair<int, int> > v;
  200. std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(v),
  201. bl::bind(constructor<std::pair<int, int> >(), _1, _2) );
  202. }
  203. int test_main(int, char *[]) {
  204. constructor_all_lengths();
  205. new_ptr_all_lengths();
  206. delayed_construction();
  207. test_destructor();
  208. test_news_and_deletes();
  209. test_array_new_and_delete();
  210. return 0;
  211. }