1 8 package test.ozoneDB.core.storage.gammaStore; 9 10 import java.util.HashSet ; 11 import java.util.Random ; 12 import java.util.Set ; 13 import java.util.TreeSet ; 14 import junit.framework.Test; 15 import junit.framework.TestResult; 16 import junit.framework.TestSuite; 17 import junit.textui.TestRunner; 18 import org.ozoneDB.core.storage.gammaStore.Loc; 19 import test.OzoneTestCase; 20 import test.OzoneTestRunner; 21 22 25 public class LocTest extends OzoneTestCase { 26 27 private int capacity; 28 private long range; 29 private int slack; 30 31 private Loc loc; 32 private Set mirror; 33 private Random random = new Random (0); 34 35 public LocTest() { 36 super("testLoc"); 37 } 38 39 public static Test suite() { 40 TestSuite suite = new TestSuite(); 41 42 suite.addTest(new LocTest()); 43 return suite; 44 } 45 46 public void testLoc() { 47 capacity = 4; 48 slack = 0; 49 range = capacity * 2; 50 execute(); 51 slack = 1; 52 execute(); 53 slack = 3; 54 execute(); 55 56 capacity = 13; 57 slack = 0; 58 range = capacity * 2; 59 execute(); 60 slack = 5; 61 execute(); 62 slack = 8; 63 execute(); 64 65 capacity = 100; 66 slack = 0; 67 range = capacity + 10; 68 execute(); 69 slack = 2000; 70 execute(); 71 } 72 73 private void execute() { 74 75 loc = new Loc(capacity, slack); 76 mirror = new TreeSet (); 77 78 checkEquality(); 79 80 randomFill(capacity); 81 82 checkEquality(); 83 84 checkEquality(); 85 86 randomRemove(capacity / 2); 87 88 checkEquality(); 89 90 randomFill(capacity); 91 92 checkEquality(); 93 94 randomRemove(0); 95 96 checkEquality(); 97 98 randomFill(capacity / 2 ); 99 100 checkEquality(); 101 102 randomRemove(capacity / 4); 103 104 checkEquality(); 105 106 randomRemove(0); 107 108 checkEquality(); 109 } 110 111 private void randomFill(int targetSize) { 112 while (mirror.size() < targetSize) { 113 assertEquals(loc.size(), mirror.size()); 114 Long value = new Long (random.nextLong() % range); 115 boolean reallyAdded = mirror.add(value); 116 assertTrue(reallyAdded != (loc.getKeyPos(value.longValue()) >= 0)); 117 loc.putKey(value.longValue()); 118 } 119 } 120 121 private void randomRemove(int targetSize) { 122 while (loc.size() > targetSize) { 123 assertTrue("loc size: " + loc.size() + "; mirror size: " + mirror.size() 124 +"\nloc:\n" + loc + "\nmirror:\n" + mirror, loc.size() == mirror.size()); 125 Long value = new Long (random.nextLong() % range); 126 boolean reallyRemoved = mirror.remove(value); 127 assertTrue(reallyRemoved == (loc.removeKey(value.longValue()) >= 0)); 128 if (!reallyRemoved) { 129 value = (Long ) mirror.iterator().next(); 130 mirror.remove(value); 131 assertTrue(loc.removeKey(value.longValue()) >= 0); 132 } 133 } 134 } 135 136 private void checkEquality() { 137 checkFull(mirror.size() == capacity); 138 for (int i = 0; i < capacity * 3; i++) { 139 Long value = new Long (random.nextLong() % range); 140 int pos = loc.getKeyPos(value.longValue()); 141 if (pos < 0) { 142 assertFalse("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value)); 143 } else { 144 assertTrue("value: " + value + "loc:\n " + loc + "\nmirror:\n" + mirror, mirror.contains(value)); 145 } 146 } 147 } 148 149 private void checkFull(boolean isFull) { 150 try { 151 loc.putKey(range + 1); 153 assertFalse(isFull); 154 loc.removeKey(range + 1); 155 } catch (IndexOutOfBoundsException e) { 156 assertTrue("should not throw IndexOutOfBoundsException, because not full\n" + loc, isFull); 157 } 158 } 159 160 161 162 public static void main(String [] args) { 163 TestRunner testRunner = new TestRunner(); 166 testRunner.run(LocTest.class); 167 } 168 169 } 170 | Popular Tags |