### Question:
HeapDisk { public synchronized void allocate(RegularFile file, int count) throws IOException { int newAllocatedBlockCount = allocatedBlockCount + count; if (newAllocatedBlockCount > maxBlockCount) { throw new IOException("out of disk space"); } int newBlocksNeeded = Math.max(count - blockCache.blockCount(), 0); for (int i = 0; i < newBlocksNeeded; i++) { file.addBlock(new byte[blockSize]); } if (newBlocksNeeded != count) { blockCache.transferBlocksTo(file, count - newBlocksNeeded); } allocatedBlockCount = newAllocatedBlockCount; } HeapDisk(Configuration config);  HeapDisk(int blockSize, int maxBlockCount, int maxCachedBlockCount); int blockSize(); synchronized long getTotalSpace(); synchronized long getUnallocatedSpace(); synchronized void allocate(RegularFile file, int count); void free(RegularFile file); synchronized void free(RegularFile file, int count);  }### Answer:
@Test public void testAllocate() throws IOException { HeapDisk disk = new HeapDisk(4, 10, 0); disk.allocate(blocks, 1); assertThat(blocks.blockCount()).isEqualTo(1); assertThat(blocks.getBlock(0).length).isEqualTo(4); assertThat(disk.getUnallocatedSpace()).isEqualTo(36); disk.allocate(blocks, 5); assertThat(blocks.blockCount()).isEqualTo(6); for (int i = 0; i < blocks.blockCount(); i++) { assertThat(blocks.getBlock(i).length).isEqualTo(4); } assertThat(disk.getUnallocatedSpace()).isEqualTo(16); assertThat(disk.blockCache.blockCount()).isEqualTo(0); }
@Test public void testFullDisk() throws IOException { HeapDisk disk = new HeapDisk(4, 10, 4); disk.allocate(blocks, 10); try { disk.allocate(blocks, 1); fail(); } catch (IOException expected) { } }
@Test public void testFullDisk_doesNotAllocatePartiallyWhenTooManyBlocksRequested() throws IOException { HeapDisk disk = new HeapDisk(4, 10, 4); disk.allocate(blocks, 6); RegularFile blocks2 = RegularFile.create(-2, disk); try { disk.allocate(blocks2, 5); fail(); } catch (IOException expected) { } assertThat(blocks2.blockCount()).isEqualTo(0); }