ss: Move all debugPrint() methods to the test stubs
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / stubs / org / eclipse / tracecompass / statesystem / core / tests / stubs / backend / HistoryTreeClassicStub.java
CommitLineData
f3476b68
GB
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
5eb1b4b0 10package org.eclipse.tracecompass.statesystem.core.tests.stubs.backend;
f3476b68
GB
11
12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
a10a38ae
GB
13import static org.junit.Assert.assertEquals;
14import static org.junit.Assert.assertTrue;
15import static org.junit.Assert.fail;
f3476b68 16
068641fa 17import java.io.File;
f3476b68 18import java.io.IOException;
0e4eaca8 19import java.io.PrintWriter;
a10a38ae 20import java.nio.channels.ClosedChannelException;
f3476b68
GB
21import java.util.List;
22
a10a38ae 23import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode;
f3476b68
GB
24import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig;
25import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
3a081e85
GB
26import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeClassic;
27import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.IHistoryTree;
f3476b68
GB
28
29import com.google.common.collect.Iterables;
30
31/**
32 * Stub class to unit test the history tree. You can set the size of the
33 * interval section before using the tree, in order to fine-tune the test.
34 *
35 * Note to developers: This tree is not meant to be used with a backend. It just
36 * exposes some info from the history tree.
37 *
38 * @author Geneviève Bastien
39 */
3a081e85
GB
40public class HistoryTreeClassicStub extends HistoryTreeClassic {
41
42 /**
43 * Minimum size a block of this tree should have
44 */
45 public static final int MINIMUM_BLOCK_SIZE = IHistoryTree.TREE_HEADER_SIZE;
f3476b68
GB
46
47 /**
48 * Constructor for this history tree stub
49 *
50 * @param conf
51 * The config to use for this History Tree.
52 * @throws IOException
53 * If an error happens trying to open/write to the file
54 * specified in the config
55 */
3a081e85 56 public HistoryTreeClassicStub(HTConfig conf) throws IOException {
f3476b68
GB
57 super(conf);
58 }
59
068641fa
GB
60 /**
61 * "Reader" constructor : instantiate a SHTree from an existing tree file on
62 * disk
63 *
64 * @param existingStateFile
65 * Path/filename of the history-file we are to open
66 * @param expProviderVersion
67 * The expected version of the state provider
68 * @throws IOException
69 * If an error happens reading the file
70 */
3a081e85 71 public HistoryTreeClassicStub(File existingStateFile, int expProviderVersion) throws IOException {
068641fa
GB
72 super(existingStateFile, expProviderVersion);
73 }
74
0e4eaca8
AM
75 // ------------------------------------------------------------------------
76 // Extra test accessors
77 // ------------------------------------------------------------------------
78
b2ca67ca
PT
79 @Override
80 public List<HTNode> getLatestBranch() {
0e4eaca8 81 /* Super method is not public */
b2ca67ca
PT
82 return checkNotNull(super.getLatestBranch());
83 }
84
f3476b68
GB
85 /**
86 * Get the latest leaf of the tree
87 *
88 * @return The current leaf node of the tree
89 */
90 public HTNode getLatestLeaf() {
91 List<HTNode> latest = getLatestBranch();
0e4f957e 92 return Iterables.getLast(latest);
f3476b68
GB
93 }
94
95 /**
96 * Get the node from the latest branch at a given position, 0 being the root
97 * and <size of latest branch - 1> being a leaf node.
98 *
99 * @param pos
100 * The position at which to return the node
101 * @return The node at position pos
102 */
103 public HTNode getNodeAt(int pos) {
104 List<HTNode> latest = getLatestBranch();
0e4f957e 105 return latest.get(pos);
f3476b68
GB
106 }
107
7c247a0f
GB
108 /**
109 * Get the depth of the tree
110 *
111 * @return The depth of the tree
112 */
113 public int getDepth() {
114 return getLatestBranch().size();
115 }
116
0e4eaca8
AM
117 // ------------------------------------------------------------------------
118 // Debug printing methods
119 // ------------------------------------------------------------------------
120
121 /**
122 * Print out the full tree for debugging purposes
123 *
124 * @param writer
125 * PrintWriter in which to write the output
126 * @param printIntervals
127 * Flag to enable full output of the interval information
128 * @param ts
129 * The timestamp that nodes have to intersect for intervals to be
130 * printed. A negative value will print intervals for all nodes.
131 * The timestamp only applies if printIntervals is true.
132 */
133 public void debugPrintFullTree(PrintWriter writer, boolean printIntervals, long ts) {
134 preOrderPrint(writer, false, getLatestBranch().get(0), 0, ts);
135
136 if (printIntervals) {
137 preOrderPrint(writer, true, getLatestBranch().get(0), 0, ts);
138 }
139 writer.println('\n');
140 }
141
142 /**
143 * Start at currentNode and print the contents of all its children, in
144 * pre-order. Give the root node in parameter to visit the whole tree, and
145 * have a nice overview.
146 */
147 private void preOrderPrint(PrintWriter writer, boolean printIntervals,
148 HTNode currentNode, int curDepth, long ts) {
149
150 writer.println(currentNode.toString());
151 /*
152 * Print intervals only if timestamp is negative or within the range of
153 * the node
154 */
155 if (printIntervals &&
156 (ts <= 0 ||
157 (ts >= currentNode.getNodeStart() && ts <= currentNode.getNodeEnd()))) {
158 currentNode.debugPrintIntervals(writer);
159 }
160
161 switch (currentNode.getNodeType()) {
162 case LEAF:
163 /* Stop if it's the leaf node */
164 return;
165
166 case CORE:
167 try {
168 final CoreNode node = (CoreNode) currentNode;
169 /* Print the extensions, if any */
170 int extension = node.getExtensionSequenceNumber();
171 while (extension != -1) {
172 HTNode nextNode = getTreeIO().readNode(extension);
173 preOrderPrint(writer, printIntervals, nextNode, curDepth, ts);
174 }
175
176 /* Print the child nodes */
177 for (int i = 0; i < node.getNbChildren(); i++) {
178 HTNode nextNode = getTreeIO().readNode(node.getChild(i));
179 for (int j = 0; j < curDepth; j++) {
180 writer.print(" ");
181 }
182 writer.print("+-");
183 preOrderPrint(writer, printIntervals, nextNode, curDepth + 1, ts);
184 }
185 } catch (ClosedChannelException e) {
186 }
187 break;
188
189 default:
190 break;
191 }
192 }
193
194 // ------------------------------------------------------------------------
195 // Assertion methods, for use with JUnit tests
196 // ------------------------------------------------------------------------
197
198 /**
199 * Check the integrity of all the nodes in the tree. Calls
200 * {@link #assertNodeIntegrity} for every node in the tree.
201 */
202 public void assertIntegrity() {
203 try {
204 for (int i = 0; i < getNodeCount(); i++) {
205 assertNodeIntegrity(getNode(i));
206 }
207 } catch (ClosedChannelException e) {
208 fail(e.getMessage());
209 }
210 }
211
212 /**
213 * Debugging method to make sure all intervals contained in the given node
214 * have valid start and end times.
215 *
216 * @param node
217 * The node to check
218 */
219 private void assertNodeIntegrity(HTNode node) {
220 if (node instanceof CoreNode) {
221 assertChildrenIntegrity((CoreNode) node);
222 }
223
224 /* Check that all intervals are within the node's range */
225 // TODO: Get the intervals of a node
226
227 }
228
a10a38ae
GB
229 private void assertChildrenIntegrity(CoreNode node) {
230 try {
231 /*
232 * Test that this node's start and end times match the start of the
233 * first child and the end of the last child, respectively
234 */
235 if (node.getNbChildren() > 0) {
236 HTNode childNode = getNode(node.getChild(0));
237 assertEquals("Equals start time of parent " + node.getSequenceNumber() + " and first child " + childNode.getSequenceNumber(),
238 node.getNodeStart(), childNode.getNodeStart());
239 if (node.isOnDisk()) {
240 childNode = getNode(node.getLatestChild());
241 assertEquals("Equals end time of parent " + node.getSequenceNumber() + " and last child " + childNode.getSequenceNumber(),
242 node.getNodeEnd(), childNode.getNodeEnd());
243 }
244 }
245
246 /*
247 * Test that the childStartTimes[] array matches the real nodes'
248 * start times
249 *
250 * Also test that children range is within the parent's range
251 */
252 for (int i = 0; i < node.getNbChildren(); i++) {
253 HTNode childNode = getNode(node.getChild(i));
254 assertEquals("Start time in parent " + node.getSequenceNumber() + " of child at index " + i,
255 childNode.getNodeStart(), node.getChildStart(i));
256 assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
257 node.getNodeStart() <= childNode.getNodeStart());
258 if (node.isOnDisk() && childNode.isOnDisk()) {
259 assertTrue("Child at index " + i + " of parent " + node.getSequenceNumber() + " has correct start time",
260 childNode.getNodeEnd() <= childNode.getNodeEnd());
261 }
262 }
263
264 } catch (ClosedChannelException e) {
265 fail(e.getMessage());
266 }
267 }
268
f3476b68 269}
This page took 0.049381 seconds and 5 git commands to generate.