49354ea7db5d3c247b20fc18691ac5e4af9ae9c9
1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
.stubs
.backend
;
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
;
18 import java
.io
.IOException
;
19 import java
.io
.PrintWriter
;
20 import java
.nio
.channels
.ClosedChannelException
;
21 import java
.util
.List
;
23 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.CoreNode
;
24 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTConfig
;
25 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTNode
;
26 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HistoryTreeClassic
;
27 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.IHistoryTree
;
29 import com
.google
.common
.collect
.Iterables
;
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.
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.
38 * @author Geneviève Bastien
40 public class HistoryTreeClassicStub
extends HistoryTreeClassic
{
43 * Minimum size a block of this tree should have
45 public static final int MINIMUM_BLOCK_SIZE
= IHistoryTree
.TREE_HEADER_SIZE
;
48 * Constructor for this history tree stub
51 * The config to use for this History Tree.
53 * If an error happens trying to open/write to the file
54 * specified in the config
56 public HistoryTreeClassicStub(HTConfig conf
) throws IOException
{
61 * "Reader" constructor : instantiate a SHTree from an existing tree file on
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
69 * If an error happens reading the file
71 public HistoryTreeClassicStub(File existingStateFile
, int expProviderVersion
) throws IOException
{
72 super(existingStateFile
, expProviderVersion
);
75 // ------------------------------------------------------------------------
76 // Extra test accessors
77 // ------------------------------------------------------------------------
80 public List
<HTNode
> getLatestBranch() {
81 /* Super method is not public */
82 return checkNotNull(super.getLatestBranch());
86 * Get the latest leaf of the tree
88 * @return The current leaf node of the tree
90 public HTNode
getLatestLeaf() {
91 List
<HTNode
> latest
= getLatestBranch();
92 return Iterables
.getLast(latest
);
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.
100 * The position at which to return the node
101 * @return The node at position pos
103 public HTNode
getNodeAt(int pos
) {
104 List
<HTNode
> latest
= getLatestBranch();
105 return latest
.get(pos
);
109 * Get the depth of the tree
111 * @return The depth of the tree
113 public int getDepth() {
114 return getLatestBranch().size();
117 // ------------------------------------------------------------------------
118 // Debug printing methods
119 // ------------------------------------------------------------------------
122 * Print out the full tree for debugging purposes
125 * PrintWriter in which to write the output
126 * @param printIntervals
127 * Flag to enable full output of the interval information
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.
133 public void debugPrintFullTree(PrintWriter writer
, boolean printIntervals
, long ts
) {
134 preOrderPrint(writer
, false, getLatestBranch().get(0), 0, ts
);
136 if (printIntervals
) {
137 preOrderPrint(writer
, true, getLatestBranch().get(0), 0, ts
);
139 writer
.println('\n');
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.
147 private void preOrderPrint(PrintWriter writer
, boolean printIntervals
,
148 HTNode currentNode
, int curDepth
, long ts
) {
150 writer
.println(currentNode
.toString());
152 * Print intervals only if timestamp is negative or within the range of
155 if (printIntervals
&&
157 (ts
>= currentNode
.getNodeStart() && ts
<= currentNode
.getNodeEnd()))) {
158 currentNode
.debugPrintIntervals(writer
);
161 switch (currentNode
.getNodeType()) {
163 /* Stop if it's the leaf node */
168 final CoreNode node
= (CoreNode
) currentNode
;
169 /* If node extensions were used, they would be printed here. */
171 /* Print the child nodes */
172 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
173 HTNode nextNode
= getTreeIO().readNode(node
.getChild(i
));
174 for (int j
= 0; j
< curDepth
; j
++) {
178 preOrderPrint(writer
, printIntervals
, nextNode
, curDepth
+ 1, ts
);
180 } catch (ClosedChannelException e
) {
189 // ------------------------------------------------------------------------
190 // Assertion methods, for use with JUnit tests
191 // ------------------------------------------------------------------------
194 * Check the integrity of all the nodes in the tree. Calls
195 * {@link #assertNodeIntegrity} for every node in the tree.
197 public void assertIntegrity() {
199 for (int i
= 0; i
< getNodeCount(); i
++) {
200 assertNodeIntegrity(getNode(i
));
202 } catch (ClosedChannelException e
) {
203 fail(e
.getMessage());
208 * Debugging method to make sure all intervals contained in the given node
209 * have valid start and end times.
214 private void assertNodeIntegrity(HTNode node
) {
215 if (node
instanceof CoreNode
) {
216 assertChildrenIntegrity((CoreNode
) node
);
219 /* Check that all intervals are within the node's range */
220 // TODO: Get the intervals of a node
224 private void assertChildrenIntegrity(CoreNode node
) {
227 * Test that this node's start and end times match the start of the
228 * first child and the end of the last child, respectively
230 if (node
.getNbChildren() > 0) {
231 HTNode childNode
= getNode(node
.getChild(0));
232 assertEquals("Equals start time of parent " + node
.getSequenceNumber() + " and first child " + childNode
.getSequenceNumber(),
233 node
.getNodeStart(), childNode
.getNodeStart());
234 if (node
.isOnDisk()) {
235 childNode
= getNode(node
.getLatestChild());
236 assertEquals("Equals end time of parent " + node
.getSequenceNumber() + " and last child " + childNode
.getSequenceNumber(),
237 node
.getNodeEnd(), childNode
.getNodeEnd());
242 * Test that the childStartTimes[] array matches the real nodes'
245 * Also test that children range is within the parent's range
247 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
248 HTNode childNode
= getNode(node
.getChild(i
));
249 assertEquals("Start time in parent " + node
.getSequenceNumber() + " of child at index " + i
,
250 childNode
.getNodeStart(), node
.getChildStart(i
));
251 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
252 node
.getNodeStart() <= childNode
.getNodeStart());
253 if (node
.isOnDisk() && childNode
.isOnDisk()) {
254 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
255 childNode
.getNodeEnd() <= childNode
.getNodeEnd());
259 } catch (ClosedChannelException e
) {
260 fail(e
.getMessage());
This page took 0.065508 seconds and 5 git commands to generate.