001: package org.shiftone.cache.policy.lfu;
002:
003: import org.shiftone.cache.CacheFactory;
004: import org.shiftone.cache.util.TestCaseBase;
005:
006: /**
007: * Class LfuCacheTestCase
008: *
009: *
010: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
011: * @version $Revision: 1.2 $
012: */
013: public class LfuCacheTestCase extends TestCaseBase {
014:
015: public LfuCacheTestCase(String name) {
016: super (name);
017: }
018:
019: /**
020: * Method getCacheFactory
021: */
022: public CacheFactory getCacheFactory() {
023: return new LfuCacheFactory();
024: }
025:
026: /**
027: * Method testOne
028: */
029: public void testLfu10() {
030:
031: LfuCache cache = new LfuCache("lfu", 5000, 10);
032:
033: cache.addObject("A", "A"); //1
034: assertEquals("A", cache.dumpLfuKeys());
035: assertEquals("A", cache.dumpFifoKeys());
036: cache.addObject("B", "B"); //2
037: assertEquals("BA", cache.dumpLfuKeys());
038: assertEquals("BA", cache.dumpFifoKeys());
039: cache.addObject("C", "C"); //3
040: assertEquals("CBA", cache.dumpLfuKeys());
041: assertEquals("CBA", cache.dumpFifoKeys());
042: cache.addObject("D", "D"); //4
043: assertEquals("DCBA", cache.dumpLfuKeys());
044: assertEquals("DCBA", cache.dumpFifoKeys());
045: cache.addObject("E", "E"); //5
046: assertEquals("EDCBA", cache.dumpLfuKeys());
047: assertEquals("EDCBA", cache.dumpFifoKeys());
048: cache.addObject("F", "F"); //6
049: assertEquals("FEDCBA", cache.dumpLfuKeys());
050: assertEquals("FEDCBA", cache.dumpFifoKeys());
051: cache.addObject("G", "G"); //7
052: assertEquals("GFEDCBA", cache.dumpLfuKeys());
053: assertEquals("GFEDCBA", cache.dumpFifoKeys());
054: cache.addObject("H", "H"); //8
055: assertEquals("HGFEDCBA", cache.dumpLfuKeys());
056: assertEquals("HGFEDCBA", cache.dumpFifoKeys());
057: cache.addObject("I", "I"); //9
058: assertEquals("IHGFEDCBA", cache.dumpLfuKeys());
059: assertEquals("IHGFEDCBA", cache.dumpFifoKeys());
060: cache.addObject("J", "J"); //10
061: assertEquals("JIHGFEDCBA", cache.dumpLfuKeys());
062: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
063:
064: // observe the effect of getObject
065: assertNotNull(cache.getObject("E"));
066: assertEquals("EJIHGFDCBA", cache.dumpLfuKeys());
067: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
068: assertNotNull(cache.getObject("H"));
069: assertEquals("HEJIGFDCBA", cache.dumpLfuKeys());
070: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
071: assertNotNull(cache.getObject("H"));
072: assertEquals("HEJIGFDCBA", cache.dumpLfuKeys());
073: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
074: assertNotNull(cache.getObject("B"));
075: assertEquals("HBEJIGFDCA", cache.dumpLfuKeys());
076: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
077: assertNotNull(cache.getObject("G"));
078: assertEquals("HGBEJIFDCA", cache.dumpLfuKeys());
079: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
080: assertNotNull(cache.getObject("G"));
081: assertEquals("GHBEJIFDCA", cache.dumpLfuKeys());
082: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
083: assertNotNull(cache.getObject("G"));
084: assertEquals("GHBEJIFDCA", cache.dumpLfuKeys());
085: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
086: assertNotNull(cache.getObject("E"));
087: assertEquals("GEHBJIFDCA", cache.dumpLfuKeys());
088: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
089: assertNotNull(cache.getObject("B"));
090: assertEquals("GBEHJIFDCA", cache.dumpLfuKeys());
091: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
092: assertNotNull(cache.getObject("I"));
093: assertEquals("GBEHIJFDCA", cache.dumpLfuKeys());
094: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
095:
096: // observe the effect of adding more nodes
097: cache.addObject("K", "K");
098: assertEquals("GBEHIKJFDC", cache.dumpLfuKeys());
099: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
100: cache.addObject("L", "L");
101: assertEquals("GBEHILKJFD", cache.dumpLfuKeys());
102: assertEquals("LKJIHGFEDB", cache.dumpFifoKeys());
103: cache.addObject("M", "M");
104: assertEquals("GBEHIMLKJF", cache.dumpLfuKeys());
105: assertEquals("MLKJIHGFEB", cache.dumpFifoKeys());
106: cache.addObject("N", "N");
107: assertEquals("GBEHINMLKJ", cache.dumpLfuKeys());
108: assertEquals("NMLKJIHGEB", cache.dumpFifoKeys());
109: cache.addObject("O", "O");
110: assertEquals("GBEHIONMLK", cache.dumpLfuKeys());
111: assertEquals("ONMLKIHGEB", cache.dumpFifoKeys());
112: assertNotNull(cache.getObject("O"));
113: assertEquals("GBEHOINMLK", cache.dumpLfuKeys());
114: assertEquals("ONMLKIHGEB", cache.dumpFifoKeys());
115: cache.addObject("P", "P");
116: assertEquals("GBEHOIPNML", cache.dumpLfuKeys());
117: assertEquals("PONMLIHGEB", cache.dumpFifoKeys());
118: assertNotNull(cache.getObject("P"));
119: assertEquals("GBEHPOINML", cache.dumpLfuKeys());
120: assertEquals("PONMLIHGEB", cache.dumpFifoKeys());
121: assertNotNull(cache.getObject("P"));
122: assertEquals("GPBEHOINML", cache.dumpLfuKeys());
123: assertEquals("PONMLIHGEB", cache.dumpFifoKeys());
124: assertNotNull(cache.getObject("P"));
125: assertEquals("PGBEHOINML", cache.dumpLfuKeys());
126: assertEquals("PONMLIHGEB", cache.dumpFifoKeys());
127: assertNotNull(cache.getObject("P"));
128: assertEquals("PGBEHOINML", cache.dumpLfuKeys());
129: assertEquals("PONMLIHGEB", cache.dumpFifoKeys());
130: cache.addObject("Q", "Q");
131: assertEquals("PGBEHOIQNM", cache.dumpLfuKeys());
132: assertEquals("QPONMIHGEB", cache.dumpFifoKeys());
133: }
134: }
|