public void testReserveAndFree() throws Exception { Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < TEST_ITERATIONS; ++i) { int trial = getTrial(rand); if (set.contains(trial)) { iAll.free(trial); set.remove(trial); } else { assertTrue("Did not reserve free integer " + trial, iAll.reserve(trial)); set.add(trial); } } for (int trial : set) { assertFalse("Integer " + trial + " not allocated!", iAll.reserve(trial)); } }
public void testAllocateAndFree() throws Exception { Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < TEST_ITERATIONS; ++i) { if (getBool(rand)) { int trial = iAll.allocate(); assertFalse("Already allocated " + trial, set.contains(trial)); set.add(trial); } else { if (!set.isEmpty()) { int trial = extractOne(set); assertFalse("Allocator agreed to reserve " + trial, iAll.reserve(trial)); iAll.free(trial); } } } for (int trial : set) { assertFalse("Integer " + trial + " should be allocated!", iAll.reserve(trial)); } }
public void testToString() throws Exception { IntAllocator ibs = new IntAllocator(LO_RANGE, HI_RANGE); assertEquals("IntAllocator{allocated = []}", ibs.toString()); ibs.allocate(); assertEquals("IntAllocator{allocated = [100]}", ibs.toString()); for (int i = 200; i < 211; i = i + 4) { ibs.reserve(i); ibs.reserve(i + 1); ibs.reserve(i + 2); } assertEquals("IntAllocator{allocated = [100, 200..202, 204..206, 208..210]}", ibs.toString()); }