mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
More Listeners tests
This commit is contained in:
@@ -26,26 +26,42 @@ import com.google.common.collect.testing.features.CollectionSize;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import org.junit.Ignore;
|
||||
import net.openhft.chronicle.hash.Value;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.google.common.collect.testing.MapTestSuiteBuilder.using;
|
||||
import static com.google.common.collect.testing.features.MapFeature.*;
|
||||
|
||||
public class GuavaTest extends TestCase {
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite hhmTests = MapTestSuiteBuilder.using(new HHMTestGenerator())
|
||||
.named("HugeHashMap Guava tests")
|
||||
.withFeatures(GENERAL_PURPOSE)
|
||||
MapTestSuiteBuilder<CharSequence, CharSequence> chmSuite = using(new CHMTestGenerator());
|
||||
configureSuite(chmSuite);
|
||||
TestSuite chmTests = chmSuite.named("Guava tests of Chronicle Map").createTestSuite();
|
||||
|
||||
MapTestSuiteBuilder<CharSequence, CharSequence> backed = using(new BackedUpMapGenerator());
|
||||
configureSuite(backed);
|
||||
TestSuite backedTests = backed
|
||||
.named("Guava tests tests of Chronicle Map, backed with HashMap")
|
||||
.createTestSuite();
|
||||
|
||||
TestSuite tests = new TestSuite();
|
||||
tests.addTest(chmTests);
|
||||
// TODO
|
||||
//tests.addTest(backedTests);
|
||||
return tests;
|
||||
}
|
||||
|
||||
private static void configureSuite(MapTestSuiteBuilder<CharSequence, CharSequence> suite) {
|
||||
suite.withFeatures(GENERAL_PURPOSE)
|
||||
.withFeatures(CollectionSize.ANY)
|
||||
.withFeatures(CollectionFeature.REMOVE_OPERATIONS)
|
||||
.withFeatures(RESTRICTS_KEYS, RESTRICTS_VALUES)
|
||||
.createTestSuite();
|
||||
TestSuite tests = new TestSuite();
|
||||
tests.addTest(hhmTests);
|
||||
return tests;
|
||||
.withFeatures(RESTRICTS_KEYS, RESTRICTS_VALUES);
|
||||
}
|
||||
|
||||
static abstract class TestGenerator
|
||||
@@ -109,10 +125,36 @@ public class GuavaTest extends TestCase {
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
||||
static class HHMTestGenerator extends CHMTestGenerator {
|
||||
|
||||
static class BackedUpMapGenerator extends CHMTestGenerator {
|
||||
|
||||
@Override
|
||||
Map<CharSequence, CharSequence> newMap() {
|
||||
Map<CharSequence, CharSequence> m = new HashMap<>();
|
||||
builder.entryOperations(new MapEntryOperations<CharSequence, CharSequence, Void>() {
|
||||
@Override
|
||||
public Void remove(@NotNull MapEntry<CharSequence, CharSequence> entry) {
|
||||
Assert.assertEquals(m, entry.context().map());
|
||||
m.remove(entry.key().get().toString());
|
||||
return MapEntryOperations.super.remove(entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void replaceValue(@NotNull MapEntry<CharSequence, CharSequence> entry,
|
||||
Value<CharSequence, ?> newValue) {
|
||||
Assert.assertEquals(m, entry.context().map());
|
||||
m.put(entry.key().get().toString(), newValue.get().toString());
|
||||
return MapEntryOperations.super.replaceValue(entry, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void insert(@NotNull MapAbsentEntry<CharSequence, CharSequence> absentEntry,
|
||||
Value<CharSequence, ?> value) {
|
||||
Assert.assertEquals(m, absentEntry.context().map());
|
||||
m.put(absentEntry.absentKey().get().toString(), value.get().toString());
|
||||
return MapEntryOperations.super.insert(absentEntry, value);
|
||||
}
|
||||
});
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.chronicle.hash.Value;
|
||||
import net.openhft.chronicle.map.impl.NullReturnValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -8,8 +9,7 @@ import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ListenersTest {
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ListenersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
public void testAnyRemove() {
|
||||
CountingEntryOperations<Integer, Integer> removeCounting =
|
||||
new CountingEntryOperations<>();
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
@@ -64,7 +64,7 @@ public class ListenersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPut() {
|
||||
public void testAnyPut() {
|
||||
CountingEntryOperations<Integer, Integer> putCounting =
|
||||
new CountingEntryOperations<>();
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
@@ -85,4 +85,99 @@ public class ListenersTest {
|
||||
assertEquals(3, putCounting.replaceValueCount.get());
|
||||
assertEquals(2, putCounting.insertCount.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsKey() {
|
||||
AtomicInteger c = new AtomicInteger();
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
ChronicleMapBuilder.of(Integer.class, Integer.class)
|
||||
.entries(100)
|
||||
.mapMethods(new MapMethods<Integer, Integer, Void>() {
|
||||
@Override
|
||||
public boolean containsKey(MapQueryContext<Integer, Integer, Void> q) {
|
||||
if (q.queriedKey().get() == 2)
|
||||
return false;
|
||||
c.incrementAndGet();
|
||||
return MapMethods.super.containsKey(q);
|
||||
}
|
||||
})
|
||||
.create();
|
||||
|
||||
assertFalse(map.containsKey(1)); // 1
|
||||
map.put(1, 1);
|
||||
assertTrue(map.containsKey(1)); // 2
|
||||
|
||||
map.put(2, 2);
|
||||
assertFalse(map.containsKey(2));
|
||||
|
||||
assertEquals(2, c.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
AtomicInteger c = new AtomicInteger();
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
ChronicleMapBuilder.of(Integer.class, Integer.class)
|
||||
.entries(100)
|
||||
.mapMethods(new MapMethods<Integer, Integer, Void>() {
|
||||
@Override
|
||||
public void get(MapQueryContext<Integer, Integer, Void> q,
|
||||
ReturnValue<Integer> returnValue) {
|
||||
if (q.queriedKey().get() == 2) {
|
||||
returnValue.returnValue(q.wrapValueAsValue(42));
|
||||
return;
|
||||
}
|
||||
c.incrementAndGet();
|
||||
MapMethods.super.get(q, returnValue);
|
||||
}
|
||||
})
|
||||
.create();
|
||||
|
||||
assertNull(map.get(1)); // 1
|
||||
map.put(1, 1);
|
||||
assertEquals(1, map.get(1).intValue()); // 2
|
||||
|
||||
map.put(2, 2);
|
||||
assertEquals(42, map.get(2).intValue());
|
||||
|
||||
assertEquals(2, c.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPut() {
|
||||
AtomicInteger c = new AtomicInteger();
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
ChronicleMapBuilder.of(Integer.class, Integer.class)
|
||||
.entries(100)
|
||||
.mapMethods(new MapMethods<Integer, Integer, Void>() {
|
||||
@Override
|
||||
public void put(MapQueryContext<Integer, Integer, Void> q,
|
||||
Value<Integer, ?> value,
|
||||
ReturnValue<Integer> returnValue) {
|
||||
if (q.queriedKey().get() == 2) {
|
||||
MapMethods.super.put(q, value, NullReturnValue.get());
|
||||
return;
|
||||
}
|
||||
if (q.queriedKey().get() == 3) {
|
||||
MapMethods.super.put(q, q.wrapValueAsValue(value.get() + 1),
|
||||
returnValue);
|
||||
return;
|
||||
}
|
||||
c.incrementAndGet();
|
||||
MapMethods.super.put(q, value, returnValue);
|
||||
}
|
||||
})
|
||||
.create();
|
||||
|
||||
assertNull(map.put(1, 1)); // 1
|
||||
assertEquals(1, map.put(1, 2).intValue()); // 2
|
||||
|
||||
assertNull(map.put(2, 1));
|
||||
assertNull(map.put(2, 2));
|
||||
|
||||
assertNull(map.put(3, 1));
|
||||
assertEquals(2, map.get(3).intValue());
|
||||
|
||||
assertEquals(2, c.get());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user