package org.mapdb;
import java.util.*;
import java.util.concurrent.ConcurrentNavigableMap;
/**
* This code comes from GoogleCollections, was modified for JDBM by Jan Kotek
*
* Tests representing the contract of {@link java.util.SortedMap}. Concrete subclasses of
* this base class test conformance of concrete {@link java.util.SortedMap} subclasses to
* that contract.
*
* @author Jared Levy
*
*/
public class BTreeMapTest3
extends ConcurrentMapInterfaceTest {
public BTreeMapTest3() {
super(false, false, true, true, true, true, false);
}
@Override
protected Integer getKeyNotInPopulatedMap() throws UnsupportedOperationException {
return -100;
}
@Override
protected String getValueNotInPopulatedMap() throws UnsupportedOperationException {
return "XYZ";
}
@Override
protected String getSecondValueNotInPopulatedMap() throws UnsupportedOperationException {
return "ASD";
}
@Override
protected ConcurrentNavigableMap makeEmptyMap() throws UnsupportedOperationException {
return DBMaker.newMemoryDB().make().getTreeMap("test");
}
@Override
protected ConcurrentNavigableMap makePopulatedMap() throws UnsupportedOperationException {
ConcurrentNavigableMap map = makeEmptyMap();
for (int i = 0; i < 100; i++){
if(i%11==0||i%7==0) continue;
map.put(i, "aa" + i);
}
return map;
}
@Override
protected ConcurrentNavigableMap makeEitherMap() {
try {
return makePopulatedMap();
} catch (UnsupportedOperationException e) {
return makeEmptyMap();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" }) // Needed for null comparator
public void testOrdering() {
final SortedMap map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Iterator iterator = map.keySet().iterator();
Integer prior = iterator.next();
Comparator super Integer> comparator = map.comparator();
while (iterator.hasNext()) {
Integer current = iterator.next();
if (comparator == null) {
Comparable comparable = (Comparable) prior;
assertTrue(comparable.compareTo(current) < 0);
} else {
assertTrue(map.comparator().compare(prior, current) < 0);
}
current = prior;
}
}
public void testFirstKeyNonEmpty() {
final SortedMap map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Integer expected = map.keySet().iterator().next();
assertEquals(expected, map.firstKey());
assertInvariants(map);
}
public void testLastKeyNonEmpty() {
final SortedMap map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
Integer expected = null;
for (Integer key : map.keySet()) {
expected = key;
}
assertEquals(expected, map.lastKey());
assertInvariants(map);
}
private static List toList(Collection collection) {
return new ArrayList(collection);
}
private static List subListSnapshot(
List list, int fromIndex, int toIndex) {
List subList = new ArrayList();
for (int i = fromIndex; i < toIndex; i++) {
subList.add(list.get(i));
}
return Collections.unmodifiableList(subList);
}
public void testHeadMap() {
final NavigableMap map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, 0, i);
SortedMap headMap = map.headMap(list.get(i).getKey());
assertEquals(expected, toList(headMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, 0, i+1);
SortedMap headMap = map.headMap(list.get(i).getKey(),true);
assertEquals(expected, toList(headMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, 0, i);
SortedMap headMap = map.headMap(list.get(i).getKey(),false);
assertEquals(expected, toList(headMap.entrySet()));
}
}
public void testTailMap() {
final NavigableMap map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, i, list.size());
SortedMap tailMap = map.tailMap(list.get(i).getKey());
assertEquals(expected, toList(tailMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, i, list.size());
SortedMap tailMap = map.tailMap(list.get(i).getKey(),true);
assertEquals(expected, toList(tailMap.entrySet()));
}
for (int i = 0; i < list.size(); i++) {
List> expected = subListSnapshot(list, i+1, list.size());
SortedMap tailMap = map.tailMap(list.get(i).getKey(),false);
assertEquals(expected, toList(tailMap.entrySet()));
}
}
public void testSubMap() {
final NavigableMap map;
try {
map = makeEitherMap();
} catch (UnsupportedOperationException e) {
return;
}
List> list = toList(map.entrySet());
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List> expected = subListSnapshot(list, i, j);
SortedMap subMap
= map.subMap(list.get(i).getKey(), list.get(j).getKey());
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List> expected = subListSnapshot(list, i, j+1);
SortedMap subMap
= map.subMap(list.get(i).getKey(), true, list.get(j).getKey(), true);
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i; j < list.size(); j++) {
List> expected = subListSnapshot(list, i+1, j);
SortedMap subMap
= map.subMap(list.get(i).getKey(), false, list.get(j).getKey(), false);
assertEquals(expected, toList(subMap.entrySet()));
assertEquals(expected.size(), subMap.size());
assertEquals(expected.size(), subMap.keySet().size());
assertEquals(expected.size(), subMap.entrySet().size());
assertEquals(expected.size(), subMap.values().size());
}
}
}
public void testSubMapIllegal() {
final SortedMap map;
try {
map = makePopulatedMap();
} catch (UnsupportedOperationException e) {
return;
}
if (map.size() < 2) {
return;
}
Iterator iterator = map.keySet().iterator();
Integer first = iterator.next();
Integer second = iterator.next();
try {
map.subMap(second, first);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
}