@Override
    public void addAt(long offset, T value) {
        // we consider -1 a legal offset to account for loading values from the 0-0.checkpoint
        if (offset < -1) {
            throw new IllegalArgumentException(
                String.format("Next offset %d must be greater than or equal to -1", offset)
            );
        }

        Map.Entry<Long, ?> lastEntry = history.lastEntry();
        if (lastEntry != null && offset <= lastEntry.getKey()) {
            throw new IllegalArgumentException(
                String.format("Next offset %d must be greater than the last offset %d", offset, lastEntry.getKey())
            );
        }

        history.put(offset, value);
    }
@Test
    void testAddAt() {
        TreeMapLogHistory<String> history = new TreeMapLogHistory<>();
        assertThrows(IllegalArgumentException.class, () -> history.addAt(-2, ""));
        assertEquals(Optional.empty(), history.lastEntry());

        history.addAt(-1, "-1");
        assertEquals(Optional.of("-1"), history.valueAtOrBefore(-1));
        assertEquals(Optional.of("-1"), history.valueAtOrBefore(0));
        history.addAt(100, "100");
        assertThrows(IllegalArgumentException.class, () -> history.addAt(99, ""));
        assertThrows(IllegalArgumentException.class, () -> history.addAt(100, ""));
        assertEquals(Optional.of("100"), history.valueAtOrBefore(100));
        assertEquals(Optional.of("100"), history.valueAtOrBefore(201));

        history.addAt(200, "200");
        assertEquals(Optional.empty(), history.valueAtOrBefore(-2));
        assertEquals(Optional.of("-1"), history.valueAtOrBefore(-1));
        assertEquals(Optional.of("100"), history.valueAtOrBefore(100));
        assertEquals(Optional.of("100"), history.valueAtOrBefore(101));
        assertEquals(Optional.of("100"), history.valueAtOrBefore(199));
        assertEquals(Optional.of("200"), history.valueAtOrBefore(200));
        assertEquals(Optional.of("200"), history.valueAtOrBefore(201));
        assertEquals(Optional.of(new LogHistory.Entry<>(200, "200")), history.lastEntry());
    }