4
// $Id: //poco/1.2/Foundation/testsuite/src/ExpireCacheTest.cpp#3 $
6
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "ExpireCacheTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Exception.h"
37
#include "Poco/ExpireCache.h"
38
#include "Poco/Bugcheck.h"
39
#include "Poco/Thread.h"
46
#define DURHALFSLEEP DURSLEEP / 2
50
ExpireCacheTest::ExpireCacheTest(const std::string& name): CppUnit::TestCase(name)
55
ExpireCacheTest::~ExpireCacheTest()
60
void ExpireCacheTest::testClear()
62
ExpireCache<int, int> aCache(DURSLEEP);
66
assert (aCache.has(1));
67
assert (aCache.has(3));
68
assert (aCache.has(5));
69
assert (*aCache.get(1) == 2);
70
assert (*aCache.get(3) == 4);
71
assert (*aCache.get(5) == 6);
73
assert (!aCache.has(1));
74
assert (!aCache.has(3));
75
assert (!aCache.has(5));
79
void ExpireCacheTest::testExpire0()
83
ExpireCache<int, int> aCache(24);
84
failmsg("cache expire lower than 25 is illegal, test should fail");
86
catch (Poco::InvalidArgumentException&)
92
void ExpireCacheTest::testExpireN()
94
// 3-1 represents the cache sorted by age, elements get replaced at the end of the list
95
// 3-1|5 -> 5 gets removed
96
ExpireCache<int, int> aCache(DURSLEEP);
97
aCache.add(1, 2); // 1
98
assert (aCache.has(1));
99
SharedPtr<int> tmp = aCache.get(1);
100
assert (!tmp.isNull());
102
assert (aCache.size() == 1);
103
Thread::sleep(DURWAIT);
104
assert (aCache.size() == 0);
105
assert (!aCache.has(1));
107
// tmp must still be valid, access it
112
aCache.add(1, 2); // 1
113
Thread::sleep(DURHALFSLEEP);
114
aCache.add(3, 4); // 3-1
115
assert (aCache.has(1));
116
assert (aCache.has(3));
118
SharedPtr<int> tmp2 = aCache.get(3);
122
Thread::sleep(DURHALFSLEEP+25); //3|1
123
assert (!aCache.has(1));
124
assert (aCache.has(3));
125
assert (*tmp == 2); // 1-3
126
assert (*tmp2 == 4); // 3-1
127
tmp2 = aCache.get(3);
129
Thread::sleep(DURHALFSLEEP+25); //3|1
130
assert (!aCache.has(3));
133
tmp2 = aCache.get(3);
137
// removing illegal entries should work too
141
assert (!aCache.has(5));
142
assert (!aCache.has(3));
146
void ExpireCacheTest::testDuplicateAdd()
148
ExpireCache<int, int> aCache(DURSLEEP);
149
aCache.add(1, 2); // 1
150
assert (aCache.has(1));
151
assert (*aCache.get(1) == 2);
153
assert (aCache.has(1));
154
assert (*aCache.get(1) == 3);
158
void ExpireCacheTest::setUp()
163
void ExpireCacheTest::tearDown()
168
CppUnit::Test* ExpireCacheTest::suite()
170
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ExpireCacheTest");
172
CppUnit_addTest(pSuite, ExpireCacheTest, testClear);
173
CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0);
174
CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN);
175
CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);