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;
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());
+ }
+ }
+
}
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;
/**
*
* @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
// ------------------------------------------------------------------------
// 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