ss: add unit tests for state value types in backends
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 7 Mar 2016 17:20:15 +0000 (12:20 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Mon, 21 Mar 2016 01:38:05 +0000 (21:38 -0400)
Change-Id: I3b33478e62331cc6b58d551db3597ae8cdbc892c
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/68097
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Tested-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-by: Hudson CI
statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/backend/StateHistoryBackendTestBase.java

index 5fd83ec968e61f93738c2036b105316d78439e16..f678b700e4ff6699b4eb17f7d0d6389e9f057a1c 100644 (file)
@@ -20,20 +20,37 @@ import java.util.List;
 
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
 import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
- * Abstract class to test implementations of the {@link IStateHistoryBackend} interface.
+ * Abstract class to test implementations of the {@link IStateHistoryBackend}
+ * interface.
  *
  * @author Patrick Tasse
  * @author Alexandre Montplaisir
+ * @author Geneviève Bastien
  */
 public abstract class StateHistoryBackendTestBase {
 
+    /* Some state values of each type */
+    private static final ITmfStateValue INT_VAL1 = TmfStateValue.newValueInt(-42);
+    private static final ITmfStateValue INT_VAL2 = TmfStateValue.newValueInt(675893);
+    private static final ITmfStateValue LONG_VAL1 = TmfStateValue.newValueLong(-78L);
+    private static final ITmfStateValue LONG_VAL2 = TmfStateValue.newValueLong(2234L);
+    private static final ITmfStateValue DOUBLE_VAL1 = TmfStateValue.newValueDouble(-9.87);
+    private static final ITmfStateValue DOUBLE_VAL2 = TmfStateValue.newValueDouble(50324.131643);
+    private static final ITmfStateValue STR_VAL1 = TmfStateValue.newValueString("A string");
+    private static final ITmfStateValue STR_VAL2 = TmfStateValue.newValueString("Another éèstr");
+
     /**
      * Gets the backend to be used for building.
      *
@@ -104,6 +121,22 @@ public abstract class StateHistoryBackendTestBase {
         }
     }
 
+    /**
+     * Initializes a list for the number of attributes in the backend and
+     * associates a null value for each
+     *
+     * @param nbAttrib
+     *            The number of attributes in the backend
+     * @return A list of null values for each attribute
+     */
+    private static List<@Nullable ITmfStateInterval> prepareIntervalList(int nbAttrib) {
+        List<@Nullable ITmfStateInterval> intervals = new ArrayList<>(nbAttrib);
+        for (int i = 0; i < nbAttrib; i++) {
+            intervals.add(null);
+        }
+        return intervals;
+    }
+
     /**
      * Test the integrity of a backend by first building the backend with the
      * specified intervals, closing it, and then querying at every single
@@ -137,10 +170,7 @@ public abstract class StateHistoryBackendTestBase {
              * intervals are returned.
              */
             for (long t = backend.getStartTime(); t <= backend.getEndTime(); t++) {
-                List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
-                for (int i = 0; i < nbAttr; i++) {
-                    stateInfo.add(null);
-                }
+                List<@Nullable ITmfStateInterval> stateInfo = prepareIntervalList(nbAttr);
                 backend.doQuery(stateInfo, t);
                 for (int attr = 0; attr < stateInfo.size(); attr++) {
                     ITmfStateInterval interval = stateInfo.get(attr);
@@ -227,4 +257,159 @@ public abstract class StateHistoryBackendTestBase {
 
         buildAndQueryFullRange(startTime, endTime, nbAttr, intervals, false);
     }
+
+    /**
+     * Test inserting values of different types and querying them right after
+     */
+    @Test
+    public void testInsertQueryStateValues() {
+        /* Test specific data initialization */
+        long startTime = 10;
+        long timeStep = 5;
+        int intQuark = 0;
+        int longQuark = 1;
+        int doubleQuark = 2;
+        int strQuark = 3;
+
+        try {
+            IStateHistoryBackend backend = getBackendForBuilding(startTime);
+            assertNotNull(backend);
+
+            /* Int interval */
+            backend.insertPastState(startTime, startTime + timeStep, intQuark, INT_VAL1);
+            ITmfStateInterval interval = backend.doSingularQuery(startTime, intQuark);
+
+            assertEquals("Int interval start time", startTime, interval.getStartTime());
+            assertEquals("Int interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+            /* Long interval */
+            backend.insertPastState(startTime, startTime + timeStep, longQuark, LONG_VAL1);
+            interval = backend.doSingularQuery(startTime, longQuark);
+
+            assertEquals("Long interval start time", startTime, interval.getStartTime());
+            assertEquals("Long interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Long interval value", LONG_VAL1, interval.getStateValue());
+
+            /* Double interval */
+            backend.insertPastState(startTime, startTime + timeStep, doubleQuark, DOUBLE_VAL1);
+            interval = backend.doSingularQuery(startTime, doubleQuark);
+
+            assertEquals("Double interval start time", startTime, interval.getStartTime());
+            assertEquals("Double interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("Double interval value", DOUBLE_VAL1, interval.getStateValue());
+
+            /* String interval */
+            backend.insertPastState(startTime, startTime + timeStep, strQuark, STR_VAL1);
+            interval = backend.doSingularQuery(startTime, strQuark);
+
+            assertEquals("String interval start time", startTime, interval.getStartTime());
+            assertEquals("String interval end time", startTime + timeStep, interval.getEndTime());
+            assertEquals("String interval value", STR_VAL1, interval.getStateValue());
+
+            /*
+             * Add other intervals for the int quark and query at different
+             * times
+             */
+            backend.insertPastState(startTime + timeStep + 1, startTime + (2 * timeStep), intQuark, INT_VAL2);
+            backend.insertPastState(startTime + (2 * timeStep) + 1, startTime + (3 * timeStep), intQuark, INT_VAL1);
+
+            interval = backend.doSingularQuery(startTime + timeStep, intQuark);
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + timeStep + 1, intQuark);
+            assertEquals("Int interval value", INT_VAL2, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + (2 * timeStep), intQuark);
+            assertEquals("Int interval value", INT_VAL2, interval.getStateValue());
+
+            interval = backend.doSingularQuery(startTime + (2 * timeStep) + 1, intQuark);
+            assertEquals("Int interval value", INT_VAL1, interval.getStateValue());
+
+        } catch (TimeRangeException | StateSystemDisposedException | IOException | AttributeNotFoundException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Test querying various state value types after the state system has been
+     * built and finished
+     */
+    @Test
+    public void testBuildNowQueryLaterStateValues() {
+        /* Test specific data initialization */
+        long startTime = 10;
+        long timeStep = 5;
+        int intQuark = 0;
+        int longQuark = 1;
+        int doubleQuark = 2;
+        int strQuark = 3;
+        int nbAttribs = 4;
+
+        try {
+            IStateHistoryBackend backend = getBackendForBuilding(startTime);
+            assertNotNull(backend);
+
+            long firstEnd = startTime + timeStep;
+            long nextStart = firstEnd + 1;
+            long endTime = nextStart + timeStep;
+
+            insertIntervals(backend, ImmutableList.of(new TmfStateInterval(startTime, startTime + timeStep, intQuark, INT_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, longQuark, LONG_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, doubleQuark, DOUBLE_VAL1),
+                    new TmfStateInterval(startTime, startTime + timeStep, strQuark, STR_VAL1),
+                    new TmfStateInterval(nextStart, endTime, intQuark, INT_VAL2),
+                    new TmfStateInterval(nextStart, endTime, longQuark, LONG_VAL2),
+                    new TmfStateInterval(nextStart, endTime, doubleQuark, DOUBLE_VAL2),
+                    new TmfStateInterval(nextStart, endTime, strQuark, STR_VAL2)));
+
+            backend.finishedBuilding(endTime);
+
+            /* Make sure the end time corresponds to the backend end time */
+            assertEquals(endTime, backend.getEndTime());
+
+            IStateHistoryBackend backendQuery = getBackendForQuerying(backend);
+
+            /* Verify start and end times */
+            assertEquals("Backend start time", startTime, backendQuery.getStartTime());
+            assertEquals("Backend end time", endTime, backendQuery.getEndTime());
+
+            List<@Nullable ITmfStateInterval> intervals = prepareIntervalList(nbAttribs);
+
+            /* Do a full query at start and verify the values */
+            backendQuery.doQuery(intervals, startTime);
+
+            ITmfStateInterval interval = intervals.get(intQuark);
+            assertNotNull(interval);
+            assertEquals("Int value after read", INT_VAL1, interval.getStateValue());
+            interval = intervals.get(longQuark);
+            assertNotNull(interval);
+            assertEquals("Long value after read", LONG_VAL1, interval.getStateValue());
+            interval = intervals.get(doubleQuark);
+            assertNotNull(interval);
+            assertEquals("Double value after read", DOUBLE_VAL1, interval.getStateValue());
+            interval = intervals.get(strQuark);
+            assertNotNull(interval);
+            assertEquals("String value after read", STR_VAL1, interval.getStateValue());
+
+            /* Do a full query at the end and verify the values */
+            backendQuery.doQuery(intervals, endTime);
+
+            interval = intervals.get(intQuark);
+            assertNotNull(interval);
+            assertEquals("Int value after read", INT_VAL2, interval.getStateValue());
+            interval = intervals.get(longQuark);
+            assertNotNull(interval);
+            assertEquals("Long value after read", LONG_VAL2, interval.getStateValue());
+            interval = intervals.get(doubleQuark);
+            assertNotNull(interval);
+            assertEquals("Double value after read", DOUBLE_VAL2, interval.getStateValue());
+            interval = intervals.get(strQuark);
+            assertNotNull(interval);
+            assertEquals("String value after read", STR_VAL2, interval.getStateValue());
+
+        } catch (TimeRangeException | IOException | StateSystemDisposedException e) {
+            fail(e.getMessage());
+        }
+    }
 }
This page took 0.028549 seconds and 5 git commands to generate.