public void testSeek()

in computer/computer-test/src/main/java/org/apache/hugegraph/computer/core/store/ValueFileTest.java [202:309]


    public void testSeek() throws IOException {
        File dir = createTempDir();
        final int bufferSize = 15;

        try {
            // Test ValueFileInput
            try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir,
                                                              bufferSize)) {
                for (int i = 0; i < 50; i++) {
                    output.writeInt(i);
                }
            }

            try (ValueFileInput input = new ValueFileInput(CONFIG, dir,
                                                           bufferSize)) {
                Long fileLength = Whitebox.getInternalState(input,
                                                            "fileLength");
                Assert.assertEquals(200L, fileLength);

                long position;
                // Position in buffer
                position = 13L;
                assertSeek(fileLength, input, position);

                // Position not in buffer but in current segment
                position = 28L;
                assertSeek(fileLength, input, position);

                // Position not in buffer and not in current segment
                position = 200L;
                assertSeek(fileLength, input, position);
                position = 3L;
                assertSeek(fileLength, input, position);
                position = 3L;
                assertSeek(fileLength, input, position);
                position = 68L;
                assertSeek(fileLength, input, position);
                position = 0L;
                assertSeek(fileLength, input, position);

                // Position out of bound
                position = 300L;
                long finalPosition = position;
                Assert.assertThrows(EOFException.class, () -> {
                    input.seek(finalPosition);
                }, e -> {
                    Assert.assertContains("reach the end of file",
                                          e.getMessage());
                });

                input.seek(16 * Integer.BYTES);
                Assert.assertEquals(16, input.readInt());

                // Random seek and assert data
                Random random = new Random();
                for (int i = 0; i < 10; i++) {
                    int num = random.nextInt(49);
                    input.seek(num * Integer.BYTES);
                    Assert.assertEquals(num, input.readInt());
                }
            }

            // Test ValueFileOutput
            try (ValueFileOutput output = new ValueFileOutput(CONFIG, dir,
                                                              bufferSize)) {
                // 200, 100, 2, 3, 4.....
                for (int i = 0; i < 50; i++) {
                    // Position in buffer
                    if (i == 2) {
                        output.seek(4);
                        output.writeInt(100);
                    }
                    // Position not int buffer
                    if (i == 10) {
                        long oldPosition = output.position();
                        output.seek(0);
                        output.writeInt(200);
                        output.seek(oldPosition);
                    }
                    output.writeInt(i);
                }

                Assert.assertThrows(EOFException.class, () -> {
                    output.seek(500L);
                }, e -> {
                    Assert.assertContains("reach the end of file",
                                          e.getMessage());
                });
            }

            try (ValueFileInput input = new ValueFileInput(CONFIG, dir,
                                                           bufferSize)) {
                for (int i = 0; i < 50; i++) {
                    if (i == 0) {
                        Assert.assertEquals(200, input.readInt());
                        continue;
                    }
                    if (i == 1) {
                        Assert.assertEquals(100, input.readInt());
                        continue;
                    }
                    Assert.assertEquals(i, input.readInt());
                }
            }
        } finally {
            FileUtils.deleteQuietly(dir);
        }
    }