📄 loctest.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// Copyright (C) 2004 Leo Mekenkamp. All rights reserved.//// $Id$package test.ozoneDB.core.storage.gammaStore;import java.util.HashSet;import java.util.Random;import java.util.Set;import java.util.TreeSet;import junit.framework.Test;import junit.framework.TestResult;import junit.framework.TestSuite;import junit.textui.TestRunner;import org.ozoneDB.core.storage.gammaStore.Loc;import test.OzoneTestCase;import test.OzoneTestRunner;/** * @author leo */public class LocTest extends OzoneTestCase { private int capacity; private long range; private int slack; private Loc loc; private Set mirror; private Random random = new Random(0); public LocTest() { super("testLoc"); } public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new LocTest()); return suite; } public void testLoc() { capacity = 4; slack = 0; range = capacity * 2; execute(); slack = 1; execute(); slack = 3; execute(); capacity = 13; slack = 0; range = capacity * 2; execute(); slack = 5; execute(); slack = 8; execute(); capacity = 100; slack = 0; range = capacity + 10; execute(); slack = 2000; execute(); } private void execute() { loc = new Loc(capacity, slack); mirror = new TreeSet(); checkEquality(); randomFill(capacity); checkEquality(); checkEquality(); randomRemove(capacity / 2); checkEquality(); randomFill(capacity); checkEquality(); randomRemove(0); checkEquality(); randomFill(capacity / 2 ); checkEquality(); randomRemove(capacity / 4); checkEquality(); randomRemove(0); checkEquality(); } private void randomFill(int targetSize) { while (mirror.size() < targetSize) { assertEquals(loc.size(), mirror.size()); Long value = new Long(random.nextLong() % range); boolean reallyAdded = mirror.add(value); assertTrue(reallyAdded != (loc.getKeyPos(value.longValue()) >= 0)); loc.putKey(value.longValue()); } } private void randomRemove(int targetSize) { while (loc.size() > targetSize) { assertTrue("loc size: " + loc.size() + "; mirror size: " + mirror.size() +"\nloc:\n" + loc + "\nmirror:\n" + mirror, loc.size() == mirror.size()); Long value = new Long(random.nextLong() % range); boolean reallyRemoved = mirror.remove(value); assertTrue(reallyRemoved == (loc.removeKey(value.longValue()) >= 0)); if (!reallyRemoved) { value = (Long) mirror.iterator().next(); mirror.remove(value); assertTrue(loc.removeKey(value.longValue()) >= 0); } } } private void checkEquality() { checkFull(mirror.size() == capacity); for (int i = 0; i < capacity * 3; i++) { Long value = new Long(random.nextLong() % range); int pos = loc.getKeyPos(value.longValue()); if (pos < 0) { assertFalse("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value)); } else { assertTrue("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value)); } } } private void checkFull(boolean isFull) { try { // RANGE + 1 is never put into the Loc loc.putKey(range + 1); assertFalse(isFull); loc.removeKey(range + 1); } catch (IndexOutOfBoundsException e) { assertTrue("should not throw IndexOutOfBoundsException, because not full\n" + loc, isFull); } } public static void main(String[] args) {// LocTest locTest = new LocTest();// locTest.run(new TestResult()); TestRunner testRunner = new TestRunner(); testRunner.run(LocTest.class); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -