@Test public void iterate() { index.insert(newBlock("a", 1)); index.insert(newBlock("c", 1)); index.insert(newBlock("b", 1)); index.insert(newBlock("c", 2)); index.insert(newBlock("a", 2)); Iterator<ResourceBlocks> it = index.iterator(); ArrayList<ResourceBlocks> resourcesBlocks = new ArrayList<>(); while(it.hasNext()) { resourcesBlocks.add(it.next()); } assertThat(resourcesBlocks).hasSize(3); assertThat(resourcesBlocks.get(0).resourceId()).isEqualTo("a"); assertThat(resourcesBlocks.get(1).resourceId()).isEqualTo("b"); assertThat(resourcesBlocks.get(2).resourceId()).isEqualTo("c"); assertThat(resourcesBlocks.get(0).blocks()).hasSize(2); assertThat(resourcesBlocks.get(1).blocks()).hasSize(1); assertThat(resourcesBlocks.get(2).blocks()).hasSize(2); }
/** * Given: index with initial capacity 1. * Expected: size and capacity should be increased after insertion of two blocks. */ @Test public void should_increase_capacity() { CloneIndex index = new PackedMemoryCloneIndex(8, 1); index.insert(newBlock("a", 1)); index.insert(newBlock("a", 2)); assertThat(index.getByResourceId("a").size(), is(2)); }
/** * Given: index, which accepts blocks with 4-byte hash. * Expected: exception during insertion of block with 8-byte hash. */ @Test(expected = IllegalArgumentException.class) public void attempt_to_insert_hash_of_incorrect_size() { CloneIndex index = new PackedMemoryCloneIndex(4, 1); index.insert(newBlock("a", 1)); }
@Test public void test() { index.insert(newBlock("a", 1)); index.insert(newBlock("a", 2)); index.insert(newBlock("b", 1)); index.insert(newBlock("c", 1)); index.insert(newBlock("d", 1)); index.insert(newBlock("e", 1)); index.insert(newBlock("e", 2)); index.insert(newBlock("e", 3)); assertThat(index.noResources()).isEqualTo(5); assertThat(index.getBySequenceHash(new ByteArray(1L)).size(), is(5)); assertThat(index.getBySequenceHash(new ByteArray(2L)).size(), is(2)); assertThat(index.getBySequenceHash(new ByteArray(3L)).size(), is(1)); assertThat(index.getBySequenceHash(new ByteArray(4L)).size(), is(0)); assertThat(index.getByResourceId("a").size(), is(2)); assertThat(index.getByResourceId("b").size(), is(1)); assertThat(index.getByResourceId("e").size(), is(3)); assertThat(index.getByResourceId("does not exist").size(), is(0)); }
/** * When: query by a hash value. * Expected: all blocks should have same hash, which presented in the form of the same object. */ @Test public void should_construct_blocks_with_normalized_hash() { index.insert(newBlock("a", 1)); index.insert(newBlock("b", 1)); index.insert(newBlock("c", 1)); ByteArray requestedHash = new ByteArray(1L); Collection<Block> blocks = index.getBySequenceHash(requestedHash); assertThat(blocks.size(), is(3)); for (Block block : blocks) { assertThat(block.getBlockHash(), sameInstance(requestedHash)); } }