@Override
    public void swap(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;

        swap(segmentNumberI, segmentOffsetI, segmentNumberJ, segmentOffsetJ);
    }
@Test
    void testSwap() 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);
        RandomIntPairGenerator generator = new RandomIntPairGenerator(SEED);

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

        // swap the records
        int start = 0, end = num - 1;
        while (start < end) {
            sorter.swap(start++, end--);
        }

        // re-read the records
        generator.reset();
        IntPair readTarget = new IntPair();

        int i = num - 1;
        while (i >= 0) {
            generator.next(record);
            readTarget = sorter.getRecord(readTarget, i--);

            int rk = readTarget.getKey();
            int gk = record.getKey();

            int rv = readTarget.getValue();
            int gv = record.getValue();

            assertThat(rk).withFailMessage("The re-read key is wrong %d", i).isEqualTo(gk);
            assertThat(rv).withFailMessage("The re-read value is wrong %d", i).isEqualTo(gv);
        }

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