@Override
    public int compare(int i, int j) {
        final int segmentNumberI = i / this.recordsPerSegment;
        final int segmentOffsetI = (i % this.recordsPerSegment) * this.recordSize;

        final int segmentNumberJ = j / this.recordsPerSegment;
        final int segmentOffsetJ = (j % this.recordsPerSegment) * this.recordSize;

        return compare(segmentNumberI, segmentOffsetI, segmentNumberJ, segmentOffsetJ);
    }
@Test
    void testCompare() throws Exception {
        final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE;
        final List<MemorySegment> memory =
                this.memoryManager.allocatePages(new DummyInvokable(), numSegments);

        FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory);
        UniformIntPairGenerator generator = new UniformIntPairGenerator(Integer.MAX_VALUE, 1, true);

        // write the records
        IntPair record = new IntPair();
        int num = -1;
        do {
            generator.next(record);
            num++;
        } while (sorter.write(record) && num < 3354624);

        // compare random elements
        Random rnd = new Random(SEED << 1);
        for (int i = 0; i < 2 * num; i++) {
            int pos1 = rnd.nextInt(num);
            int pos2 = rnd.nextInt(num);

            int cmp = sorter.compare(pos1, pos2);

            if (pos1 < pos2) {
                assertThat(cmp).isLessThanOrEqualTo(0);
            } else {
                assertThat(cmp).isGreaterThanOrEqualTo(0);
            }
        }

        // release the memory occupied by the buffers
        sorter.dispose();
        this.memoryManager.release(memory);
    }