Revision 421, 0.9 kB
(checked in by vbarta, 15 years ago)
|
more tests
|
Line | |
---|
1 | #include "shared_ptr.h" |
---|
2 | #include "UnitTest++.h" |
---|
3 | #include <vector> |
---|
4 | |
---|
5 | class Foo |
---|
6 | { |
---|
7 | private: |
---|
8 | int x; |
---|
9 | |
---|
10 | public: |
---|
11 | Foo(int x):x(x) { } |
---|
12 | |
---|
13 | int get_x() const { return x; } |
---|
14 | |
---|
15 | void set_x(int nx) { x = nx; } |
---|
16 | }; |
---|
17 | |
---|
18 | typedef std::vector<bdm::shared_ptr<Foo> > TFooVector; |
---|
19 | |
---|
20 | TEST(test_shared_ptr) |
---|
21 | { |
---|
22 | TFooVector v; |
---|
23 | |
---|
24 | bdm::shared_ptr<Foo> zero; |
---|
25 | CHECK_EQUAL(0l, zero.use_count()); |
---|
26 | bdm::shared_ptr<Foo> z2(zero); |
---|
27 | CHECK_EQUAL(0l, z2.use_count()); |
---|
28 | |
---|
29 | bdm::shared_ptr<Foo> one(new Foo(1)); |
---|
30 | v.push_back(one); |
---|
31 | CHECK(!one.unique()); |
---|
32 | |
---|
33 | v.push_back(one); |
---|
34 | v.push_back(bdm::shared_ptr<Foo>(new Foo(2))); |
---|
35 | CHECK_EQUAL(static_cast<TFooVector::size_type>(3), v.size()); |
---|
36 | |
---|
37 | CHECK(v[0] == v[1]); |
---|
38 | CHECK(v[1] != v[2]); |
---|
39 | |
---|
40 | Foo c(*(v[0])); |
---|
41 | CHECK_EQUAL(1, c.get_x()); |
---|
42 | |
---|
43 | Foo *p = v[1].get(); |
---|
44 | CHECK_EQUAL(1, p->get_x()); |
---|
45 | p->set_x(12); |
---|
46 | CHECK_EQUAL(12, one->get_x()); |
---|
47 | |
---|
48 | CHECK(v[2].unique()); |
---|
49 | CHECK_EQUAL(2, v[2]->get_x()); |
---|
50 | } |
---|