📄 hardfastcachetest.java
字号:
package com.aliasi.test.unit.util;import com.aliasi.test.unit.BaseTestCase;import com.aliasi.util.HardFastCache;public class HardFastCacheTest extends BaseTestCase { public void testOne() { int numIts = 100000; HardFastCache cache = new HardFastCache(5000000,1.0); for (long i = 0; i < numIts; ++i) cache.put(new Long(i), new int[(int)(i % 10L)]); for (long i = 0; i < numIts; ++i) assertEquals((int)(i % 10L), ((int[]) cache.get(new Long(i))).length); } public void testRecover() { // shouldn't blow out memory int megabyte = 1000000; int numMegabytes = 100; HardFastCache cache = new HardFastCache(5000000,1.0); for (int i = 0; i < numMegabytes; ++i) cache.put(new Integer(i), new int[megabyte]); succeed(); } public void testConstrux() { try { new HardFastCache(0); fail(); } catch (IllegalArgumentException e) { succeed(); } try { new HardFastCache(1,0.0); fail(); } catch (IllegalArgumentException e) { succeed(); } } public void testPrune() { HardFastCache cache = new HardFastCache(150,0.5); int max = 10000; for (int i = 0; i < max; ++i) cache.put(new Integer(i),new Integer(i/2)); assertTrue(cache.size() < 75); } public void testMulti() throws InterruptedException { HardFastCache cache = new HardFastCache(1000000,1.0); int numThreads = 2; // 16; // 128; int numEntries = 8; // 128; // 1024; Thread[] threads = new Thread[numThreads]; for (int i = 0; i < numThreads; ++i) { threads[i] = new Thread(new TestCache(cache,numEntries)); threads[i].start(); } for (int i = 0; i < numThreads; ++i) { threads[i].join(); } for (int i = 0; i < numEntries; ++i) { Integer val = (Integer) cache.get(new Integer(i)); if (val == null) continue; assertEquals(val, new Integer(i/2)); } } private static class TestCache implements Runnable { final HardFastCache mCache; int mNum; TestCache(HardFastCache cache, int num) { mCache = cache; mNum = num; } public void run() { for (int i = 0; i < mNum; ++i) mCache.put(new Integer(i), new Integer(i/2)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -