Revision 565, 1.1 kB
(checked in by vbarta, 15 years ago)
|
using own error macros (basically copied from IT++, but never aborting)
|
Line | |
---|
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 | TEST ( test_shared_ptr ) { |
---|
25 | TFooVector v; |
---|
26 | |
---|
27 | bdm::shared_ptr<Foo> zero; |
---|
28 | CHECK_EQUAL ( 0l, zero.use_count() ); |
---|
29 | bdm::shared_ptr<Foo> z2 ( zero ); |
---|
30 | CHECK_EQUAL ( 0l, z2.use_count() ); |
---|
31 | |
---|
32 | bdm::shared_ptr<Foo> one ( new Foo ( 1 ) ); |
---|
33 | v.push_back ( one ); |
---|
34 | CHECK ( !one.unique() ); |
---|
35 | |
---|
36 | v.push_back ( one ); |
---|
37 | v.push_back ( bdm::shared_ptr<Foo> ( new Foo ( 2 ) ) ); |
---|
38 | CHECK_EQUAL ( static_cast<TFooVector::size_type> ( 3 ), v.size() ); |
---|
39 | |
---|
40 | CHECK ( v[0] == v[1] ); |
---|
41 | CHECK ( v[1] != v[2] ); |
---|
42 | |
---|
43 | Foo c ( * ( v[0] ) ); |
---|
44 | CHECK_EQUAL ( 1, c.get_x() ); |
---|
45 | |
---|
46 | Foo *p = v[1].get(); |
---|
47 | CHECK_EQUAL ( 1, p->get_x() ); |
---|
48 | p->set_x ( 12 ); |
---|
49 | CHECK_EQUAL ( 12, one->get_x() ); |
---|
50 | |
---|
51 | CHECK ( v[2].unique() ); |
---|
52 | CHECK_EQUAL ( 2, v[2]->get_x() ); |
---|
53 | } |
---|
54 | |
---|
55 | TEST ( test_shared_ptr_error ) { |
---|
56 | bdm::shared_ptr<Foo> empty; |
---|
57 | try { |
---|
58 | Foo x(*empty); |
---|
59 | CHECK ( false ); |
---|
60 | } catch ( std::runtime_error &exc ) { |
---|
61 | CHECK ( exc.what() ); |
---|
62 | } |
---|
63 | } |
---|