timing.core: add testing for less used functions in the segment store
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core.tests / src / org / eclipse / tracecompass / segmentstore / core / tests / AbstractTestSegmentStore.java
index 05818af722fbaeef9710e9497d214684721ce943..ac51468ee7934a893a925223792f7f2f7200d48b 100644 (file)
@@ -17,13 +17,18 @@ import static org.junit.Assert.assertTrue;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
 import org.eclipse.tracecompass.segmentstore.core.ISegment;
 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
+import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -54,6 +59,16 @@ public abstract class AbstractTestSegmentStore {
      */
     protected abstract ISegmentStore<@NonNull ISegment> getSegmentStore();
 
+    /**
+     * Get the segment store to test with initial data
+     *
+     * @param data
+     *            the data
+     *
+     * @return the segment store
+     */
+    protected abstract ISegmentStore<@NonNull ISegment> getSegmentStore(@NonNull ISegment @NonNull [] data);
+
     private static final @NonNull ISegment SEGMENT_2_6 = new BasicSegment(2, 6);
     private static final @NonNull ISegment SEGMENT_4_6 = new BasicSegment(4, 6);
     private static final @NonNull ISegment SEGMENT_4_8 = new BasicSegment(4, 8);
@@ -99,6 +114,62 @@ public abstract class AbstractTestSegmentStore {
         assertEquals(SEGMENTS.size(), fSegmentStore.size());
     }
 
+    /**
+     * Testing isEmpty() method
+     */
+    @Test
+    public void testIsEmpty() {
+        assertFalse(fSegmentStore.isEmpty());
+        fSegmentStore.clear();
+        assertTrue(fSegmentStore.isEmpty());
+    }
+
+    /**
+     * Testing adding a collection with the addAll method
+     */
+    @Test
+    public void testAddAll() {
+        assertFalse(fSegmentStore.isEmpty());
+        fSegmentStore.clear();
+        assertTrue(fSegmentStore.isEmpty());
+        fSegmentStore.addAll(SEGMENTS);
+        assertTrue(fSegmentStore.containsAll(SEGMENTS));
+    }
+
+    /**
+     * Testing "copy" constructor
+     */
+    @Test
+    public void testAddAllConstructor() {
+        @SuppressWarnings("null")
+        ISegmentStore<@NonNull ISegment> other = getSegmentStore(fSegmentStore.toArray(new ISegment[fSegmentStore.size()]));
+        assertTrue(fSegmentStore.containsAll(other));
+        assertTrue(other.containsAll(fSegmentStore));
+    }
+
+    /**
+     * Testing "copy" constructor out of order
+     */
+    @Test
+    public void testAddAllConstructorOutOfOrder() {
+        @SuppressWarnings("null")
+        ISegmentStore<@NonNull ISegment> other = getSegmentStore(REVERSE_SEGMENTS.toArray(new ISegment[fSegmentStore.size()]));
+        assertTrue(fSegmentStore.containsAll(other));
+        assertTrue(other.containsAll(fSegmentStore));
+    }
+
+    /**
+     * Testing adding an out of order collection with the addAll method
+     */
+    @Test
+    public void testAddAllOutOfOrder() {
+        assertFalse(fSegmentStore.isEmpty());
+        fSegmentStore.clear();
+        assertTrue(fSegmentStore.isEmpty());
+        fSegmentStore.addAll(REVERSE_SEGMENTS);
+        assertTrue(fSegmentStore.containsAll(SEGMENTS));
+    }
+
     /**
      * Test the contains() method.
      */
@@ -111,6 +182,18 @@ public abstract class AbstractTestSegmentStore {
         assertFalse(fSegmentStore.contains(otherSegment));
     }
 
+    /**
+     * Test containsAll() method
+     */
+    public void testContainsAll() {
+        ISegmentStore<@NonNull ISegment> store = getSegmentStore();
+
+        store.add(SEGMENT_2_6);
+        assertTrue(store.containsAll(Collections.emptyList()));
+        assertTrue(store.containsAll(Collections.singleton(SEGMENT_2_6)));
+        assertFalse(store.containsAll(Collections.singleton(SEGMENT_4_6)));
+    }
+
     /**
      * Test the toArray() method.
      */
@@ -362,4 +445,73 @@ public abstract class AbstractTestSegmentStore {
         assertEquals(lastExpected, fixture);
     }
 
+    /**
+     *  Test to check ordered iterators
+     */
+    @Test
+    public void testSortedIterator() {
+        List<@NonNull Comparator<ISegment>> comparators = new LinkedList<>();
+        comparators.add(SegmentComparators.INTERVAL_END_COMPARATOR);
+        comparators.add(NonNullUtils.checkNotNull(SegmentComparators.INTERVAL_END_COMPARATOR.reversed()));
+        comparators.add(SegmentComparators.INTERVAL_START_COMPARATOR);
+        comparators.add(NonNullUtils.checkNotNull(SegmentComparators.INTERVAL_START_COMPARATOR.reversed()));
+        comparators.add(SegmentComparators.INTERVAL_LENGTH_COMPARATOR);
+        comparators.add(NonNullUtils.checkNotNull(SegmentComparators.INTERVAL_LENGTH_COMPARATOR.reversed()));
+
+        Iterable<ISegment> iterable;
+        for (Comparator<ISegment> comparator : comparators) {
+            iterable = fSegmentStore.iterator(comparator);
+            verifySortedIterable(iterable, 5, comparator);
+            iterable = fSegmentStore.getIntersectingElements(5, comparator);
+            verifySortedIterable(iterable, 3, comparator);
+            iterable = fSegmentStore.getIntersectingElements(7, 14, comparator);
+            verifySortedIterable(iterable, 3, comparator);
+        }
+    }
+
+    private static void verifySortedIterable(Iterable<ISegment> iterable, int expectedSize, Comparator<ISegment> comparator) {
+        // check its size
+        assertEquals(expectedSize, Iterables.size(iterable));
+        Iterator<ISegment> iterator = iterable.iterator();
+        // check the order
+        ISegment prev, current = iterator.next();
+        while (iterator.hasNext()) {
+            prev = current;
+            current = iterator.next();
+            assertTrue(comparator.compare(prev, current) <= 0);
+        }
+    }
+
+    /**
+     * Test retainAll() contract
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRetainAll() {
+        ISegmentStore<@NonNull ISegment> store = getSegmentStore();
+
+        store.add(SEGMENT_2_6);
+        store.retainAll(Collections.emptyList());
+    }
+
+    /**
+     * Test remove() contract
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRemove() {
+        ISegmentStore<@NonNull ISegment> store = getSegmentStore();
+
+        store.add(SEGMENT_2_6);
+        store.remove(SEGMENT_2_6);
+    }
+
+    /**
+     * Test removeAll() contract
+     */
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRemoveAll() {
+        ISegmentStore<@NonNull ISegment> store = getSegmentStore();
+
+        store.add(SEGMENT_2_6);
+        store.removeAll(Collections.emptyList());
+    }
 }
\ No newline at end of file
This page took 0.026635 seconds and 5 git commands to generate.