### Question:
HugeSparseLongArray { public long get(long index) { final int pageIndex = pageIndex(index); if (pageIndex < pages.length) { long[] page = pages[pageIndex]; if (page != null) { return page[indexInPage(index)]; } } return defaultValue; } private  HugeSparseLongArray(long capacity, long[][] pages, long defaultValue); static MemoryRange memoryEstimation(long maxId, long maxEntries); long getCapacity(); long get(long index); boolean contains(long index);  }### Answer:
@Test void shouldSetAndGet() { HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(10, AllocationTracker.empty()); int index = integer(2, 8); int value = integer(42, 1337); array.set(index, value); assertEquals(value, array.build().get(index)); }
@Test void shouldAddAndGet() { HugeSparseLongArray.GrowingBuilder array = HugeSparseLongArray.GrowingBuilder.create(0L, AllocationTracker.empty() ); int index = integer(2, 8); int value = integer(42, 1337); array.addTo(index, value); assertEquals(value, array.build().get(index)); }
@Test void shouldReturnNegativeOneForOutOfRangeValues() { HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(10, AllocationTracker.empty()); array.set(integer(2, 8), integer(42, 1337)); assertEquals(-1L, array.build().get(integer(100, 200))); }
@Test void shouldReturnNegativeOneForUnsetPages() { HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(PS + 10, AllocationTracker.empty()); array.set(5000, integer(42, 1337)); assertEquals(-1L, array.build().get(integer(100, 200))); }
@Test void shouldNotFailOnGetsThatAreoutOfBounds() { HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(10, AllocationTracker.empty()); array.set(integer(2, 8), integer(42, 1337)); assertEquals(-1L, array.build().get(integer(100_000_000, 200_000_000))); }
@Test void shouldNotCreateAnyPagesOnInitialization() throws Throwable { AllocationTracker tracker = AllocationTracker.create(); HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(2 * PS, tracker); final AtomicReferenceArray<long[]> pages = getPages(array); for (int i = 0; i < pages.length(); i++) { long[] page = pages.get(i); assertNull(page); } long tracked = tracker.trackedBytes(); assertEquals(sizeOfObjectArray(2), tracked); }
@Test void shouldCreateAndTrackSparsePagesOnDemand() throws Throwable { AllocationTracker tracker = AllocationTracker.create(); HugeSparseLongArray.Builder array = HugeSparseLongArray.Builder.create(2 * PS, tracker); int index = integer(PS, 2 * PS - 1); int value = integer(42, 1337); array.set(index, value); final AtomicReferenceArray<long[]> pages = getPages(array); for (int i = 0; i < pages.length(); i++) { if (i == 1) { assertNotNull(pages.get(i)); } else { assertNull(pages.get(i)); } } long tracked = tracker.trackedBytes(); assertEquals(sizeOfObjectArray(2) + sizeOfLongArray(PS), tracked); }