1 | // Copyright 2003 The Trustees of Indiana University. |
---|---|
2 | |
3 | // Use, modification and distribution is subject to the Boost Software |
4 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
5 | // http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | // Shared container iterator adaptor |
8 | // Author: Ronald Garcia |
9 | // See http://boost.org/libs/utility/shared_container_iterator.html |
10 | // for documentation. |
11 | |
12 | // |
13 | // shared_iterator_test.cpp - Regression tests for shared_container_iterator. |
14 | // |
15 | |
16 | |
17 | #include "boost/shared_container_iterator.hpp" |
18 | #include "boost/shared_ptr.hpp" |
19 | #include <vector> |
20 | #include <cassert> |
21 | |
22 | struct resource { |
23 | static int count; |
24 | resource() { ++count; } |
25 | resource(resource const&) { ++count; } |
26 | ~resource() { --count; } |
27 | }; |
28 | int resource::count = 0; |
29 | |
30 | typedef std::vector<resource> resources_t; |
31 | |
32 | typedef boost::shared_container_iterator< resources_t > iterator; |
33 | |
34 | |
35 | void set_range(iterator& i, iterator& end) { |
36 | |
37 | boost::shared_ptr< resources_t > objs(new resources_t()); |
38 | |
39 | for (int j = 0; j != 6; ++j) |
40 | objs->push_back(x: resource()); |
41 | |
42 | i = iterator(objs->begin(),objs); |
43 | end = iterator(objs->end(),objs); |
44 | assert(resource::count == 6); |
45 | } |
46 | |
47 | |
48 | int main() { |
49 | |
50 | assert(resource::count == 0); |
51 | |
52 | { |
53 | iterator i; |
54 | { |
55 | iterator end; |
56 | set_range(i,end); |
57 | assert(resource::count == 6); |
58 | } |
59 | assert(resource::count == 6); |
60 | } |
61 | assert(resource::count == 0); |
62 | |
63 | return 0; |
64 | } |
65 |