1 | // |
---|---|
2 | // ObjectPoolTest.cpp |
3 | // |
4 | // Copyright (c) 2010-2012, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "ObjectPoolTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/ObjectPool.h" |
15 | #include "Poco/Exception.h" |
16 | |
17 | |
18 | using Poco::ObjectPool; |
19 | |
20 | |
21 | ObjectPoolTest::ObjectPoolTest(const std::string& rName): CppUnit::TestCase(rName) |
22 | { |
23 | } |
24 | |
25 | |
26 | ObjectPoolTest::~ObjectPoolTest() |
27 | { |
28 | } |
29 | |
30 | |
31 | void ObjectPoolTest::testObjectPool() |
32 | { |
33 | ObjectPool<std::string, Poco::SharedPtr<std::string> > pool(3, 4); |
34 | |
35 | assertTrue (pool.capacity() == 3); |
36 | assertTrue (pool.peakCapacity() == 4); |
37 | assertTrue (pool.size() == 0); |
38 | assertTrue (pool.available() == 4); |
39 | |
40 | Poco::SharedPtr<std::string> pStr1 = pool.borrowObject(); |
41 | pStr1->assign("first"); |
42 | assertTrue (pool.size() == 1); |
43 | assertTrue (pool.available() == 3); |
44 | |
45 | Poco::SharedPtr<std::string> pStr2 = pool.borrowObject(); |
46 | pStr2->assign("second"); |
47 | assertTrue (pool.size() == 2); |
48 | assertTrue (pool.available() == 2); |
49 | |
50 | Poco::SharedPtr<std::string> pStr3 = pool.borrowObject(); |
51 | pStr3->assign("third"); |
52 | assertTrue (pool.size() == 3); |
53 | assertTrue (pool.available() == 1); |
54 | |
55 | Poco::SharedPtr<std::string> pStr4 = pool.borrowObject(); |
56 | pStr4->assign("fourth"); |
57 | assertTrue (pool.size() == 4); |
58 | assertTrue (pool.available() == 0); |
59 | |
60 | Poco::SharedPtr<std::string> pStr5 = pool.borrowObject(); |
61 | assertTrue (pStr5.isNull()); |
62 | |
63 | pool.returnObject(pStr4); |
64 | assertTrue (pool.size() == 4); |
65 | assertTrue (pool.available() == 1); |
66 | |
67 | pool.returnObject(pStr3); |
68 | assertTrue (pool.size() == 4); |
69 | assertTrue (pool.available() == 2); |
70 | |
71 | pStr3 = pool.borrowObject(); |
72 | assertTrue (*pStr3 == "third"); |
73 | assertTrue (pool.available() == 1); |
74 | |
75 | pool.returnObject(pStr3); |
76 | pool.returnObject(pStr2); |
77 | pool.returnObject(pStr1); |
78 | |
79 | assertTrue (pool.size() == 3); |
80 | assertTrue (pool.available() == 4); |
81 | |
82 | pStr1 = pool.borrowObject(); |
83 | assertTrue (*pStr1 == "second"); |
84 | assertTrue (pool.available() == 3); |
85 | |
86 | pool.returnObject(pStr1); |
87 | assertTrue (pool.available() == 4); |
88 | } |
89 | |
90 | |
91 | void ObjectPoolTest::setUp() |
92 | { |
93 | } |
94 | |
95 | |
96 | void ObjectPoolTest::tearDown() |
97 | { |
98 | } |
99 | |
100 | |
101 | CppUnit::Test* ObjectPoolTest::suite() |
102 | { |
103 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ObjectPoolTest"); |
104 | |
105 | CppUnit_addTest(pSuite, ObjectPoolTest, testObjectPool); |
106 | |
107 | return pSuite; |
108 | } |
109 |