KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TestLRUMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.map;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.Test;
24 import junit.textui.TestRunner;
25
26 import org.apache.commons.collections.BulkTest;
27 import org.apache.commons.collections.OrderedMap;
28 import org.apache.commons.collections.ResettableIterator;
29
30 /**
31  * JUnit tests.
32  *
33  * @version $Revision: 1.8 $ $Date: 2004/05/12 19:51:28 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class TestLRUMap extends AbstractTestOrderedMap {
38
39     public TestLRUMap(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static void main(String JavaDoc[] args) {
44         TestRunner.run(suite());
45     }
46     
47     public static Test suite() {
48         return BulkTest.makeSuite(TestLRUMap.class);
49     }
50
51     public Map JavaDoc makeEmptyMap() {
52         return new LRUMap();
53     }
54
55     public boolean isGetStructuralModify() {
56         return true;
57     }
58     
59     public String JavaDoc getCompatibilityVersion() {
60         return "3";
61     }
62
63     //-----------------------------------------------------------------------
64
public void testLRU() {
65         if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
66         Object JavaDoc[] keys = getSampleKeys();
67         Object JavaDoc[] values = getSampleValues();
68         Iterator JavaDoc it = null;
69         
70         LRUMap map = new LRUMap(2);
71         assertEquals(0, map.size());
72         assertEquals(false, map.isFull());
73         assertEquals(2, map.maxSize());
74         
75         map.put(keys[0], values[0]);
76         assertEquals(1, map.size());
77         assertEquals(false, map.isFull());
78         assertEquals(2, map.maxSize());
79         
80         map.put(keys[1], values[1]);
81         assertEquals(2, map.size());
82         assertEquals(true, map.isFull());
83         assertEquals(2, map.maxSize());
84         it = map.keySet().iterator();
85         assertSame(keys[0], it.next());
86         assertSame(keys[1], it.next());
87         it = map.values().iterator();
88         assertSame(values[0], it.next());
89         assertSame(values[1], it.next());
90
91         map.put(keys[2], values[2]);
92         assertEquals(2, map.size());
93         assertEquals(true, map.isFull());
94         assertEquals(2, map.maxSize());
95         it = map.keySet().iterator();
96         assertSame(keys[1], it.next());
97         assertSame(keys[2], it.next());
98         it = map.values().iterator();
99         assertSame(values[1], it.next());
100         assertSame(values[2], it.next());
101
102         map.put(keys[2], values[0]);
103         assertEquals(2, map.size());
104         assertEquals(true, map.isFull());
105         assertEquals(2, map.maxSize());
106         it = map.keySet().iterator();
107         assertSame(keys[1], it.next());
108         assertSame(keys[2], it.next());
109         it = map.values().iterator();
110         assertSame(values[1], it.next());
111         assertSame(values[0], it.next());
112
113         map.put(keys[1], values[3]);
114         assertEquals(2, map.size());
115         assertEquals(true, map.isFull());
116         assertEquals(2, map.maxSize());
117         it = map.keySet().iterator();
118         assertSame(keys[2], it.next());
119         assertSame(keys[1], it.next());
120         it = map.values().iterator();
121         assertSame(values[0], it.next());
122         assertSame(values[3], it.next());
123     }
124     
125     //-----------------------------------------------------------------------
126
public void testReset() {
127         resetEmpty();
128         OrderedMap ordered = (OrderedMap) map;
129         ((ResettableIterator) ordered.mapIterator()).reset();
130         
131         resetFull();
132         ordered = (OrderedMap) map;
133         List JavaDoc list = new ArrayList JavaDoc(ordered.keySet());
134         ResettableIterator it = (ResettableIterator) ordered.mapIterator();
135         assertSame(list.get(0), it.next());
136         assertSame(list.get(1), it.next());
137         it.reset();
138         assertSame(list.get(0), it.next());
139     }
140     
141     //-----------------------------------------------------------------------
142
public void testAccessOrder() {
143         if (isPutAddSupported() == false || isPutChangeSupported() == false) return;
144         Object JavaDoc[] keys = getSampleKeys();
145         Object JavaDoc[] values = getSampleValues();
146         Iterator JavaDoc it = null;
147         
148         resetEmpty();
149         map.put(keys[0], values[0]);
150         map.put(keys[1], values[1]);
151         it = map.keySet().iterator();
152         assertSame(keys[0], it.next());
153         assertSame(keys[1], it.next());
154         it = map.values().iterator();
155         assertSame(values[0], it.next());
156         assertSame(values[1], it.next());
157
158         // no change to order
159
map.put(keys[1], values[1]);
160         it = map.keySet().iterator();
161         assertSame(keys[0], it.next());
162         assertSame(keys[1], it.next());
163         it = map.values().iterator();
164         assertSame(values[0], it.next());
165         assertSame(values[1], it.next());
166
167         // no change to order
168
map.put(keys[1], values[2]);
169         it = map.keySet().iterator();
170         assertSame(keys[0], it.next());
171         assertSame(keys[1], it.next());
172         it = map.values().iterator();
173         assertSame(values[0], it.next());
174         assertSame(values[2], it.next());
175
176         // change to order
177
map.put(keys[0], values[3]);
178         it = map.keySet().iterator();
179         assertSame(keys[1], it.next());
180         assertSame(keys[0], it.next());
181         it = map.values().iterator();
182         assertSame(values[2], it.next());
183         assertSame(values[3], it.next());
184
185         // change to order
186
map.get(keys[1]);
187         it = map.keySet().iterator();
188         assertSame(keys[0], it.next());
189         assertSame(keys[1], it.next());
190         it = map.values().iterator();
191         assertSame(values[3], it.next());
192         assertSame(values[2], it.next());
193
194         // change to order
195
map.get(keys[0]);
196         it = map.keySet().iterator();
197         assertSame(keys[1], it.next());
198         assertSame(keys[0], it.next());
199         it = map.values().iterator();
200         assertSame(values[2], it.next());
201         assertSame(values[3], it.next());
202
203         // no change to order
204
map.get(keys[0]);
205         it = map.keySet().iterator();
206         assertSame(keys[1], it.next());
207         assertSame(keys[0], it.next());
208         it = map.values().iterator();
209         assertSame(values[2], it.next());
210         assertSame(values[3], it.next());
211     }
212     
213     public void testClone() {
214         LRUMap map = new LRUMap(10);
215         map.put("1", "1");
216         Map JavaDoc cloned = (Map JavaDoc) map.clone();
217         assertEquals(map.size(), cloned.size());
218         assertSame(map.get("1"), cloned.get("1"));
219     }
220     
221     public void testRemoveLRU() {
222         MockLRUMapSubclass map = new MockLRUMapSubclass(2);
223         assertNull(map.entry);
224         map.put("A", "a");
225         assertNull(map.entry);
226         map.put("B", "b");
227         assertNull(map.entry);
228         map.put("C", "c"); // removes oldest, which is A=a
229
assertNotNull(map.entry);
230         assertEquals("A", map.key);
231         assertEquals("a", map.value);
232         assertEquals("C", map.entry.getKey()); // entry is reused
233
assertEquals("c", map.entry.getValue()); // entry is reused
234
assertEquals(false, map.containsKey("A"));
235         assertEquals(true, map.containsKey("B"));
236         assertEquals(true, map.containsKey("C"));
237     }
238     
239     static class MockLRUMapSubclass extends LRUMap {
240         LinkEntry entry;
241         Object JavaDoc key;
242         Object JavaDoc value;
243
244         MockLRUMapSubclass(int size) {
245             super(size);
246         }
247
248         protected boolean removeLRU(LinkEntry entry) {
249             this.entry = entry;
250             this.key = entry.getKey();
251             this.value = entry.getValue();
252             return true;
253         }
254     }
255     
256     public void testRemoveLRUBlocksRemove() {
257         MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2, false);
258         assertEquals(0, map.size());
259         map.put("A", "a");
260         assertEquals(1, map.size());
261         map.put("B", "b");
262         assertEquals(2, map.size());
263         map.put("C", "c"); // should remove oldest, which is A=a, but this is blocked
264
assertEquals(3, map.size());
265         assertEquals(2, map.maxSize());
266         assertEquals(true, map.containsKey("A"));
267         assertEquals(true, map.containsKey("B"));
268         assertEquals(true, map.containsKey("C"));
269     }
270
271     public void testRemoveLRUBlocksRemoveScan() {
272         MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2, true);
273         assertEquals(0, map.size());
274         map.put("A", "a");
275         assertEquals(1, map.size());
276         map.put("B", "b");
277         assertEquals(2, map.size());
278         map.put("C", "c"); // should remove oldest, which is A=a, but this is blocked
279
assertEquals(3, map.size());
280         assertEquals(2, map.maxSize());
281         assertEquals(true, map.containsKey("A"));
282         assertEquals(true, map.containsKey("B"));
283         assertEquals(true, map.containsKey("C"));
284     }
285     
286     static class MockLRUMapSubclassBlocksRemove extends LRUMap {
287         MockLRUMapSubclassBlocksRemove(int size, boolean scanUntilRemove) {
288             super(size, scanUntilRemove);
289         }
290
291         protected boolean removeLRU(LinkEntry entry) {
292             return false;
293         }
294     }
295     
296     public void testRemoveLRUFirstBlocksRemove() {
297         MockLRUMapSubclassFirstBlocksRemove map = new MockLRUMapSubclassFirstBlocksRemove(2);
298         assertEquals(0, map.size());
299         map.put("A", "a");
300         assertEquals(1, map.size());
301         map.put("B", "b");
302         assertEquals(2, map.size());
303         map.put("C", "c"); // should remove oldest, which is A=a but this is blocked - so advance to B=b
304
assertEquals(2, map.size());
305         assertEquals(2, map.maxSize());
306         assertEquals(true, map.containsKey("A"));
307         assertEquals(false, map.containsKey("B"));
308         assertEquals(true, map.containsKey("C"));
309     }
310
311     static class MockLRUMapSubclassFirstBlocksRemove extends LRUMap {
312         MockLRUMapSubclassFirstBlocksRemove(int size) {
313             super(size, true);
314         }
315
316         protected boolean removeLRU(LinkEntry entry) {
317             if ("a".equals(entry.getValue())) {
318                 return false;
319             } else {
320                 return true;
321             }
322         }
323     }
324
325 // public void testCreate() throws Exception {
326
// resetEmpty();
327
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LRUMap.emptyCollection.version3.obj");
328
// resetFull();
329
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LRUMap.fullCollection.version3.obj");
330
// }
331
}
332
Popular Tags