001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010: import com.ibm.icu.impl.LRUMap;
011:
012: public class LRUMapTest extends TestFmwk {
013: public static void main(String[] args) throws Exception {
014: LRUMapTest test = new LRUMapTest();
015: test.run(args);
016: }
017:
018: public void TestLRUMap() {
019: // Default size - max 64
020: logln("Testing LRUMap with the default size");
021: LRUMap map = new LRUMap();
022: execute(map, 64);
023:
024: // max size - 16
025: logln("Testing LRUMap with initial/max size - 4/16");
026: map = new LRUMap(4, 16);
027: execute(map, 16);
028: }
029:
030: private void execute(LRUMap map, int maxSize /* maxSize > 6 */) {
031: Integer num;
032: String numStr;
033:
034: for (int i = 0; i <= maxSize; i++) {
035: num = new Integer(i);
036: numStr = num.toString();
037: map.put(numStr, num);
038: }
039: // The first key/value should be removed, because
040: // we already put 65 entries
041: num = (Integer) map.get("0");
042: if (num == null) {
043: logln("OK: The entry '0' was removed.");
044: } else {
045: errln("The entry '0' is still available.");
046: }
047: // Access the second entry '1', which is currently
048: // the eldest.
049: num = (Integer) map.get("1");
050: if (num == null) {
051: errln("The eldest entry '1' was removed.");
052: } else {
053: logln("OK: The eldest entry '1' is available.");
054: }
055: // Put another entry - because the entry '1' was
056: // accessed above, the entry '2' is the eldest and
057: // putting new entry should remove the entry.
058: num = new Integer(maxSize + 1);
059: map.put(num.toString(), num);
060: num = (Integer) map.get("2");
061: if (num == null) {
062: logln("OK: The entry '2' was removed.");
063: } else {
064: errln("The entry '2' is still available.");
065: }
066: // The entry '3' is the eldest for now.
067: boolean b = map.containsKey("3");
068: if (b) {
069: logln("OK: The eldest entry '3' is available.");
070: } else {
071: errln("The eldest entry '3' was removed.");
072: }
073: // contansKey should not affect the access order
074: num = new Integer(maxSize + 2);
075: map.put(num.toString(), num);
076: num = (Integer) map.get("3");
077: if (num == null) {
078: logln("OK: The entry '3' was removed.");
079: } else {
080: errln("The entry '3' is still available.");
081: }
082: // Putting existing entry with new value
083: num = (Integer) map.put("4", new Integer(-4));
084: if (num == null) {
085: errln("The entry '4' no longer exists");
086: }
087: if (num.intValue() != 4) {
088: errln("The value for '4' was not 4");
089: }
090: // The entry '5' is the eldest for now, because
091: // the entry '4' was updated above.
092: num = new Integer(maxSize + 3);
093: map.put(num.toString(), num);
094: num = (Integer) map.get("5");
095: if (num == null) {
096: logln("OK: The entry '5' was removed.");
097: } else {
098: errln("The entry '5' is still available.");
099: }
100: // Clear the map
101: map.clear();
102: num = (Integer) map.get("6");
103: if (num == null) {
104: logln("OK: The entry '6' was removed.");
105: } else {
106: errln("The entry '6' is still available.");
107: }
108: }
109: }
|