2023-02-22 22:31:36 +01:00
|
|
|
package it.cavallium.dbengine.tests;
|
2021-03-13 19:01:36 +01:00
|
|
|
|
2023-02-22 22:31:36 +01:00
|
|
|
import it.cavallium.dbengine.buffers.Buf;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2023-02-22 22:31:36 +01:00
|
|
|
import it.cavallium.dbengine.database.collections.DatabaseMapDictionaryDeep;
|
2021-03-13 19:01:36 +01:00
|
|
|
import java.util.Arrays;
|
2021-08-31 15:50:11 +02:00
|
|
|
import org.junit.jupiter.api.AfterAll;
|
2021-03-13 19:01:36 +01:00
|
|
|
import org.junit.jupiter.api.Assertions;
|
2021-08-31 15:50:11 +02:00
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
2021-03-13 19:01:36 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
public class TestRanges {
|
2021-04-30 19:15:04 +02:00
|
|
|
|
2021-08-31 15:50:11 +02:00
|
|
|
@BeforeAll
|
|
|
|
public static void beforeAll() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterAll
|
|
|
|
public static void afterAll() {
|
|
|
|
}
|
|
|
|
|
2021-03-13 19:01:36 +01:00
|
|
|
@Test
|
|
|
|
public void testNextRangeKey() {
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x00, 0x00});
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x00, 0x01});
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x00, 0x02});
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x01, 0x02});
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x00, (byte) 0xFF});
|
|
|
|
testNextRangeKey(new byte[] {0x00, 0x01, (byte) 0xFF});
|
|
|
|
testNextRangeKey(new byte[] {0x00, (byte) 0xFF, (byte) 0xFF});
|
2021-08-31 15:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testNextRangeKey2() {
|
2021-03-13 19:01:36 +01:00
|
|
|
testNextRangeKey(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF});
|
|
|
|
testNextRangeKey(new byte[] {(byte) 0xFF, (byte) 0, (byte) 0xFF});
|
|
|
|
testNextRangeKey(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNextRangeKey(byte[] prefixKey) {
|
2021-08-31 15:50:11 +02:00
|
|
|
byte[] firstRangeKey;
|
2023-02-22 22:31:36 +01:00
|
|
|
Buf firstRangeKeyBuf = Buf.create(prefixKey.length);
|
|
|
|
firstRangeKeyBuf.addElements(0, prefixKey);
|
|
|
|
firstRangeKeyBuf = DatabaseMapDictionaryDeep.firstRangeKey(firstRangeKeyBuf, prefixKey.length, Buf.createZeroes(7 + 3));
|
|
|
|
firstRangeKey = firstRangeKeyBuf.asArray();
|
2021-08-31 15:50:11 +02:00
|
|
|
byte[] nextRangeKey;
|
2023-02-22 22:31:36 +01:00
|
|
|
Buf nextRangeKeyBuf = Buf.create(prefixKey.length);
|
|
|
|
nextRangeKeyBuf.addElements(0, prefixKey);
|
|
|
|
nextRangeKeyBuf = DatabaseMapDictionaryDeep.nextRangeKey(nextRangeKeyBuf, prefixKey.length, Buf.createZeroes(7 + 3));
|
|
|
|
nextRangeKey = nextRangeKeyBuf.asArray();
|
2021-03-13 19:01:36 +01:00
|
|
|
|
|
|
|
if (Arrays.equals(prefixKey, new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF})) {
|
2022-03-16 22:41:51 +01:00
|
|
|
org.assertj.core.api.Assertions
|
|
|
|
.assertThat(nextRangeKey)
|
|
|
|
.isEqualTo(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
|
|
|
|
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0});
|
2021-03-13 19:01:36 +01:00
|
|
|
} else {
|
|
|
|
long biPrefix = 0;
|
|
|
|
var s = 0;
|
|
|
|
for (int i = prefixKey.length - 1; i >= 0; i--) {
|
|
|
|
biPrefix += ((long) (prefixKey[i] & 0xFF)) << s;
|
|
|
|
s += Byte.SIZE;
|
|
|
|
}
|
|
|
|
var nrPrefix = Arrays.copyOf(nextRangeKey, prefixKey.length);
|
|
|
|
|
|
|
|
long biNextPrefix = 0;
|
|
|
|
s = 0;
|
|
|
|
for (int i = prefixKey.length - 1; i >= 0; i--) {
|
|
|
|
biNextPrefix += ((long) (nrPrefix[i] & 0xFF)) << s;
|
|
|
|
s += Byte.SIZE;
|
|
|
|
}
|
|
|
|
Assertions.assertEquals(biPrefix + 1, biNextPrefix);
|
|
|
|
Assertions.assertArrayEquals(
|
|
|
|
new byte[7 + 3],
|
|
|
|
Arrays.copyOfRange(nextRangeKey, prefixKey.length, prefixKey.length + 7 + 3)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|