~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/ExpireCacheTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ExpireCacheTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/ExpireCacheTest.cpp#3 $
 
5
//
 
6
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
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"
 
40
 
 
41
 
 
42
using namespace Poco;
 
43
 
 
44
 
 
45
#define DURSLEEP 250
 
46
#define DURHALFSLEEP DURSLEEP / 2
 
47
#define DURWAIT  300
 
48
 
 
49
 
 
50
ExpireCacheTest::ExpireCacheTest(const std::string& name): CppUnit::TestCase(name)
 
51
{
 
52
}
 
53
 
 
54
 
 
55
ExpireCacheTest::~ExpireCacheTest()
 
56
{
 
57
}
 
58
 
 
59
 
 
60
void ExpireCacheTest::testClear()
 
61
{
 
62
        ExpireCache<int, int> aCache(DURSLEEP);
 
63
        aCache.add(1, 2);
 
64
        aCache.add(3, 4);
 
65
        aCache.add(5, 6);
 
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);
 
72
        aCache.clear();
 
73
        assert (!aCache.has(1));
 
74
        assert (!aCache.has(3));
 
75
        assert (!aCache.has(5));
 
76
}
 
77
 
 
78
 
 
79
void ExpireCacheTest::testExpire0()
 
80
{
 
81
        try
 
82
        {
 
83
                ExpireCache<int, int> aCache(24);
 
84
                failmsg("cache expire lower than 25 is illegal, test should fail");
 
85
        }
 
86
        catch (Poco::InvalidArgumentException&)
 
87
        {
 
88
        }
 
89
}
 
90
 
 
91
 
 
92
void ExpireCacheTest::testExpireN()
 
93
{
 
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());
 
101
        assert (*tmp == 2);
 
102
        assert (aCache.size() == 1);
 
103
        Thread::sleep(DURWAIT);
 
104
        assert (aCache.size() == 0);
 
105
        assert (!aCache.has(1));
 
106
 
 
107
        // tmp must still be valid, access it
 
108
        assert (*tmp == 2);
 
109
        tmp = aCache.get(1);
 
110
        assert (!tmp);
 
111
 
 
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));
 
117
        tmp = aCache.get(1);
 
118
        SharedPtr<int> tmp2 = aCache.get(3);
 
119
        assert (*tmp == 2); 
 
120
        assert (*tmp2 == 4);
 
121
 
 
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);
 
128
        assert (*tmp2 == 4);
 
129
        Thread::sleep(DURHALFSLEEP+25); //3|1
 
130
        assert (!aCache.has(3));
 
131
        assert (*tmp2 == 4);
 
132
        tmp = aCache.get(1);
 
133
        tmp2 = aCache.get(3);
 
134
        assert (!tmp);
 
135
        assert (!tmp2);
 
136
 
 
137
        // removing illegal entries should work too
 
138
        aCache.remove(666);
 
139
 
 
140
        aCache.clear();
 
141
        assert (!aCache.has(5));
 
142
        assert (!aCache.has(3));
 
143
}
 
144
 
 
145
 
 
146
void ExpireCacheTest::testDuplicateAdd()
 
147
{
 
148
        ExpireCache<int, int> aCache(DURSLEEP);
 
149
        aCache.add(1, 2); // 1
 
150
        assert (aCache.has(1));
 
151
        assert (*aCache.get(1) == 2);
 
152
        aCache.add(1, 3);
 
153
        assert (aCache.has(1));
 
154
        assert (*aCache.get(1) == 3);
 
155
}
 
156
 
 
157
 
 
158
void ExpireCacheTest::setUp()
 
159
{
 
160
}
 
161
 
 
162
 
 
163
void ExpireCacheTest::tearDown()
 
164
{
 
165
}
 
166
 
 
167
 
 
168
CppUnit::Test* ExpireCacheTest::suite()
 
169
{
 
170
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ExpireCacheTest");
 
171
 
 
172
        CppUnit_addTest(pSuite, ExpireCacheTest, testClear);
 
173
        CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0);
 
174
        CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN);
 
175
        CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);
 
176
 
 
177
        return pSuite;
 
178
}