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
.nio
.channels
.ClosedChannelException
;
20 import java
.util
.List
;
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
;
27 import com
.google
.common
.collect
.Iterables
;
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.
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.
36 * @author Geneviève Bastien
38 public class HistoryTreeStub
extends HistoryTree
{
41 * Constructor for this history tree stub
44 * The config to use for this History Tree.
46 * If an error happens trying to open/write to the file
47 * specified in the config
49 public HistoryTreeStub(HTConfig conf
) throws IOException
{
54 * "Reader" constructor : instantiate a SHTree from an existing tree file on
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
62 * If an error happens reading the file
64 public HistoryTreeStub(File existingStateFile
, int expProviderVersion
) throws IOException
{
65 super(existingStateFile
, expProviderVersion
);
69 public List
<HTNode
> getLatestBranch() {
70 return checkNotNull(super.getLatestBranch());
74 * Get the latest leaf of the tree
76 * @return The current leaf node of the tree
78 public HTNode
getLatestLeaf() {
79 List
<HTNode
> latest
= getLatestBranch();
80 return Iterables
.getLast(latest
);
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.
88 * The position at which to return the node
89 * @return The node at position pos
91 public HTNode
getNodeAt(int pos
) {
92 List
<HTNode
> latest
= getLatestBranch();
93 return latest
.get(pos
);
97 * Get the depth of the tree
99 * @return The depth of the tree
101 public int getDepth() {
102 return getLatestBranch().size();
105 private void assertChildrenIntegrity(CoreNode node
) {
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
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());
123 * Test that the childStartTimes[] array matches the real nodes'
126 * Also test that children range is within the parent's range
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());
140 } catch (ClosedChannelException e
) {
141 fail(e
.getMessage());
146 * Debugging method to make sure all intervals contained in the given node
147 * have valid start and end times.
152 private void assertNodeIntegrity(HTNode node
) {
153 if (node
instanceof CoreNode
) {
154 assertChildrenIntegrity((CoreNode
) node
);
157 /* Check that all intervals are within the node's range */
158 // TODO: Get the intervals of a node
163 * Check the integrity of all the nodes in the tree. Calls
164 * {@link #assertNodeIntegrity} for every node in the tree.
166 public void assertIntegrity() {
168 for (int i
= 0; i
< getNodeCount(); i
++) {
169 assertNodeIntegrity(getNode(i
));
171 } catch (ClosedChannelException e
) {
172 fail(e
.getMessage());
This page took 0.038523 seconds and 5 git commands to generate.