1 | #include "shared_ptr.h" |
---|
2 | #include "UnitTest++.h" |
---|
3 | #include <stdexcept> |
---|
4 | #include <vector> |
---|
5 | |
---|
6 | class Foo { |
---|
7 | private: |
---|
8 | int x; |
---|
9 | |
---|
10 | public: |
---|
11 | Foo ( int x ) : x ( x ) { } |
---|
12 | |
---|
13 | int get_x() const { |
---|
14 | return x; |
---|
15 | } |
---|
16 | |
---|
17 | void set_x ( int nx ) { |
---|
18 | x = nx; |
---|
19 | } |
---|
20 | }; |
---|
21 | |
---|
22 | typedef std::vector<bdm::shared_ptr<Foo> > TFooVector; |
---|
23 | |
---|
24 | typedef std::vector<bdm::shared_ptr<const Foo> > TConstFooVector; |
---|
25 | |
---|
26 | TEST ( test_shared_ptr ) { |
---|
27 | TFooVector v; |
---|
28 | |
---|
29 | bdm::shared_ptr<Foo> zero; |
---|
30 | CHECK_EQUAL ( 0l, zero.use_count() ); |
---|
31 | bdm::shared_ptr<Foo> z2 ( zero ); |
---|
32 | CHECK_EQUAL ( 0l, z2.use_count() ); |
---|
33 | |
---|
34 | bdm::shared_ptr<Foo> one ( new Foo ( 1 ) ); |
---|
35 | v.push_back ( one ); |
---|
36 | CHECK ( !one.unique() ); |
---|
37 | |
---|
38 | v.push_back ( one ); |
---|
39 | v.push_back ( bdm::shared_ptr<Foo> ( new Foo ( 2 ) ) ); |
---|
40 | CHECK_EQUAL ( static_cast<TFooVector::size_type> ( 3 ), v.size() ); |
---|
41 | |
---|
42 | CHECK ( v[0] == v[1] ); |
---|
43 | CHECK ( v[1] != v[2] ); |
---|
44 | |
---|
45 | Foo c ( * ( v[0] ) ); |
---|
46 | CHECK_EQUAL ( 1, c.get_x() ); |
---|
47 | |
---|
48 | Foo *p = v[1].get(); |
---|
49 | CHECK_EQUAL ( 1, p->get_x() ); |
---|
50 | p->set_x ( 12 ); |
---|
51 | CHECK_EQUAL ( 12, one->get_x() ); |
---|
52 | |
---|
53 | CHECK ( v[2].unique() ); |
---|
54 | CHECK_EQUAL ( 2, v[2]->get_x() ); |
---|
55 | } |
---|
56 | |
---|
57 | TEST ( test_shared_ptr_const ) { |
---|
58 | TConstFooVector v; |
---|
59 | |
---|
60 | bdm::shared_ptr<const Foo> zero; |
---|
61 | CHECK_EQUAL ( 0l, zero.use_count() ); |
---|
62 | bdm::shared_ptr<const Foo> z2 ( zero ); |
---|
63 | CHECK_EQUAL ( 0l, z2.use_count() ); |
---|
64 | |
---|
65 | bdm::shared_ptr<const Foo> one ( new Foo ( 1 ) ); |
---|
66 | v.push_back ( one ); |
---|
67 | CHECK ( !one.unique() ); |
---|
68 | |
---|
69 | v.push_back ( one ); |
---|
70 | v.push_back ( bdm::shared_ptr<const Foo> ( new Foo ( 2 ) ) ); |
---|
71 | CHECK_EQUAL ( static_cast<TConstFooVector::size_type> ( 3 ), v.size() ); |
---|
72 | |
---|
73 | CHECK ( v[0] == v[1] ); |
---|
74 | CHECK ( v[1] != v[2] ); |
---|
75 | |
---|
76 | Foo c ( * ( v[0] ) ); |
---|
77 | CHECK_EQUAL ( 1, c.get_x() ); |
---|
78 | |
---|
79 | const Foo *p = v[1].get(); |
---|
80 | CHECK_EQUAL ( 1, p->get_x() ); |
---|
81 | |
---|
82 | CHECK ( v[2].unique() ); |
---|
83 | CHECK_EQUAL ( 2, v[2]->get_x() ); |
---|
84 | |
---|
85 | bdm::shared_ptr<Foo> non_const; |
---|
86 | bdm::shared_ptr<const Foo> another ( non_const ); |
---|
87 | CHECK ( !non_const.unique() ); |
---|
88 | } |
---|
89 | |
---|
90 | TEST ( test_shared_ptr_error ) { |
---|
91 | bdm::shared_ptr<Foo> empty; |
---|
92 | try { |
---|
93 | Foo x(*empty); |
---|
94 | CHECK ( false ); |
---|
95 | } catch ( std::runtime_error &exc ) { |
---|
96 | CHECK ( exc.what() ); |
---|
97 | } |
---|
98 | } |
---|