1 | // |
---|---|
2 | // AutoReleasePoolTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "AutoReleasePoolTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/AutoReleasePool.h" |
15 | |
16 | |
17 | using Poco::AutoReleasePool; |
18 | |
19 | |
20 | namespace |
21 | { |
22 | class TestObj |
23 | { |
24 | public: |
25 | TestObj(): _rc(1) |
26 | { |
27 | ++_count; |
28 | } |
29 | |
30 | void duplicate() |
31 | { |
32 | ++_rc; |
33 | } |
34 | |
35 | void release() |
36 | { |
37 | if (--_rc == 0) |
38 | delete this; |
39 | } |
40 | |
41 | int rc() const |
42 | { |
43 | return _rc; |
44 | } |
45 | |
46 | static int count() |
47 | { |
48 | return _count; |
49 | } |
50 | |
51 | protected: |
52 | ~TestObj() |
53 | { |
54 | --_count; |
55 | } |
56 | |
57 | private: |
58 | int _rc; |
59 | static int _count; |
60 | }; |
61 | |
62 | int TestObj::_count = 0; |
63 | } |
64 | |
65 | |
66 | AutoReleasePoolTest::AutoReleasePoolTest(const std::string& rName): CppUnit::TestCase(rName) |
67 | { |
68 | } |
69 | |
70 | |
71 | AutoReleasePoolTest::~AutoReleasePoolTest() |
72 | { |
73 | } |
74 | |
75 | |
76 | void AutoReleasePoolTest::testAutoReleasePool() |
77 | { |
78 | AutoReleasePool<TestObj> arp; |
79 | arp.add(new TestObj); |
80 | arp.add(new TestObj); |
81 | assertTrue (TestObj::count() == 2); |
82 | arp.release(); |
83 | assertTrue (TestObj::count() == 0); |
84 | } |
85 | |
86 | |
87 | void AutoReleasePoolTest::setUp() |
88 | { |
89 | } |
90 | |
91 | |
92 | void AutoReleasePoolTest::tearDown() |
93 | { |
94 | } |
95 | |
96 | |
97 | CppUnit::Test* AutoReleasePoolTest::suite() |
98 | { |
99 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AutoReleasePoolTest"); |
100 | |
101 | CppUnit_addTest(pSuite, AutoReleasePoolTest, testAutoReleasePool); |
102 | |
103 | return pSuite; |
104 | } |
105 |