/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package net.openhft.chronicle.map;
import com.google.common.collect.HashBiMap;
import com.google.common.primitives.Ints;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.hash.Data;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.model.DataValueGenerator;
import net.openhft.lang.values.IntValue;
import net.openhft.lang.values.LongValue;
import net.openhft.lang.values.LongValue$$Native;
import org.junit.Ignore;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import static net.openhft.chronicle.map.Alignment.*;
import static org.junit.Assert.*;
@SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"})
public class ChronicleMapTest {
static long count = 0;
private StringBuilder sb = new StringBuilder();
static void assertKeySet(Set keySet, int[] expectedKeys) {
Set expectedSet = new HashSet();
for (int expectedKey : expectedKeys) {
expectedSet.add(expectedKey);
}
assertEquals(expectedSet, keySet);
}
static void assertValues(Collection values, CharSequence[] expectedValues) {
List expectedList = new ArrayList();
for (CharSequence expectedValue : expectedValues) {
expectedList.add(expectedValue.toString());
}
Collections.sort(expectedList);
List actualList = new ArrayList();
for (CharSequence actualValue : values) {
actualList.add(actualValue.toString());
}
Collections.sort(actualList);
assertEquals(expectedList, actualList);
}
static void assertEntrySet(Set> entrySet, int[] expectedKeys, CharSequence[] expectedValues) {
Set> expectedSet = new HashSet>();
for (int i = 0; i < expectedKeys.length; i++) {
expectedSet.add(new AbstractMap.SimpleEntry(expectedKeys[i], expectedValues[i]));
}
assertEquals(expectedSet, entrySet);
}
static void assertMap(Map map, int[] expectedKeys, CharSequence[] expectedValues) {
assertEquals(expectedKeys.length, map.size());
for (int i = 0; i < expectedKeys.length; i++) {
assertEquals("On position " + i, expectedValues[i], map.get(expectedKeys[i]));
}
}
public static LongValue nativeLongValue() {
return new LongValue$$Native();
}
public static IntValue nativeIntValue() {
return DataValueClasses.newDirectReference(IntValue.class);
// return new LongValue$$Native();
}
static File getPersistenceFile() {
String TMP = System.getProperty("java.io.tmpdir");
File file = new File(TMP + "/chm-test" + System.nanoTime() + count++);
file.deleteOnExit();
return file;
}
private ChronicleMap getViewTestMap(int noOfElements) throws IOException {
ChronicleMap map =
ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(noOfElements * 2 + 100)
.averageValueSize((noOfElements + "").length())
.putReturnsNull(true)
.removeReturnsNull(true).create();
int[] expectedKeys = new int[noOfElements];
String[] expectedValues = new String[noOfElements];
for (int i = 1; i <= noOfElements; i++) {
String value = "" + i;
map.put(i, value);
expectedKeys[i - 1] = i;
expectedValues[i - 1] = value;
}
return map;
}
private static void printStatus() {
if (!new File("/proc/self/status").exists()) return;
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/self/status"));
for (String line; (line = br.readLine()) != null; )
if (line.startsWith("Vm"))
System.out.print(line.replaceAll(" +", " ") + ", ");
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testRemoveWithKey() {
try (final ChronicleMap map =
ChronicleMapBuilder
.of(CharSequence.class, CharSequence.class)
.minSegments(2).create()) {
assertFalse(map.containsKey("key3"));
map.put("key1", "one");
map.put("key2", "two");
assertEquals(2, map.size());
assertTrue(map.containsKey("key1"));
assertTrue(map.containsKey("key2"));
assertFalse(map.containsKey("key3"));
assertEquals("one", map.get("key1"));
assertEquals("two", map.get("key2"));
final CharSequence result = map.remove("key1");
assertEquals(1, map.size());
assertEquals("one", result);
assertFalse(map.containsKey("key1"));
assertEquals(null, map.get("key1"));
assertEquals("two", map.get("key2"));
assertFalse(map.containsKey("key3"));
// lets add one more item for luck !
map.put("key3", "three");
assertEquals("three", map.get("key3"));
assertTrue(map.containsKey("key3"));
assertEquals(2, map.size());
// and just for kicks we'll overwrite what we have
map.put("key3", "overwritten");
assertEquals("overwritten", map.get("key3"));
assertTrue(map.containsKey("key3"));
assertEquals(2, map.size());
}
}
@Test
public void testByteArrayPersistenceFileReuse() throws IOException {
final File persistenceFile = Builder.getPersistenceFile();
for (int i = 0; i < 3; i++) {
try (ChronicleMap map = ChronicleMapBuilder.of(byte[].class, byte[]
.class).createPersistedTo(persistenceFile)) {
byte[] o = map.get("hello".getBytes());
System.out.println(o == null ? "null" : new String(o));
map.put("hello".getBytes(), "world".getBytes());
}
}
persistenceFile.delete();
}
@Test
public void testEqualsCharSequence() {
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class);
try (final ChronicleMap map1 = builder.create()) {
map1.put("hello", "world");
try (final ChronicleMap map2 = builder.create()) {
map2.put("hello", "world");
assertEquals(map1, map2);
}
}
}
@Test
public void testEqualsCharArray() {
char[] value = new char[5];
Arrays.fill(value, 'X');
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, char[].class);
try (final ChronicleMap map1 = builder.create()) {
map1.put("hello", value);
try (final ChronicleMap map2 = builder.create()) {
map2.put("hello", value);
assertEquals(map1, map2);
}
}
}
@Test
public void testEqualsByteArray() {
byte[] value = new byte[5];
Arrays.fill(value, (byte) 'X');
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, byte[].class);
try (final ChronicleMap map1 = builder.create()) {
map1.put("hello", value);
try (final ChronicleMap map2 = builder.create()) {
map2.put("hello", value);
assertEquals(map1, map2);
}
}
}
@Test
public void testSize() {
try (final ChronicleMap map =
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.minSegments(1024)
.removeReturnsNull(true).create()) {
for (int i = 1; i < 1024; i++) {
map.put("key" + i, "value");
assertEquals(i, map.size());
}
for (int i = 1023; i >= 1; ) {
map.remove("key" + i);
i--;
assertEquals(i, map.size());
}
}
}
@Test
public void testRemoveInteger() throws IOException {
int count = 300;
try (final ChronicleMap