ss.tests: Move HT integrity check to an assert method of the stub
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 18 Apr 2016 16:24:52 +0000 (12:24 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Wed, 6 Jul 2016 17:35:37 +0000 (13:35 -0400)
Change-Id: I55ba757873a4c582d1f4ce4d1c5baf19d1167570
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/70890
Reviewed-by: Hudson CI
statesystem/org.eclipse.tracecompass.statesystem.core.tests/stubs/org/eclipse/tracecompass/statesystem/core/tests/stubs/backend/HistoryTreeStub.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java

index bb68c7ef25f98cb8037a2d3b48e350b6f2ec4833..9c5937af68a380fca2a678d365802a5c4877495d 100644 (file)
 package org.eclipse.tracecompass.statesystem.core.tests.stubs.backend;
 
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
 import java.util.List;
 
+import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTree;
@@ -97,4 +102,75 @@ public class HistoryTreeStub extends HistoryTree {
         return getLatestBranch().size();
     }
 
+    private void assertChildrenIntegrity(CoreNode node) {
+        try {
+            /*
+             * Test that this node's start and end times match the start of the
+             * first child and the end of the last child, respectively
+             */
+            if (node.getNbChildren() > 0) {
+                HTNode childNode = getNode(node.getChild(0));
+                assertEquals("Equals start time of parent " + node.getSequenceNumber() + " and first child " + childNode.getSequenceNumber(),
+                        node.getNodeStart(), childNode.getNodeStart());
+                if (node.isOnDisk()) {
+                    childNode = getNode(node.getLatestChild());
+                    assertEquals("Equals end time of parent " + node.getSequenceNumber() + " and last child " + childNode.getSequenceNumber(),
+                            node.getNodeEnd(), childNode.getNodeEnd());
+                }
+            }
+
+            /*
+             * Test that the childStartTimes[] array matches the real nodes'
+             * start times
+             *
+             * Also test that children range is within the parent's range
+             */
+            for (int i = 0; i < node.getNbChildren(); i++) {
+                HTNode childNode = getNode(node.getChild(i));
+                assertEquals("Start time in parent " + node.getSequenceNumber() + " of child at index " + i,
+                        childNode.getNodeStart(), node.getChildStart(i));
+                assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
+                        node.getNodeStart() <= childNode.getNodeStart());
+                if (node.isOnDisk() && childNode.isOnDisk()) {
+                    assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
+                            childNode.getNodeEnd() <= childNode.getNodeEnd());
+                }
+            }
+
+        } catch (ClosedChannelException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Debugging method to make sure all intervals contained in the given node
+     * have valid start and end times.
+     *
+     * @param node
+     *            The node to check
+     */
+    private void assertNodeIntegrity(HTNode node) {
+        if (node instanceof CoreNode) {
+            assertChildrenIntegrity((CoreNode) node);
+        }
+
+        /* Check that all intervals are within the node's range */
+        // TODO: Get the intervals of a node
+
+    }
+
+    /**
+     * Check the integrity of all the nodes in the tree. Calls
+     * {@link #assertNodeIntegrity} for every node in the tree.
+     */
+    public void assertIntegrity() {
+        try {
+            for (int i = 0; i < getNodeCount(); i++) {
+                assertNodeIntegrity(getNode(i));
+            }
+        } catch (ClosedChannelException e) {
+            fail(e.getMessage());
+        }
+    }
+
 }
index af0b7f6981a38b877616f4680c45afb953e15f8d..38ef0c1fc13785f951755dc94c5197d3cb0ba13f 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.tracecompass.internal.statesystem.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 
 /**
@@ -344,10 +345,31 @@ public class HistoryTree {
      *
      * @return The immutable latest branch
      */
+    @VisibleForTesting
     protected List<@NonNull HTNode> getLatestBranch() {
         return ImmutableList.copyOf(fLatestBranch);
     }
 
+    /**
+     * Read a node at sequence number
+     *
+     * @param seqNum
+     *            The sequence number of the node to read
+     * @return The HTNode object
+     * @throws ClosedChannelException
+     *             Exception thrown when reading the node
+     */
+    @VisibleForTesting
+    protected @NonNull HTNode getNode(int seqNum) throws ClosedChannelException {
+        // First, check in the latest branch if the node is there
+        for (HTNode node : fLatestBranch) {
+            if (node.getSequenceNumber() == seqNum) {
+                return node;
+            }
+        }
+        return fTreeIO.readNode(seqNum);
+    }
+
     // ------------------------------------------------------------------------
     // HT_IO interface
     // ------------------------------------------------------------------------
@@ -681,97 +703,6 @@ public class HistoryTree {
     // Test/debugging methods
     // ------------------------------------------------------------------------
 
-    /**
-     * Debugging method to make sure all intervals contained in the given node
-     * have valid start and end times.
-     *
-     * @param zenode
-     *            The node to check
-     * @return True if everything is fine, false if there is at least one
-     *         invalid timestamp (end time < start time, time outside of the
-     *         range of the node, etc.)
-     */
-    @SuppressWarnings("nls")
-    public boolean checkNodeIntegrity(HTNode zenode) {
-        /* Only used for debugging, shouldn't be externalized */
-        HTNode otherNode;
-        CoreNode node;
-        StringBuffer buf = new StringBuffer();
-        boolean ret = true;
-
-        // FIXME /* Only testing Core Nodes for now */
-        if (!(zenode instanceof CoreNode)) {
-            return true;
-        }
-
-        node = (CoreNode) zenode;
-
-        try {
-            /*
-             * Test that this node's start and end times match the start of the
-             * first child and the end of the last child, respectively
-             */
-            if (node.getNbChildren() > 0) {
-                otherNode = fTreeIO.readNode(node.getChild(0));
-                if (node.getNodeStart() != otherNode.getNodeStart()) {
-                    buf.append("Start time of node (" + node.getNodeStart() + ") "
-                            + "does not match start time of first child " + "("
-                            + otherNode.getNodeStart() + "), " + "node #"
-                            + otherNode.getSequenceNumber() + ")\n");
-                    ret = false;
-                }
-                if (node.isOnDisk()) {
-                    otherNode = fTreeIO.readNode(node.getLatestChild());
-                    if (node.getNodeEnd() != otherNode.getNodeEnd()) {
-                        buf.append("End time of node (" + node.getNodeEnd()
-                                + ") does not match end time of last child ("
-                                + otherNode.getNodeEnd() + ", node #"
-                                + otherNode.getSequenceNumber() + ")\n");
-                        ret = false;
-                    }
-                }
-            }
-
-            /*
-             * Test that the childStartTimes[] array matches the real nodes'
-             * start times
-             */
-            for (int i = 0; i < node.getNbChildren(); i++) {
-                otherNode = fTreeIO.readNode(node.getChild(i));
-                if (otherNode.getNodeStart() != node.getChildStart(i)) {
-                    buf.append("  Expected start time of child node #"
-                            + node.getChild(i) + ": " + node.getChildStart(i)
-                            + "\n" + "  Actual start time of node #"
-                            + otherNode.getSequenceNumber() + ": "
-                            + otherNode.getNodeStart() + "\n");
-                    ret = false;
-                }
-            }
-
-        } catch (ClosedChannelException e) {
-            Activator.getDefault().logError(e.getMessage(), e);
-        }
-
-        if (!ret) {
-            Activator.getDefault().logError("SHT: Integrity check failed for node #"
-                    + node.getSequenceNumber() + ":" + buf.toString());
-        }
-        return ret;
-    }
-
-    /**
-     * Check the integrity of all the nodes in the tree. Calls
-     * {@link #checkNodeIntegrity} for every node in the tree.
-     */
-    public void checkIntegrity() {
-        try {
-            for (int i = 0; i < fNodeCount; i++) {
-                checkNodeIntegrity(fTreeIO.readNode(i));
-            }
-        } catch (ClosedChannelException e) {
-        }
-    }
-
     /* Only used for debugging, shouldn't be externalized */
     @SuppressWarnings("nls")
     @Override
This page took 0.02714 seconds and 5 git commands to generate.