From 88ff28087b590bfdba815e2811fedde54f273e2b Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Thu, 9 May 2019 09:17:57 +0100 Subject: [PATCH] Made failing test more generic, https://github.com/OpenHFT/Chronicle-Map/issues/169 --- .../map/ValueAlignmentRelocationTest.java | 73 +++++++++++++------ 1 file changed, 49 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/test/java/net/openhft/chronicle/map/ValueAlignmentRelocationTest.java diff --git a/src/test/java/net/openhft/chronicle/map/ValueAlignmentRelocationTest.java b/src/test/java/net/openhft/chronicle/map/ValueAlignmentRelocationTest.java old mode 100644 new mode 100755 index da6b3a577..a4ecdb740 --- a/src/test/java/net/openhft/chronicle/map/ValueAlignmentRelocationTest.java +++ b/src/test/java/net/openhft/chronicle/map/ValueAlignmentRelocationTest.java @@ -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 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 map = ChronicleMapBuilder + ChronicleMapBuilder 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 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 map = ChronicleMapBuilder + ChronicleMapBuilder 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 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); - } - }