d6675c59bf0e2599ca372c23821862fc29fa0748
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 /* 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
);
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
++) {
183 preOrderPrint(writer
, printIntervals
, nextNode
, curDepth
+ 1, ts
);
185 } catch (ClosedChannelException e
) {
194 // ------------------------------------------------------------------------
195 // Assertion methods, for use with JUnit tests
196 // ------------------------------------------------------------------------
199 * Check the integrity of all the nodes in the tree. Calls
200 * {@link #assertNodeIntegrity} for every node in the tree.
202 public void assertIntegrity() {
204 for (int i
= 0; i
< getNodeCount(); i
++) {
205 assertNodeIntegrity(getNode(i
));
207 } catch (ClosedChannelException e
) {
208 fail(e
.getMessage());
213 * Debugging method to make sure all intervals contained in the given node
214 * have valid start and end times.
219 private void assertNodeIntegrity(HTNode node
) {
220 if (node
instanceof CoreNode
) {
221 assertChildrenIntegrity((CoreNode
) node
);
224 /* Check that all intervals are within the node's range */
225 // TODO: Get the intervals of a node
229 private void assertChildrenIntegrity(CoreNode node
) {
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
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());
247 * Test that the childStartTimes[] array matches the real nodes'
250 * Also test that children range is within the parent's range
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());
264 } catch (ClosedChannelException e
) {
265 fail(e
.getMessage());
This page took 0.037496 seconds and 4 git commands to generate.