diff options
| author | teschi <teschi+github@team23.org> | 2012-04-03 01:31:10 +0200 |
|---|---|---|
| committer | teschi <teschi+github@team23.org> | 2012-04-03 01:31:10 +0200 |
| commit | 5fd86fa6a69fdc3e87cb04b59395e070851de1dd (patch) | |
| tree | 5330613d292c6f184232344536f1fa271bcf4d2e /tests/src/cgeo/geocaching/utils | |
| parent | 61fb8dd256aa799ba8ae464acd30879683f7f032 (diff) | |
| download | cgeo-5fd86fa6a69fdc3e87cb04b59395e070851de1dd.zip cgeo-5fd86fa6a69fdc3e87cb04b59395e070851de1dd.tar.gz cgeo-5fd86fa6a69fdc3e87cb04b59395e070851de1dd.tar.bz2 | |
Support different modes of operation on LastRecentlyUsedMap
Added a Set interface for the LastRecentlyUsedMap
Changed CGeoMap to use the LastRecentlyUsedSet as its caches-cache
Diffstat (limited to 'tests/src/cgeo/geocaching/utils')
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/LeastRecentlyUsedMapTest.java | 83 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/LeastRecentlyUsedSetTest.java | 85 |
2 files changed, 168 insertions, 0 deletions
diff --git a/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedMapTest.java b/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedMapTest.java new file mode 100644 index 0000000..939bc68 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedMapTest.java @@ -0,0 +1,83 @@ +package cgeo.geocaching.utils; + +import cgeo.geocaching.utils.LeastRecentlyUsedMap.OperationModes; + +import java.util.Collection; +import java.util.Map; + +import junit.framework.TestCase; + +public class LeastRecentlyUsedMapTest extends TestCase { + + private static String colToStr(Collection<?> col) + { + StringBuilder strb = new StringBuilder(); + boolean first = true; + for (Object o : col) { + if (!first) { + strb.append(", "); + } + first = false; + strb.append(o.toString()); + } + return strb.toString(); + } + + public static void testLruMode() { + Map<String, String> map = new LeastRecentlyUsedMap<String, String>(4, OperationModes.LRU_CACHE); + map.put("one", "1"); + map.put("two", "2"); + map.put("three", "3"); + // keep in cache + map.get("one"); + map.put("four", "4"); + map.put("five", "5"); + map.put("six", "6"); + // keep in cache + map.get("one"); + // re-add + map.put("five", "5"); + map.put("seven", "7"); + + assertEquals("six, one, five, seven", colToStr(map.keySet())); + } + + public static void testBoundedMode() { + Map<String, String> map = new LeastRecentlyUsedMap<String, String>(5, OperationModes.BOUNDED); + map.put("one", "1"); + map.put("two", "2"); + map.put("three", "3"); + // read does not change anything + map.get("one"); + map.put("four", "4"); + // re-put should update the order + map.put("three", "3"); + map.put("five", "5"); + // read does not change anything + map.get("one"); + map.put("six", "6"); + map.put("seven", "7"); + + assertEquals("four, three, five, six, seven", colToStr(map.keySet())); + } + + public static void testBoundedIgnoreReinsertMode() { + Map<String, String> map = new LeastRecentlyUsedMap<String, String>(5, OperationModes.BOUNDED_IGNORE_REINSERT); + map.put("one", "1"); + map.put("two", "2"); + map.put("three", "3"); + // read does not change anything + map.get("one"); + map.put("four", "4"); + // re-put should update the order + map.put("two", "2"); + map.put("five", "5"); + // read does not change anything + map.get("one"); + map.put("six", "6"); + map.put("seven", "7"); + + assertEquals("three, four, five, six, seven", colToStr(map.keySet())); + } + +} diff --git a/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedSetTest.java b/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedSetTest.java new file mode 100644 index 0000000..f910b84 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/LeastRecentlyUsedSetTest.java @@ -0,0 +1,85 @@ +package cgeo.geocaching.utils; + +import cgeo.geocaching.utils.LeastRecentlyUsedMap.OperationModes; + +import java.util.Collection; +import java.util.Set; + +import junit.framework.TestCase; + +public class LeastRecentlyUsedSetTest extends TestCase { + + private static String colToStr(Collection<?> col) + { + StringBuilder strb = new StringBuilder(); + boolean first = true; + for (Object o : col) { + if (!first) { + strb.append(", "); + } + first = false; + strb.append(o.toString()); + } + return strb.toString(); + } + + public static void testBoundedMode() { + Set<String> set = new LeastRecentlyUsedSet<String>(5, OperationModes.BOUNDED); + set.add("one"); + set.add("two"); + set.add("three"); + // read does not change anything + set.contains("one"); + set.add("four"); + // re-put should update the order + set.add("three"); + set.add("five"); + // read does not change anything + set.contains("one"); + set.add("six"); + set.add("seven"); + + assertEquals("four, three, five, six, seven", colToStr(set)); + } + + public static void testLruMode() { + // the same as behaviour as BOUNDED + + Set<String> set = new LeastRecentlyUsedSet<String>(5, OperationModes.LRU_CACHE); + set.add("one"); + set.add("two"); + set.add("three"); + // read does not change anything + set.contains("one"); + set.add("four"); + // re-put should update the order + set.add("three"); + set.add("five"); + // read does not change anything + set.contains("one"); + set.add("six"); + set.add("seven"); + + assertEquals("four, three, five, six, seven", colToStr(set)); + } + + public static void testBoundedIgnoreReinsertMode() { + Set<String> set = new LeastRecentlyUsedSet<String>(5, OperationModes.BOUNDED_IGNORE_REINSERT); + set.add("one"); + set.add("two"); + set.add("three"); + // read does not change anything + set.contains("one"); + set.add("four"); + // re-put should update the order + set.add("two"); + set.add("five"); + // read does not change anything + set.contains("one"); + set.add("six"); + set.add("seven"); + + assertEquals("three, four, five, six, seven", colToStr(set)); + } + +} |
