forked from seancarr/commons
/
TestMemoryBoundLruHashMap.java
165 lines (117 loc) · 4.49 KB
/
TestMemoryBoundLruHashMap.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
/**
* Copyright 2013 LiveRamp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.liveramp.commons.collections;
import java.util.Iterator;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import com.liveramp.commons.util.IntegerMemoryUsageEstimator;
import com.liveramp.commons.util.LongMemoryUsageEstimator;
import com.liveramp.commons.util.StringMemoryUsageEstimator;
public class TestMemoryBoundLruHashMap extends TestCase {
@Test
public void testNumBytesCapacity() {
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<Integer, String>(128, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());
assertEquals(0, map.size());
assertEquals(0, map.getNumManagedBytes());
// Insert
map.putAndEvict(1, "1"); // 40 byte string
assertEquals(1, map.size());
assertEquals(56, map.getNumManagedBytes());
assertEquals("1", map.get(1));
// Insert
map.putAndEvict(2, "12"); // 48 byte string
assertEquals(2, map.size());
assertEquals(120, map.getNumManagedBytes());
assertEquals("12", map.get(2));
// Insert which goes over the size threshold
map.putAndEvict(3, "1"); // 40 byte string
assertEquals(2, map.size());
assertEquals(120, map.getNumManagedBytes());
assertEquals("1", map.get(3));
// Insert which goes over the size threshold
map.putAndEvict(4, "1234567890"); // 64 byte string
assertEquals(1, map.size());
assertEquals(80, map.getNumManagedBytes());
assertEquals("1234567890", map.get(4));
// Insert that replaces a value
map.putAndEvict(4, "1");
assertEquals(1, map.size());
assertEquals(56, map.getNumManagedBytes());
assertEquals("1", map.get(4));
// Insert
map.putAndEvict(2, "12");
assertEquals(2, map.size());
assertEquals(120, map.getNumManagedBytes());
assertEquals("12", map.get(2));
// Remove
map.remove(4);
assertEquals(1, map.size());
assertEquals(64, map.getNumManagedBytes());
assertEquals(null, map.get(4));
}
@Test
public void testNumItemsCapacity() {
MemoryBoundLruHashMap<Integer, String> map = new MemoryBoundLruHashMap<Integer, String>(2, 1024, new IntegerMemoryUsageEstimator(), new StringMemoryUsageEstimator());
assertEquals(0, map.size());
assertEquals(0, map.getNumManagedBytes());
// Insert
map.putAndEvict(1, "a");
assertEquals(1, map.size());
assertEquals(56, map.getNumManagedBytes());
assertEquals("a", map.get(1));
// Insert
map.putAndEvict(2, "ab");
assertEquals(2, map.size());
assertEquals(120, map.getNumManagedBytes());
assertEquals("ab", map.get(2));
// Insert which goes over the num items limit
map.putAndEvict(3, "abc");
assertEquals(2, map.size());
assertEquals(128, map.getNumManagedBytes());
// 1 should be gone
assertEquals(null, map.get(1));
assertEquals("ab", map.get(2));
assertEquals("abc", map.get(3));
// Get to change access ordering
map.get(2);
// Insert which goes over the num items limit
map.putAndEvict(1, "a");
assertEquals(2, map.size());
assertEquals(120, map.getNumManagedBytes());
// 3 should be gone
assertEquals("a", map.get(1));
assertEquals("ab", map.get(2));
assertEquals(null, map.get(3));
}
@Test
public void testIterator() {
MemoryBoundLruHashMap<Long, Long> map = new MemoryBoundLruHashMap<Long, Long>(10, 1024, new LongMemoryUsageEstimator(), new LongMemoryUsageEstimator());
map.putAndEvict(1L, 1L);
map.putAndEvict(2L, 2L);
map.putAndEvict(3L, 3L);
Iterator<Map.Entry<Long, Long>> itr = map.iterator();
assertEquals(1L, (long)itr.next().getKey());
assertEquals(2L, (long)itr.next().getKey());
assertEquals(3, map.size());
assertEquals(96, map.getNumManagedBytes());
itr.remove();
assertEquals(2, map.size());
assertEquals(64, map.getNumManagedBytes());
assertEquals(3L, (long)itr.next().getKey());
assertFalse(itr.hasNext());
}
}