| 1 | // constructor_tests.cpp -- The Boost Lambda Library ------------------ |
|---|
| 2 | // |
|---|
| 3 | // Copyright (C) 2000-2003 Jaakko Järvi (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 | // ----------------------------------------------------------------------- |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <boost/test/minimal.hpp> // see "Header Implementation Option" |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include "boost/lambda/lambda.hpp" |
|---|
| 19 | #include "boost/lambda/bind.hpp" |
|---|
| 20 | |
|---|
| 21 | #include "boost/lambda/construct.hpp" |
|---|
| 22 | |
|---|
| 23 | #include <iostream> |
|---|
| 24 | #include <algorithm> |
|---|
| 25 | #include <vector> |
|---|
| 26 | |
|---|
| 27 | using namespace boost::lambda; |
|---|
| 28 | using namespace std; |
|---|
| 29 | |
|---|
| 30 | template<class T> |
|---|
| 31 | bool check_tuple(int n, const T& t) |
|---|
| 32 | { |
|---|
| 33 | return (t.get_head() == n) && check_tuple(n+1, t.get_tail()); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | template <> |
|---|
| 37 | bool check_tuple(int n, const null_type& ) { return true; } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | void constructor_all_lengths() |
|---|
| 41 | { |
|---|
| 42 | bool ok; |
|---|
| 43 | ok = check_tuple( |
|---|
| 44 | 1, |
|---|
| 45 | bind(constructor<tuple<int> >(), |
|---|
| 46 | 1)() |
|---|
| 47 | ); |
|---|
| 48 | BOOST_CHECK(ok); |
|---|
| 49 | |
|---|
| 50 | ok = check_tuple( |
|---|
| 51 | 1, |
|---|
| 52 | bind(constructor<tuple<int, int> >(), |
|---|
| 53 | 1, 2)() |
|---|
| 54 | ); |
|---|
| 55 | BOOST_CHECK(ok); |
|---|
| 56 | |
|---|
| 57 | ok = check_tuple( |
|---|
| 58 | 1, |
|---|
| 59 | bind(constructor<tuple<int, int, int> >(), |
|---|
| 60 | 1, 2, 3)() |
|---|
| 61 | ); |
|---|
| 62 | BOOST_CHECK(ok); |
|---|
| 63 | |
|---|
| 64 | ok = check_tuple( |
|---|
| 65 | 1, |
|---|
| 66 | bind(constructor<tuple<int, int, int, int> >(), |
|---|
| 67 | 1, 2, 3, 4)() |
|---|
| 68 | ); |
|---|
| 69 | BOOST_CHECK(ok); |
|---|
| 70 | |
|---|
| 71 | ok = check_tuple( |
|---|
| 72 | 1, |
|---|
| 73 | bind(constructor<tuple<int, int, int, int, int> >(), |
|---|
| 74 | 1, 2, 3, 4, 5)() |
|---|
| 75 | ); |
|---|
| 76 | BOOST_CHECK(ok); |
|---|
| 77 | |
|---|
| 78 | ok = check_tuple( |
|---|
| 79 | 1, |
|---|
| 80 | bind(constructor<tuple<int, int, int, int, int, int> >(), |
|---|
| 81 | 1, 2, 3, 4, 5, 6)() |
|---|
| 82 | ); |
|---|
| 83 | BOOST_CHECK(ok); |
|---|
| 84 | |
|---|
| 85 | ok = check_tuple( |
|---|
| 86 | 1, |
|---|
| 87 | bind(constructor<tuple<int, int, int, int, int, int, int> >(), |
|---|
| 88 | 1, 2, 3, 4, 5, 6, 7)() |
|---|
| 89 | ); |
|---|
| 90 | BOOST_CHECK(ok); |
|---|
| 91 | |
|---|
| 92 | ok = check_tuple( |
|---|
| 93 | 1, |
|---|
| 94 | bind(constructor<tuple<int, int, int, int, int, int, int, int> >(), |
|---|
| 95 | 1, 2, 3, 4, 5, 6, 7, 8)() |
|---|
| 96 | ); |
|---|
| 97 | BOOST_CHECK(ok); |
|---|
| 98 | |
|---|
| 99 | ok = check_tuple( |
|---|
| 100 | 1, |
|---|
| 101 | bind(constructor<tuple<int, int, int, int, int, int, int, int, int> >(), |
|---|
| 102 | 1, 2, 3, 4, 5, 6, 7, 8, 9)() |
|---|
| 103 | ); |
|---|
| 104 | BOOST_CHECK(ok); |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void new_ptr_all_lengths() |
|---|
| 109 | { |
|---|
| 110 | bool ok; |
|---|
| 111 | ok = check_tuple( |
|---|
| 112 | 1, |
|---|
| 113 | *(bind(new_ptr<tuple<int> >(), |
|---|
| 114 | 1))() |
|---|
| 115 | ); |
|---|
| 116 | BOOST_CHECK(ok); |
|---|
| 117 | |
|---|
| 118 | ok = check_tuple( |
|---|
| 119 | 1, |
|---|
| 120 | *(bind(new_ptr<tuple<int, int> >(), |
|---|
| 121 | 1, 2))() |
|---|
| 122 | ); |
|---|
| 123 | BOOST_CHECK(ok); |
|---|
| 124 | |
|---|
| 125 | ok = check_tuple( |
|---|
| 126 | 1, |
|---|
| 127 | *(bind(new_ptr<tuple<int, int, int> >(), |
|---|
| 128 | 1, 2, 3))() |
|---|
| 129 | ); |
|---|
| 130 | BOOST_CHECK(ok); |
|---|
| 131 | |
|---|
| 132 | ok = check_tuple( |
|---|
| 133 | 1, |
|---|
| 134 | *(bind(new_ptr<tuple<int, int, int, int> >(), |
|---|
| 135 | 1, 2, 3, 4))() |
|---|
| 136 | ); |
|---|
| 137 | BOOST_CHECK(ok); |
|---|
| 138 | |
|---|
| 139 | ok = check_tuple( |
|---|
| 140 | 1, |
|---|
| 141 | *(bind(new_ptr<tuple<int, int, int, int, int> >(), |
|---|
| 142 | 1, 2, 3, 4, 5))() |
|---|
| 143 | ); |
|---|
| 144 | BOOST_CHECK(ok); |
|---|
| 145 | |
|---|
| 146 | ok = check_tuple( |
|---|
| 147 | 1, |
|---|
| 148 | *(bind(new_ptr<tuple<int, int, int, int, int, int> >(), |
|---|
| 149 | 1, 2, 3, 4, 5, 6))() |
|---|
| 150 | ); |
|---|
| 151 | BOOST_CHECK(ok); |
|---|
| 152 | |
|---|
| 153 | ok = check_tuple( |
|---|
| 154 | 1, |
|---|
| 155 | *(bind(new_ptr<tuple<int, int, int, int, int, int, int> >(), |
|---|
| 156 | 1, 2, 3, 4, 5, 6, 7))() |
|---|
| 157 | ); |
|---|
| 158 | BOOST_CHECK(ok); |
|---|
| 159 | |
|---|
| 160 | ok = check_tuple( |
|---|
| 161 | 1, |
|---|
| 162 | *(bind(new_ptr<tuple<int, int, int, int, int, int, int, int> >(), |
|---|
| 163 | 1, 2, 3, 4, 5, 6, 7, 8))() |
|---|
| 164 | ); |
|---|
| 165 | BOOST_CHECK(ok); |
|---|
| 166 | |
|---|
| 167 | ok = check_tuple( |
|---|
| 168 | 1, |
|---|
| 169 | *(bind(new_ptr<tuple<int, int, int, int, int, int, int, int, int> >(), |
|---|
| 170 | 1, 2, 3, 4, 5, 6, 7, 8, 9))() |
|---|
| 171 | ); |
|---|
| 172 | BOOST_CHECK(ok); |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | class is_destructor_called { |
|---|
| 177 | bool& b; |
|---|
| 178 | public: |
|---|
| 179 | is_destructor_called(bool& bb) : b(bb) { b = false; } |
|---|
| 180 | ~is_destructor_called() { b = true; } |
|---|
| 181 | }; |
|---|
| 182 | |
|---|
| 183 | void test_destructor () |
|---|
| 184 | { |
|---|
| 185 | char space[sizeof(is_destructor_called)]; |
|---|
| 186 | bool flag; |
|---|
| 187 | |
|---|
| 188 | is_destructor_called* idc = new(space) is_destructor_called(flag); |
|---|
| 189 | BOOST_CHECK(flag == false); |
|---|
| 190 | bind(destructor(), _1)(idc); |
|---|
| 191 | BOOST_CHECK(flag == true); |
|---|
| 192 | |
|---|
| 193 | idc = new(space) is_destructor_called(flag); |
|---|
| 194 | BOOST_CHECK(flag == false); |
|---|
| 195 | bind(destructor(), _1)(*idc); |
|---|
| 196 | BOOST_CHECK(flag == true); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | class count_deletes { |
|---|
| 201 | public: |
|---|
| 202 | static int count; |
|---|
| 203 | ~count_deletes() { ++count; } |
|---|
| 204 | }; |
|---|
| 205 | |
|---|
| 206 | int count_deletes::count = 0; |
|---|
| 207 | |
|---|
| 208 | void test_news_and_deletes () |
|---|
| 209 | { |
|---|
| 210 | int* i[10]; |
|---|
| 211 | for_each(i, i+10, _1 = bind(new_ptr<int>(), 2)); |
|---|
| 212 | int count_errors = 0; |
|---|
| 213 | |
|---|
| 214 | for_each(i, i+10, (*_1 == 2) || ++var(count_errors)); |
|---|
| 215 | BOOST_CHECK(count_errors == 0); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | count_deletes* ct[10]; |
|---|
| 219 | for_each(ct, ct+10, _1 = bind(new_ptr<count_deletes>())); |
|---|
| 220 | count_deletes::count = 0; |
|---|
| 221 | for_each(ct, ct+10, bind(delete_ptr(), _1)); |
|---|
| 222 | BOOST_CHECK(count_deletes::count == 10); |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | void test_array_new_and_delete() |
|---|
| 227 | { |
|---|
| 228 | count_deletes* c; |
|---|
| 229 | (_1 = bind(new_array<count_deletes>(), 5))(c); |
|---|
| 230 | count_deletes::count = 0; |
|---|
| 231 | |
|---|
| 232 | bind(delete_array(), _1)(c); |
|---|
| 233 | BOOST_CHECK(count_deletes::count == 5); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | void delayed_construction() |
|---|
| 238 | { |
|---|
| 239 | vector<int> x(3); |
|---|
| 240 | vector<int> y(3); |
|---|
| 241 | |
|---|
| 242 | fill(x.begin(), x.end(), 0); |
|---|
| 243 | fill(y.begin(), y.end(), 1); |
|---|
| 244 | |
|---|
| 245 | vector<pair<int, int> > v; |
|---|
| 246 | |
|---|
| 247 | transform(x.begin(), x.end(), y.begin(), back_inserter(v), |
|---|
| 248 | bind(constructor<pair<int, int> >(), _1, _2) ); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | int test_main(int, char *[]) { |
|---|
| 252 | |
|---|
| 253 | constructor_all_lengths(); |
|---|
| 254 | new_ptr_all_lengths(); |
|---|
| 255 | delayed_construction(); |
|---|
| 256 | test_destructor(); |
|---|
| 257 | test_news_and_deletes(); |
|---|
| 258 | test_array_new_and_delete(); |
|---|
| 259 | |
|---|
| 260 | return 0; |
|---|
| 261 | } |
|---|