-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftCacheMapTest.java
More file actions
93 lines (81 loc) · 3.33 KB
/
SoftCacheMapTest.java
File metadata and controls
93 lines (81 loc) · 3.33 KB
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
package maven;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
public class SoftCacheMapTest {
private SoftCacheMap<Integer, Integer> getCache(int size, int frequency) {
return new SoftCacheMap<>(size, frequency);
}
private void testBefore(int size, int frequency, ArrayList<Integer> values,
ArrayList<Integer> expectedBefore) throws Exception {
String errorMessage = "Bad result";
SoftCacheMap<Integer, Integer> cache = getCache(size, frequency);
for (int i = 0; i < values.size(); ++i) {
cache.put(i, values.get(i));
}
for (int i = 0; i < values.size(); ++i) {
Assert.assertEquals(errorMessage, expectedBefore.get(i), cache.getIfPresent(i));
}
}
private void testAfter(int size, int frequency, ArrayList<Integer> values,
ArrayList<Integer> expectedAfter) {
String errorMessage = "Bad result";
SoftCacheMap<Integer, Integer> cache = getCache(size, frequency);
for (int i = 0; i < values.size(); ++i) {
cache.put(i, new Integer(values.get(i)));
}
try {
Object[] big = new Object[(int) Runtime.getRuntime().maxMemory()];
} catch (OutOfMemoryError e) {
// ignore
}
int counter = 0;
for (int i = 0; i < values.size(); ++i) {
if (cache.getIfPresent(i) != null) {
Assert.assertEquals(errorMessage, expectedAfter.get(counter), cache.getIfPresent(i));
++counter;
}
}
}
@Test
public void testReferenceQueueBefore() throws Exception {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
ArrayList<Integer> expectedBefore = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
testBefore(0, 1, values, expectedBefore);
}
@Test
public void testReferenceQueueAfter() throws Exception {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
testAfter(0, 1, values, null);
}
@Test
public void testRecentlyUsedQueueAfter0() throws Exception {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
ArrayList<Integer> expectedAfter = new ArrayList<>(Arrays.asList(4, 5));
testAfter(2, 1, values, expectedAfter);
}
@Test
public void testRecentlyUsedQueueAfter1() throws Exception {
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
ArrayList<Integer> expectedAfter = new ArrayList<>(Arrays.asList(2, 5));
String errorMessage = "Bad result";
SoftCacheMap<Integer, Integer> cache = getCache(2, 1);
for (int i = 0; i < values.size(); ++i) {
cache.put(i, new Integer(values.get(i)));
}
cache.getIfPresent(2);
try {
Object[] big = new Object[(int) Runtime.getRuntime().maxMemory()];
} catch (OutOfMemoryError e) {
// ignore
}
int counter = 0;
for (int i = 0; i < values.size(); ++i) {
if (cache.getIfPresent(i) != null) {
Assert.assertEquals(errorMessage, expectedAfter.get(counter), cache.getIfPresent(i));
++counter;
}
}
}
}