This commit is contained in:
Peter Lawrey
2019-05-09 09:17:57 +01:00
parent cdb24d3317
commit 88ff28087b
+49 -24
View File
@@ -4,23 +4,48 @@ import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
@RunWith(Parameterized.class)
public class ValueAlignmentRelocationTest {
private final boolean persisted;
public ValueAlignmentRelocationTest(String name, boolean persisted) {
this.persisted = persisted;
}
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// TODO FIX https://github.com/OpenHFT/Chronicle-Map/issues/169
// {"Volatile", false},
{"Persisted", true}
});
}
@NotNull
private static String toString(final byte[] value) {
return new String(value, 0, 0, value.length);
}
@Test
public void testValueAlignmentRelocation() throws IOException {
File file = Files.createTempFile("test", "cm3").toFile();
file.deleteOnExit();
ChronicleMap<byte[], byte[]> map = ChronicleMapBuilder
ChronicleMapBuilder<byte[], byte[]> builder = ChronicleMapBuilder
.of(byte[].class, byte[].class)
.averageKeySize(5)
.averageValueSize(5)
@@ -29,8 +54,8 @@ public class ValueAlignmentRelocationTest {
.entryAndValueOffsetAlignment(8)
.actualSegments(1)
.actualChunkSize(2)
.entries(10)
.createPersistedTo(file);
.entries(10);
ChronicleMap<byte[], byte[]> map = persisted ? builder.createPersistedTo(file) : builder.create();
Random r = new Random(0);
for (int firstKeySize = 1; firstKeySize < 10; firstKeySize++) {
@@ -56,7 +81,8 @@ public class ValueAlignmentRelocationTest {
map.put(new byte[]{(byte) i}, new byte[]{(byte) i});
map.put(("Hello" + i).getBytes(), "world".getBytes());
}
System.out.println("firstKeySize=" + firstKeySize + ",second key=" + secondKeySize);
// System.out.println("firstKeySize=" + firstKeySize + ",second key=" + secondKeySize);
Assert.assertEquals(Arrays.toString(map.get(firstKey)), Arrays.toString(thirdValue));
Assert.assertTrue(Arrays.equals(map.get(firstKey), thirdValue));
}
}
@@ -65,8 +91,9 @@ public class ValueAlignmentRelocationTest {
@Test
public void testValueAlignmentRelocationNoRandomTest() throws IOException {
File file = Files.createTempFile("test", "cm3").toFile();
file.deleteOnExit();
ChronicleMap<byte[], byte[]> map = ChronicleMapBuilder
ChronicleMapBuilder<byte[], byte[]> builder = ChronicleMapBuilder
.of(byte[].class, byte[].class)
.averageKeySize(5)
.averageValueSize(5)
@@ -75,31 +102,29 @@ public class ValueAlignmentRelocationTest {
.entryAndValueOffsetAlignment(8)
.actualSegments(1)
.actualChunkSize(2)
.entries(10)
.createPersistedTo(file);
.entries(10);
ChronicleMap<byte[], byte[]> map = persisted ? builder.createPersistedTo(file) : builder.create();
byte[] firstKey = "austi".getBytes(ISO_8859_1);
byte[] firstValue = "12345678".getBytes(ISO_8859_1);
byte[] _austi = "austi".getBytes(ISO_8859_1);
byte[] _12345678 = "12345678".getBytes(ISO_8859_1);
byte[] _h = "h".getBytes(ISO_8859_1);
byte[] _a = "a".getBytes(ISO_8859_1);
String expected = "1234567890123456";
byte[] _1234567890123456 = expected.getBytes(ISO_8859_1);
byte[] _Hello = "Hello".getBytes(ISO_8859_1);
byte[] _world = "world".getBytes(ISO_8859_1);
byte[] secondKey = "h".getBytes(ISO_8859_1);
byte[] secondValue = "a".getBytes(ISO_8859_1);
map.put(_austi, _12345678);
map.put(_h, _a);
map.put(firstKey, firstValue);
map.put(secondKey, secondValue);
map.put(_austi, _1234567890123456);
String actual0 = toString(map.get(_austi));
Assert.assertEquals(expected, actual0);
byte[] thirdValue = "1234567890123456".getBytes(ISO_8859_1);
map.put(firstKey, thirdValue);
map.put(("Hello").getBytes(ISO_8859_1), "world".getBytes(ISO_8859_1));
String actual = toString(map.get(firstKey));
String expected = toString(thirdValue);
map.put(_Hello, _world);
String actual = toString(map.get(_austi));
Assert.assertEquals(expected, actual);
}
@NotNull
private String toString(final byte[] value) {
return new String(value, 0, 0, value.length);
}
}