b46894d47819559ce90702ba1513b8f93a77e9f8
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
.HistoryTreeClassic
;
26 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.IHistoryTree
;
28 import com
.google
.common
.collect
.Iterables
;
31 * Stub class to unit test the history tree. You can set the size of the
32 * interval section before using the tree, in order to fine-tune the test.
34 * Note to developers: This tree is not meant to be used with a backend. It just
35 * exposes some info from the history tree.
37 * @author Geneviève Bastien
39 public class HistoryTreeClassicStub
extends HistoryTreeClassic
{
42 * Minimum size a block of this tree should have
44 public static final int MINIMUM_BLOCK_SIZE
= IHistoryTree
.TREE_HEADER_SIZE
;
47 * Constructor for this history tree stub
50 * The config to use for this History Tree.
52 * If an error happens trying to open/write to the file
53 * specified in the config
55 public HistoryTreeClassicStub(HTConfig conf
) throws IOException
{
60 * "Reader" constructor : instantiate a SHTree from an existing tree file on
63 * @param existingStateFile
64 * Path/filename of the history-file we are to open
65 * @param expProviderVersion
66 * The expected version of the state provider
68 * If an error happens reading the file
70 public HistoryTreeClassicStub(File existingStateFile
, int expProviderVersion
) throws IOException
{
71 super(existingStateFile
, expProviderVersion
);
75 public List
<HTNode
> getLatestBranch() {
76 return checkNotNull(super.getLatestBranch());
80 * Get the latest leaf of the tree
82 * @return The current leaf node of the tree
84 public HTNode
getLatestLeaf() {
85 List
<HTNode
> latest
= getLatestBranch();
86 return Iterables
.getLast(latest
);
90 * Get the node from the latest branch at a given position, 0 being the root
91 * and <size of latest branch - 1> being a leaf node.
94 * The position at which to return the node
95 * @return The node at position pos
97 public HTNode
getNodeAt(int pos
) {
98 List
<HTNode
> latest
= getLatestBranch();
99 return latest
.get(pos
);
103 * Get the depth of the tree
105 * @return The depth of the tree
107 public int getDepth() {
108 return getLatestBranch().size();
111 private void assertChildrenIntegrity(CoreNode node
) {
114 * Test that this node's start and end times match the start of the
115 * first child and the end of the last child, respectively
117 if (node
.getNbChildren() > 0) {
118 HTNode childNode
= getNode(node
.getChild(0));
119 assertEquals("Equals start time of parent " + node
.getSequenceNumber() + " and first child " + childNode
.getSequenceNumber(),
120 node
.getNodeStart(), childNode
.getNodeStart());
121 if (node
.isOnDisk()) {
122 childNode
= getNode(node
.getLatestChild());
123 assertEquals("Equals end time of parent " + node
.getSequenceNumber() + " and last child " + childNode
.getSequenceNumber(),
124 node
.getNodeEnd(), childNode
.getNodeEnd());
129 * Test that the childStartTimes[] array matches the real nodes'
132 * Also test that children range is within the parent's range
134 for (int i
= 0; i
< node
.getNbChildren(); i
++) {
135 HTNode childNode
= getNode(node
.getChild(i
));
136 assertEquals("Start time in parent " + node
.getSequenceNumber() + " of child at index " + i
,
137 childNode
.getNodeStart(), node
.getChildStart(i
));
138 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
139 node
.getNodeStart() <= childNode
.getNodeStart());
140 if (node
.isOnDisk() && childNode
.isOnDisk()) {
141 assertTrue("Child at index " + i
+ " of parent " + node
.getSequenceNumber() + " has correct start time",
142 childNode
.getNodeEnd() <= childNode
.getNodeEnd());
146 } catch (ClosedChannelException e
) {
147 fail(e
.getMessage());
152 * Debugging method to make sure all intervals contained in the given node
153 * have valid start and end times.
158 private void assertNodeIntegrity(HTNode node
) {
159 if (node
instanceof CoreNode
) {
160 assertChildrenIntegrity((CoreNode
) node
);
163 /* Check that all intervals are within the node's range */
164 // TODO: Get the intervals of a node
169 * Check the integrity of all the nodes in the tree. Calls
170 * {@link #assertNodeIntegrity} for every node in the tree.
172 public void assertIntegrity() {
174 for (int i
= 0; i
< getNodeCount(); i
++) {
175 assertNodeIntegrity(getNode(i
));
177 } catch (ClosedChannelException e
) {
178 fail(e
.getMessage());
This page took 0.035126 seconds and 5 git commands to generate.