forked from IASI-SAKS/groucho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUCacheTest.java
244 lines (205 loc) · 6.05 KB
/
LRUCacheTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* This file IS NOT part of the GROUCHO project.
*
* This is a reference test program developed by
* a PhD student at Università della Svizzera Italiana
* by reading the documentation available about LRUCache.
* The developer had long industrial experience in software
* testing. Specifically, before starting the PhD Program
* worked both as software tester, and test manager in
* several companies.
*
*/
package ch.usi.precrime.lrucache;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.util.Collection;
import java.util.Set;
import org.junit.Test;
import com.opensymphony.oscache.base.algorithm.LRUCache;
import it.cnr.iasi.saks.groucho.lab.instrument.test.experiments.oscache.testIssueCache236.LRUCache_AbstractUnitTest;
/**
* This file IS NOT part of the GROUCHO project.
*
* This is a reference test program developed by
* a PhD student at Università della Svizzera Italiana
* by reading the documentation available about LRUCache.
* The developer had long industrial experience in software
* testing. Specifically, before starting the PhD Program
* worked both as software tester, and test manager in
* several companies.
*
*/
/**
* Unit test for LRUCache.
*/
public class LRUCacheTest extends LRUCache_AbstractUnitTest {
protected final String K1 = "key-6fd3fc6e3105127bdc968175d1f108ec";
protected final String V1 = "val-6fd3fc6e3105127bdc968175d1f108ec";
protected final String K2 = "key-f4173c82908877d21cb616ae758a38a6";
protected final String V2 = "val-f4173c82908877d21cb616ae758a38a6";
@Test
public void testClear_one()
{
this.cache.put(K1, V1);
this.cache.clear();
assertTrue(this.cache.size() == 0);
}
@Test
public void testClear_more()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
}
this.cache.clear();
assertTrue(this.cache.size() == 0);
}
@Test
public void testClone()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
}
LRUCache clone_cache = (LRUCache) this.cache.clone();
assertTrue(clone_cache.size() == this.cache.size());
for(int i = 0; i < 3; i++) {
assertTrue(clone_cache.get(K1 + "-" + Integer.toString(i)).equals(V1 + "-" + Integer.toString(i)));
}
}
@Test
public void testContains()
{
this.cache.put(K1, V1);
assertTrue(this.cache.contains(V1));
}
@Test
public void testNotContains()
{
this.cache.put(K1, V1);
assertTrue(!this.cache.contains(V2));
}
@Test
public void testContainsKey()
{
this.cache.put(K1, V1);
assertTrue(this.cache.containsKey(K1));
}
@Test
public void testNotContainsKey()
{
this.cache.clear();
this.cache.put(K1, V1);
assertFalse(this.cache.containsKey(K2));
}
@Test
public void testContainsValue()
{
this.cache.clear();
this.cache.put(K1, V1);
assertTrue(this.cache.containsValue(V1));
}
@Test
public void testNotContainsValue()
{
this.cache.put(K1, V1);
assertFalse(this.cache.containsValue(V2));
}
@Test
public void testGet()
{
this.cache.put(K1, V1);
assertTrue(this.cache.get(K1) == V1);
assertTrue(this.cache.get(K2) == null);
}
@Test
public void testContainsValue_morethanOnce()
{
this.cache.put(K1, V1);
this.cache.put(K2, V1);
assertTrue(this.cache.containsValue(V1));
}
@Test
public void testGetMaxEntries()
{
assertTrue(this.cache.getMaxEntries() == CACHE_CAPACITY);
}
@Test
public void testIsEmpty_whenEmpty()
{
assertTrue(this.cache.isEmpty());
}
@Test
public void testIsEmpty_whenNotEmpty()
{
this.cache.put(K1, V1);
assertFalse(this.cache.isEmpty());
}
@Test
public void testKeySet()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
}
Set keySet = this.cache.keySet();
for(int i = 0; i < 3; i++) {
assertTrue(keySet.contains(K1 + "-" +Integer.toString(i)));
}
}
@Test
public void testPut()
{
this.cache.put(K1, V1);
assertTrue(this.cache.get(K1) == V1);
}
@Test
public void testPutMore()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
assertTrue(this.cache.get(K1 + "-" + Integer.toString(i)).equals(V1 + "-" + Integer.toString(i)));
}
}
@Test
public void testRemove()
{
this.cache.put(K1, V1);
this.cache.remove(K1);
assertFalse(this.cache.containsKey(K1));
}
@Test
public void testSetMaxEntries()
{
this.cache.setMaxEntries(5);
assertTrue(this.cache.getMaxEntries() == 5);
}
@Test
public void testSize_zero()
{
assertTrue(this.cache.size() == 0);
}
@Test
public void testSize_one()
{
this.cache.put(K1, V1);
assertTrue(this.cache.size() == 1);
}
@Test
public void testSize_more()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
}
assertTrue(this.cache.size() == 3);
}
@Test
public void testValues()
{
for(int i = 0; i < 3; i++) {
this.cache.put(K1 + "-" + Integer.toString(i), V1 + "-" + Integer.toString(i));
}
Collection values = this.cache.values();
for(int i = 0; i < 3; i++) {
assertTrue(values.contains(V1 + "-" + Integer.toString(i)));
}
}
}