### Question:
AttrStore { Object get(int idx) { assert idx >= 0 && idx < totalSize; return shards.get(Shard.indexOfShard(idx)) .get(Shard.indexWithinShard(idx)); } AttrStore(List<Shard> shards, int total);   }### Answer:
@Test public void testSwap() { AttrStoreBuilder asb = new AttrStoreBuilder(); asb.add("foo"); asb.add("bar"); asb.swap(0, 1); assertThat(asb.get(0), equalTo("bar")); assertThat(asb.get(1), equalTo("foo")); AttrStore as = asb.build(); assertThat(as.get(0), equalTo("bar")); assertThat(as.get(1), equalTo("foo")); }
@Test public void testSwapSkip() { AttrStoreBuilder asb = new AttrStoreBuilder(); asb.add("foo"); asb.skip(); asb.swap(0, 1); assertThat(asb.get(0), nullValue()); assertThat(asb.get(1), equalTo("foo")); AttrStore as = asb.build(); assertThat(as.get(0), nullValue()); assertThat(as.get(1), equalTo("foo")); }
@Test public void testSwapAcrossShards() { AttrStoreBuilder asb = new AttrStoreBuilder(); for (int i = 0; i < Shard.SHARD_SIZE + 50; i++) { asb.add(Integer.toString(i)); } asb.swap(0, Shard.SHARD_SIZE + 2); assertThat(asb.get(0), equalTo(Integer.toString(Shard.SHARD_SIZE + 2))); assertThat(asb.get(Shard.SHARD_SIZE + 2), equalTo("0")); AttrStore as = asb.build(); assertThat(as.get(0), equalTo(Integer.toString(Shard.SHARD_SIZE + 2))); assertThat(as.get(Shard.SHARD_SIZE + 2), equalTo("0")); }
@Test public void testSwapAndUpgrade() { AttrStoreBuilder asb = new AttrStoreBuilder(LongShard::create); for (long i = 0; i < Shard.SHARD_SIZE; i++) { asb.add(i); } for (long i = 0; i < 50; i++) { asb.add(Integer.MAX_VALUE + i); } asb.swap(0, Shard.SHARD_SIZE + 2); assertThat(asb.get(0), equalTo(Integer.MAX_VALUE + 2L)); assertThat(asb.get(Shard.SHARD_SIZE + 2), equalTo(0L)); AttrStore as = asb.build(); assertThat(as.get(0), equalTo(Integer.MAX_VALUE + 2L)); assertThat(as.get(Shard.SHARD_SIZE + 2), equalTo(0L)); }
@Test public void testSwapAndUpgradeReverse() { AttrStoreBuilder asb = new AttrStoreBuilder(LongShard::create); for (long i = 0; i < Shard.SHARD_SIZE; i++) { asb.add(i); } for (long i = 0; i < 50; i++) { asb.add(Integer.MAX_VALUE + i); } asb.swap(Shard.SHARD_SIZE + 2, 0); assertThat(asb.get(0), equalTo(Integer.MAX_VALUE + 2L)); assertThat(asb.get(Shard.SHARD_SIZE + 2), equalTo(0L)); AttrStore as = asb.build(); assertThat(as.get(0), equalTo(Integer.MAX_VALUE + 2L)); assertThat(as.get(Shard.SHARD_SIZE + 2), equalTo(0L)); }