|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.persistence.varint; |
| 20 | + |
| 21 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 22 | + |
| 23 | +import java.nio.ByteBuffer; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.assertj.core.api.SoftAssertions; |
| 27 | +import org.assertj.core.api.junit.jupiter.InjectSoftAssertions; |
| 28 | +import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.Arguments; |
| 33 | +import org.junit.jupiter.params.provider.MethodSource; |
| 34 | + |
| 35 | +@ExtendWith(SoftAssertionsExtension.class) |
| 36 | +public class TestVarInt { |
| 37 | + @InjectSoftAssertions SoftAssertions soft; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void negative() { |
| 41 | + var buf = ByteBuffer.allocate(9); |
| 42 | + soft.assertThatIllegalArgumentException().isThrownBy(() -> VarInt.putVarInt(buf, -1L)); |
| 43 | + soft.assertThatIllegalArgumentException() |
| 44 | + .isThrownBy(() -> VarInt.putVarInt(buf, Long.MIN_VALUE)); |
| 45 | + } |
| 46 | + |
| 47 | + @ParameterizedTest |
| 48 | + @MethodSource |
| 49 | + public void varInt(long value, byte[] binary) { |
| 50 | + var buf = ByteBuffer.allocate(9); |
| 51 | + VarInt.putVarInt(buf, value); |
| 52 | + soft.assertThat(buf.position()).isEqualTo(binary.length); |
| 53 | + soft.assertThat(Arrays.copyOf(buf.array(), buf.position())).containsExactly(binary); |
| 54 | + soft.assertThat(VarInt.varIntLen(value)).isEqualTo(binary.length); |
| 55 | + |
| 56 | + var read = buf.duplicate().flip(); |
| 57 | + VarInt.skipVarInt(read); |
| 58 | + soft.assertThat(read.position()).isEqualTo(binary.length); |
| 59 | + |
| 60 | + if (value > Integer.MAX_VALUE) { |
| 61 | + soft.assertThatIllegalArgumentException() |
| 62 | + .isThrownBy(() -> VarInt.readVarInt(buf.duplicate().flip())) |
| 63 | + .withMessageStartingWith("Out of range: "); |
| 64 | + soft.assertThat(VarInt.readVarLong(buf.duplicate().flip())).isEqualTo(value); |
| 65 | + } else { |
| 66 | + soft.assertThat(VarInt.readVarInt(buf.duplicate().flip())).isEqualTo(value); |
| 67 | + soft.assertThat(VarInt.readVarLong(buf.duplicate().flip())).isEqualTo(value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void notVarInt() { |
| 73 | + var buf = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 74 | + soft.assertThatIllegalArgumentException() |
| 75 | + .isThrownBy(() -> VarInt.readVarInt(ByteBuffer.wrap(buf))); |
| 76 | + soft.assertThatIllegalArgumentException() |
| 77 | + .isThrownBy(() -> VarInt.skipVarInt(ByteBuffer.wrap(buf))); |
| 78 | + } |
| 79 | + |
| 80 | + static Stream<Arguments> varInt() { |
| 81 | + return Stream.of( |
| 82 | + // one byte |
| 83 | + arguments(0L, new byte[] {0}), |
| 84 | + arguments(1L, new byte[] {1}), |
| 85 | + arguments(42L, new byte[] {42}), |
| 86 | + arguments(127L, new byte[] {127}), |
| 87 | + // 2 bytes |
| 88 | + arguments(128L, new byte[] {(byte) 0x80, 1}), |
| 89 | + // 21 bite -> 3 x 7 bits |
| 90 | + arguments(0x1fffff, new byte[] {-1, -1, 127}), |
| 91 | + // 28 bits -> 4 x 7 bits |
| 92 | + arguments(0xfffffff, new byte[] {-1, -1, -1, 127}), |
| 93 | + // 35 bits -> 5 x 7 bits |
| 94 | + arguments(0x7ffffffffL, new byte[] {-1, -1, -1, -1, 127}), |
| 95 | + arguments(0x321321321L, new byte[] {-95, -90, -56, -119, 50}), |
| 96 | + // 42 bits -> 6 x 7 bits |
| 97 | + arguments(0x3ffffffffffL, new byte[] {-1, -1, -1, -1, -1, 127}), |
| 98 | + // 49 bits -> 7 x 7 bits |
| 99 | + arguments(0x1ffffffffffffL, new byte[] {-1, -1, -1, -1, -1, -1, 127}), |
| 100 | + // 56 bits -> 8 x 7 bits |
| 101 | + arguments(0xffffffffffffffL, new byte[] {-1, -1, -1, -1, -1, -1, -1, 127}), |
| 102 | + arguments(0x32132132132132L, new byte[] {-78, -62, -52, -112, -109, -28, -124, 25}), |
| 103 | + // 63 bits -> 9 x 7 bits |
| 104 | + arguments(Long.MAX_VALUE, new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, 127}), |
| 105 | + arguments( |
| 106 | + Long.MAX_VALUE - 0x1111111111111111L, |
| 107 | + new byte[] {-18, -35, -69, -9, -18, -35, -69, -9, 110})); |
| 108 | + } |
| 109 | +} |
0 commit comments