use of com.baidu.hugegraph.util.collection.CollectionFactory in project incubator-hugegraph by apache.
the class CollectionFactoryTest method testNewList.
@Test
public void testNewList() {
// With type
factory = new CollectionFactory();
List<?> list = factory.newList();
Assert.assertInstanceOf(FastList.class, list);
factory = new CollectionFactory(CollectionType.EC);
list = factory.newList();
Assert.assertInstanceOf(FastList.class, list);
factory = new CollectionFactory(CollectionType.FU);
list = factory.newList();
Assert.assertInstanceOf(ObjectArrayList.class, list);
factory = new CollectionFactory(CollectionType.JCF);
list = factory.newList();
Assert.assertInstanceOf(ArrayList.class, list);
// With initial capacity
int initialCapacity = 10;
factory = new CollectionFactory();
list = factory.newList(initialCapacity);
Assert.assertInstanceOf(FastList.class, list);
Object[] items = Whitebox.getInternalState(list, "items");
Assert.assertEquals(initialCapacity, items.length);
factory = new CollectionFactory(CollectionType.EC);
list = factory.newList(initialCapacity);
Assert.assertInstanceOf(FastList.class, list);
items = Whitebox.getInternalState(list, "items");
Assert.assertEquals(initialCapacity, items.length);
factory = new CollectionFactory(CollectionType.FU);
list = factory.newList(initialCapacity);
Assert.assertInstanceOf(ObjectArrayList.class, list);
items = Whitebox.getInternalState(list, "a");
Assert.assertEquals(initialCapacity, items.length);
factory = new CollectionFactory(CollectionType.JCF);
list = factory.newList(initialCapacity);
Assert.assertInstanceOf(ArrayList.class, list);
items = Whitebox.getInternalState(list, "elementData");
Assert.assertEquals(initialCapacity, items.length);
// With collection
Collection<Integer> integers = ImmutableSet.of(1, 2, 3, 4);
factory = new CollectionFactory();
list = factory.newList(integers);
Assert.assertInstanceOf(FastList.class, list);
Assert.assertEquals(integers.size(), list.size());
for (int integer : integers) {
Assert.assertTrue(list.contains(integer));
}
factory = new CollectionFactory(CollectionType.EC);
list = factory.newList(integers);
Assert.assertInstanceOf(FastList.class, list);
Assert.assertEquals(integers.size(), list.size());
for (int integer : integers) {
Assert.assertTrue(list.contains(integer));
}
factory = new CollectionFactory(CollectionType.FU);
list = factory.newList(integers);
Assert.assertInstanceOf(ObjectArrayList.class, list);
Assert.assertEquals(integers.size(), list.size());
for (int integer : integers) {
Assert.assertTrue(list.contains(integer));
}
factory = new CollectionFactory(CollectionType.JCF);
list = factory.newList(integers);
Assert.assertInstanceOf(ArrayList.class, list);
Assert.assertEquals(integers.size(), list.size());
for (int integer : integers) {
Assert.assertTrue(list.contains(integer));
}
}
use of com.baidu.hugegraph.util.collection.CollectionFactory in project incubator-hugegraph by apache.
the class CollectionFactoryTest method testCollectionFactoryConstructor.
@Test
public void testCollectionFactoryConstructor() {
factory = new CollectionFactory();
CollectionType type = Whitebox.getInternalState(factory, "type");
Assert.assertEquals(CollectionType.EC, type);
factory = new CollectionFactory(CollectionType.EC);
type = Whitebox.getInternalState(factory, "type");
Assert.assertEquals(CollectionType.EC, type);
factory = new CollectionFactory(CollectionType.FU);
type = Whitebox.getInternalState(factory, "type");
Assert.assertEquals(CollectionType.FU, type);
factory = new CollectionFactory(CollectionType.JCF);
type = Whitebox.getInternalState(factory, "type");
Assert.assertEquals(CollectionType.JCF, type);
}
use of com.baidu.hugegraph.util.collection.CollectionFactory in project incubator-hugegraph by apache.
the class CollectionFactoryTest method testIdSet.
@Test
public void testIdSet() {
factory = new CollectionFactory(CollectionType.EC);
IdSet idSet = factory.newIdSet();
Set<Id> ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(UnifiedSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
factory = new CollectionFactory(CollectionType.FU);
idSet = factory.newIdSet();
ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(ObjectOpenHashSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
factory = new CollectionFactory(CollectionType.JCF);
idSet = factory.newIdSet();
ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(HashSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
idSet = CollectionFactory.newIdSet(CollectionType.EC);
ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(UnifiedSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
idSet = CollectionFactory.newIdSet(CollectionType.FU);
ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(ObjectOpenHashSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
idSet = CollectionFactory.newIdSet(CollectionType.JCF);
ids = Whitebox.getInternalState(idSet, "nonNumberIds");
Assert.assertInstanceOf(HashSet.class, ids);
Assert.assertInstanceOf(LongHashSet.class, Whitebox.getInternalState(idSet, "numberIds"));
}
use of com.baidu.hugegraph.util.collection.CollectionFactory in project incubator-hugegraph by apache.
the class CollectionFactoryTest method testNewSet.
@Test
public void testNewSet() {
// With type
factory = new CollectionFactory();
Set<?> set = factory.newSet();
Assert.assertInstanceOf(UnifiedSet.class, set);
factory = new CollectionFactory(CollectionType.EC);
set = factory.newSet();
Assert.assertInstanceOf(UnifiedSet.class, set);
factory = new CollectionFactory(CollectionType.FU);
set = factory.newSet();
Assert.assertInstanceOf(ObjectOpenHashSet.class, set);
factory = new CollectionFactory(CollectionType.JCF);
set = factory.newSet();
Assert.assertInstanceOf(HashSet.class, set);
// With initial capacity
int initialCapacity = 10;
factory = new CollectionFactory();
set = factory.newSet(initialCapacity);
Assert.assertInstanceOf(UnifiedSet.class, set);
Object[] items = Whitebox.getInternalState(set, "table");
// Initial size of UnifiedSet is (initialCapacity / 0.75)
Assert.assertEquals(16, items.length);
factory = new CollectionFactory(CollectionType.EC);
set = factory.newSet(initialCapacity);
Assert.assertInstanceOf(UnifiedSet.class, set);
items = Whitebox.getInternalState(set, "table");
// Initial size of UnifiedSet is (initialCapacity / 0.75)
Assert.assertEquals(16, items.length);
factory = new CollectionFactory(CollectionType.FU);
set = factory.newSet(initialCapacity);
Assert.assertInstanceOf(ObjectOpenHashSet.class, set);
items = Whitebox.getInternalState(set, "key");
Assert.assertEquals(17, items.length);
factory = new CollectionFactory(CollectionType.JCF);
set = factory.newSet(initialCapacity);
Assert.assertInstanceOf(HashSet.class, set);
Map<?, ?> map = Whitebox.getInternalState(set, "map");
Assert.assertInstanceOf(HashMap.class, map);
Assert.assertEquals(0, map.size());
// With collection
Collection<Integer> integers = ImmutableSet.of(1, 2, 3, 4);
factory = new CollectionFactory();
set = factory.newSet(integers);
Assert.assertInstanceOf(UnifiedSet.class, set);
Assert.assertEquals(integers.size(), set.size());
for (int integer : integers) {
Assert.assertTrue(set.contains(integer));
}
factory = new CollectionFactory(CollectionType.EC);
set = factory.newSet(integers);
Assert.assertInstanceOf(UnifiedSet.class, set);
Assert.assertEquals(integers.size(), set.size());
for (int integer : integers) {
Assert.assertTrue(set.contains(integer));
}
factory = new CollectionFactory(CollectionType.FU);
set = factory.newSet(integers);
Assert.assertInstanceOf(ObjectOpenHashSet.class, set);
Assert.assertEquals(integers.size(), set.size());
for (int integer : integers) {
Assert.assertTrue(set.contains(integer));
}
factory = new CollectionFactory(CollectionType.JCF);
set = factory.newSet(integers);
Assert.assertInstanceOf(HashSet.class, set);
Assert.assertEquals(integers.size(), set.size());
for (int integer : integers) {
Assert.assertTrue(set.contains(integer));
}
}
use of com.baidu.hugegraph.util.collection.CollectionFactory in project incubator-hugegraph by apache.
the class CollectionFactoryTest method testNewMap.
@Test
public void testNewMap() {
// With type
factory = new CollectionFactory();
Map<?, ?> map = factory.newMap();
Assert.assertInstanceOf(UnifiedMap.class, map);
factory = new CollectionFactory(CollectionType.EC);
map = factory.newMap();
Assert.assertInstanceOf(UnifiedMap.class, map);
factory = new CollectionFactory(CollectionType.FU);
map = factory.newMap();
Assert.assertInstanceOf(Object2ObjectOpenHashMap.class, map);
factory = new CollectionFactory(CollectionType.JCF);
map = factory.newMap();
Assert.assertInstanceOf(HashMap.class, map);
// With initial capacity
int initialCapacity = 10;
factory = new CollectionFactory();
map = factory.newMap(initialCapacity);
Assert.assertInstanceOf(UnifiedMap.class, map);
Object[] items = Whitebox.getInternalState(map, "table");
// Initial size of UnifiedSet is (initialCapacity / 0.75) * 2
Assert.assertEquals(32, items.length);
factory = new CollectionFactory(CollectionType.EC);
map = factory.newMap(initialCapacity);
Assert.assertInstanceOf(UnifiedMap.class, map);
items = Whitebox.getInternalState(map, "table");
// Initial size of UnifiedSet is (initialCapacity / 0.75) * 2
Assert.assertEquals(32, items.length);
factory = new CollectionFactory(CollectionType.FU);
map = factory.newMap(initialCapacity);
Assert.assertInstanceOf(Object2ObjectOpenHashMap.class, map);
items = Whitebox.getInternalState(map, "key");
Assert.assertEquals(17, items.length);
items = Whitebox.getInternalState(map, "value");
Assert.assertEquals(17, items.length);
factory = new CollectionFactory(CollectionType.JCF);
map = factory.newMap(initialCapacity);
Assert.assertInstanceOf(HashMap.class, map);
items = Whitebox.getInternalState(map, "table");
Assert.assertNull(items);
// With collection
Map<Integer, String> keyValues = ImmutableMap.of(1, "A", 2, "B", 3, "C", 4, "D");
factory = new CollectionFactory();
map = factory.newMap(keyValues);
Assert.assertInstanceOf(UnifiedMap.class, map);
Assert.assertEquals(keyValues.size(), map.size());
for (Map.Entry<Integer, String> entry : keyValues.entrySet()) {
Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
}
factory = new CollectionFactory(CollectionType.EC);
map = factory.newMap(keyValues);
Assert.assertInstanceOf(UnifiedMap.class, map);
Assert.assertEquals(keyValues.size(), map.size());
for (Map.Entry<Integer, String> entry : keyValues.entrySet()) {
Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
}
factory = new CollectionFactory(CollectionType.FU);
map = factory.newMap(keyValues);
Assert.assertInstanceOf(Object2ObjectOpenHashMap.class, map);
Assert.assertEquals(keyValues.size(), map.size());
for (Map.Entry<Integer, String> entry : keyValues.entrySet()) {
Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
}
factory = new CollectionFactory(CollectionType.JCF);
map = factory.newMap(keyValues);
Assert.assertInstanceOf(HashMap.class, map);
Assert.assertEquals(keyValues.size(), map.size());
for (Map.Entry<Integer, String> entry : keyValues.entrySet()) {
Assert.assertEquals(entry.getValue(), map.get(entry.getKey()));
}
}
Aggregations