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
.Activator
;
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
.IHistoryTree
;
27 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.ParentNode
;
28 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.classic
.CoreNode
;
29 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.classic
.HistoryTreeClassic
;
31 import com
.google
.common
.collect
.Iterables
;
34 * Stub class to unit test the history tree. You can set the size of the
35 * interval section before using the tree, in order to fine-tune the test.
37 * Note to developers: This tree is not meant to be used with a backend. It just
38 * exposes some info from the history tree.
40 * @author Geneviève Bastien
42 public class HistoryTreeClassicStub
extends HistoryTreeClassic
{
45 * Minimum size a block of this tree should have
47 public static final int MINIMUM_BLOCK_SIZE
= IHistoryTree
.TREE_HEADER_SIZE
;
50 * Constructor for this history tree stub
53 * The config to use for this History Tree.
55 * If an error happens trying to open/write to the file
56 * specified in the config
58 public HistoryTreeClassicStub(HTConfig conf
) throws IOException
{
63 * "Reader" constructor : instantiate a SHTree from an existing tree file on
66 * @param existingStateFile
67 * Path/filename of the history-file we are to open
68 * @param expProviderVersion
69 * The expected version of the state provider
71 * If an error happens reading the file
73 public HistoryTreeClassicStub(File existingStateFile
, int expProviderVersion
) throws IOException
{
74 super(existingStateFile
, expProviderVersion
);
77 // ------------------------------------------------------------------------
78 // Extra test accessors
79 // ------------------------------------------------------------------------
82 public List
<HTNode
> getLatestBranch() {
83 /* Super method is not public */
84 return checkNotNull(super.getLatestBranch());
88 * Get the latest leaf of the tree
90 * @return The current leaf node of the tree
92 public HTNode
getLatestLeaf() {
93 List
<HTNode
> latest
= getLatestBranch();
94 return Iterables
.getLast(latest
);
98 * Get the node from the latest branch at a given position, 0 being the root
99 * and <size of latest branch - 1> being a leaf node.
102 * The position at which to return the node
103 * @return The node at position pos
105 public HTNode
getNodeAt(int pos
) {
106 List
<HTNode
> latest
= getLatestBranch();
107 return latest
.get(pos
);
111 * Get the depth of the tree
113 * @return The depth of the tree
115 public int getDepth() {
116 return getLatestBranch().size();
119 // ------------------------------------------------------------------------
120 // Debug printing methods
121 // ------------------------------------------------------------------------
124 * Print out the full tree for debugging purposes
127 * PrintWriter in which to write the output
128 * @param printIntervals
129 * Flag to enable full output of the interval information
131 * The timestamp that nodes have to intersect for intervals to be
132 * printed. A negative value will print intervals for all nodes.
133 * The timestamp only applies if printIntervals is true.
135 public void debugPrintFullTree(PrintWriter writer
, boolean printIntervals
, long ts
) {
136 preOrderPrint(writer
, false, getLatestBranch().get(0), 0, ts
);
138 if (printIntervals
) {
139 preOrderPrint(writer
, true, getLatestBranch().get(0), 0, ts
);
141 writer
.println('\n');
145 * Start at currentNode and print the contents of all its children, in
146 * pre-order. Give the root node in parameter to visit the whole tree, and
147 * have a nice overview.
149 private void preOrderPrint(PrintWriter writer
, boolean printIntervals
,
150 HTNode currentNode
, int curDepth
, long ts
) {
152 writer
.println(currentNode
.toString());
154 * Print intervals only if timestamp is negative or within the range of
157 if (printIntervals
&&
159 (ts
>= currentNode
.getNodeStart() && ts
<= currentNode
.getNodeEnd()))) {
160 currentNode
.debugPrintIntervals(writer
);
163 switch (currentNode
.getNodeType()) {
165 /* Stop if it's the leaf node */
170 final CoreNode node
= (CoreNode
) currentNode
;
171 /* If node extensions were used, they would be printed here. */
173 /* Print the child nodes */
174 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
175 HTNode nextNode
= getTreeIO().readNode(node
.getChild(i
));
176 for (int j
= 0; j
< curDepth
; j
++) {
180 preOrderPrint(writer
, printIntervals
, nextNode
, curDepth
+ 1, ts
);
182 } catch (ClosedChannelException e
) {
183 Activator
.getDefault().logError(e
.getMessage());
192 // ------------------------------------------------------------------------
193 // Assertion methods, for use with JUnit tests
194 // ------------------------------------------------------------------------
197 * Check the integrity of all the nodes in the tree. Calls
198 * {@link #assertNodeIntegrity} for every node in the tree.
200 public void assertIntegrity() {
202 for (int i
= 0; i
< getNodeCount(); i
++) {
203 assertNodeIntegrity(getNode(i
));
205 } catch (ClosedChannelException e
) {
206 fail(e
.getMessage());
211 * Debugging method to make sure all intervals contained in the given node
212 * have valid start and end times.
217 private void assertNodeIntegrity(HTNode node
) {
218 if (node
instanceof ParentNode
) {
219 assertChildrenIntegrity((ParentNode
) node
);
222 /* Check that all intervals are within the node's range */
223 // TODO: Get the intervals of a node
227 private void assertChildrenIntegrity(ParentNode node
) {
230 * Test that this node's start and end times match the start of the
231 * first child and the end of the last child, respectively
233 if (node
.getNbChildren() > 0) {
234 HTNode childNode
= getNode(node
.getChild(0));
235 assertEquals("Equals start time of parent " + node
.getSequenceNumber() + " and first child " + childNode
.getSequenceNumber(),
236 node
.getNodeStart(), childNode
.getNodeStart());
237 if (node
.isOnDisk()) {
238 childNode
= getNode(node
.getLatestChild());
239 assertEquals("Equals end time of parent " + node
.getSequenceNumber() + " and last child " + childNode
.getSequenceNumber(),
240 node
.getNodeEnd(), childNode
.getNodeEnd());
245 * Test that the childStartTimes[] array matches the real nodes'
248 * Also test that children range is within the parent's range
250 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
251 HTNode childNode
= getNode(node
.getChild(i
));
252 assertEquals("Start time in parent " + node
.getSequenceNumber() + " of child at index " + i
,
253 childNode
.getNodeStart(), node
.getChildStart(i
));
254 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
255 node
.getNodeStart() <= childNode
.getNodeStart());
256 if (node
.isOnDisk() && childNode
.isOnDisk()) {
257 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
258 childNode
.getNodeEnd() <= childNode
.getNodeEnd());
262 } catch (ClosedChannelException e
) {
263 fail(e
.getMessage());
This page took 0.043207 seconds and 5 git commands to generate.