ss.tests: Move HT integrity check to an assert method of the stub
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / stubs / org / eclipse / tracecompass / statesystem / core / tests / stubs / backend / HistoryTreeStub.java
1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.statesystem.core.tests.stubs.backend;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.channels.ClosedChannelException;
20 import java.util.List;
21
22 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode;
23 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig;
24 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
25 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTree;
26
27 import com.google.common.collect.Iterables;
28
29 /**
30 * Stub class to unit test the history tree. You can set the size of the
31 * interval section before using the tree, in order to fine-tune the test.
32 *
33 * Note to developers: This tree is not meant to be used with a backend. It just
34 * exposes some info from the history tree.
35 *
36 * @author Geneviève Bastien
37 */
38 public class HistoryTreeStub extends HistoryTree {
39
40 /**
41 * Constructor for this history tree stub
42 *
43 * @param conf
44 * The config to use for this History Tree.
45 * @throws IOException
46 * If an error happens trying to open/write to the file
47 * specified in the config
48 */
49 public HistoryTreeStub(HTConfig conf) throws IOException {
50 super(conf);
51 }
52
53 /**
54 * "Reader" constructor : instantiate a SHTree from an existing tree file on
55 * disk
56 *
57 * @param existingStateFile
58 * Path/filename of the history-file we are to open
59 * @param expProviderVersion
60 * The expected version of the state provider
61 * @throws IOException
62 * If an error happens reading the file
63 */
64 public HistoryTreeStub(File existingStateFile, int expProviderVersion) throws IOException {
65 super(existingStateFile, expProviderVersion);
66 }
67
68 @Override
69 public List<HTNode> getLatestBranch() {
70 return checkNotNull(super.getLatestBranch());
71 }
72
73 /**
74 * Get the latest leaf of the tree
75 *
76 * @return The current leaf node of the tree
77 */
78 public HTNode getLatestLeaf() {
79 List<HTNode> latest = getLatestBranch();
80 return Iterables.getLast(latest);
81 }
82
83 /**
84 * Get the node from the latest branch at a given position, 0 being the root
85 * and <size of latest branch - 1> being a leaf node.
86 *
87 * @param pos
88 * The position at which to return the node
89 * @return The node at position pos
90 */
91 public HTNode getNodeAt(int pos) {
92 List<HTNode> latest = getLatestBranch();
93 return latest.get(pos);
94 }
95
96 /**
97 * Get the depth of the tree
98 *
99 * @return The depth of the tree
100 */
101 public int getDepth() {
102 return getLatestBranch().size();
103 }
104
105 private void assertChildrenIntegrity(CoreNode node) {
106 try {
107 /*
108 * Test that this node's start and end times match the start of the
109 * first child and the end of the last child, respectively
110 */
111 if (node.getNbChildren() > 0) {
112 HTNode childNode = getNode(node.getChild(0));
113 assertEquals("Equals start time of parent " + node.getSequenceNumber() + " and first child " + childNode.getSequenceNumber(),
114 node.getNodeStart(), childNode.getNodeStart());
115 if (node.isOnDisk()) {
116 childNode = getNode(node.getLatestChild());
117 assertEquals("Equals end time of parent " + node.getSequenceNumber() + " and last child " + childNode.getSequenceNumber(),
118 node.getNodeEnd(), childNode.getNodeEnd());
119 }
120 }
121
122 /*
123 * Test that the childStartTimes[] array matches the real nodes'
124 * start times
125 *
126 * Also test that children range is within the parent's range
127 */
128 for (int i = 0; i < node.getNbChildren(); i++) {
129 HTNode childNode = getNode(node.getChild(i));
130 assertEquals("Start time in parent " + node.getSequenceNumber() + " of child at index " + i,
131 childNode.getNodeStart(), node.getChildStart(i));
132 assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
133 node.getNodeStart() <= childNode.getNodeStart());
134 if (node.isOnDisk() && childNode.isOnDisk()) {
135 assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
136 childNode.getNodeEnd() <= childNode.getNodeEnd());
137 }
138 }
139
140 } catch (ClosedChannelException e) {
141 fail(e.getMessage());
142 }
143 }
144
145 /**
146 * Debugging method to make sure all intervals contained in the given node
147 * have valid start and end times.
148 *
149 * @param node
150 * The node to check
151 */
152 private void assertNodeIntegrity(HTNode node) {
153 if (node instanceof CoreNode) {
154 assertChildrenIntegrity((CoreNode) node);
155 }
156
157 /* Check that all intervals are within the node's range */
158 // TODO: Get the intervals of a node
159
160 }
161
162 /**
163 * Check the integrity of all the nodes in the tree. Calls
164 * {@link #assertNodeIntegrity} for every node in the tree.
165 */
166 public void assertIntegrity() {
167 try {
168 for (int i = 0; i < getNodeCount(); i++) {
169 assertNodeIntegrity(getNode(i));
170 }
171 } catch (ClosedChannelException e) {
172 fail(e.getMessage());
173 }
174 }
175
176 }
This page took 0.038523 seconds and 5 git commands to generate.