Commit | Line | Data |
---|---|---|
f3476b68 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 École Polytechnique de Montréal | |
3 | * | |
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 | *******************************************************************************/ | |
9 | ||
5eb1b4b0 | 10 | package org.eclipse.tracecompass.statesystem.core.tests.stubs.backend; |
f3476b68 GB |
11 | |
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
a10a38ae GB |
13 | import static org.junit.Assert.assertEquals; |
14 | import static org.junit.Assert.assertTrue; | |
15 | import static org.junit.Assert.fail; | |
f3476b68 | 16 | |
068641fa | 17 | import java.io.File; |
f3476b68 | 18 | import java.io.IOException; |
a10a38ae | 19 | import java.nio.channels.ClosedChannelException; |
f3476b68 GB |
20 | import java.util.List; |
21 | ||
a10a38ae | 22 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.CoreNode; |
f3476b68 GB |
23 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTConfig; |
24 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode; | |
3a081e85 GB |
25 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeClassic; |
26 | import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.IHistoryTree; | |
f3476b68 GB |
27 | |
28 | import com.google.common.collect.Iterables; | |
29 | ||
30 | /** | |
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. | |
33 | * | |
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. | |
36 | * | |
37 | * @author Geneviève Bastien | |
38 | */ | |
3a081e85 GB |
39 | public class HistoryTreeClassicStub extends HistoryTreeClassic { |
40 | ||
41 | /** | |
42 | * Minimum size a block of this tree should have | |
43 | */ | |
44 | public static final int MINIMUM_BLOCK_SIZE = IHistoryTree.TREE_HEADER_SIZE; | |
f3476b68 GB |
45 | |
46 | /** | |
47 | * Constructor for this history tree stub | |
48 | * | |
49 | * @param conf | |
50 | * The config to use for this History Tree. | |
51 | * @throws IOException | |
52 | * If an error happens trying to open/write to the file | |
53 | * specified in the config | |
54 | */ | |
3a081e85 | 55 | public HistoryTreeClassicStub(HTConfig conf) throws IOException { |
f3476b68 GB |
56 | super(conf); |
57 | } | |
58 | ||
068641fa GB |
59 | /** |
60 | * "Reader" constructor : instantiate a SHTree from an existing tree file on | |
61 | * disk | |
62 | * | |
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 | |
67 | * @throws IOException | |
68 | * If an error happens reading the file | |
69 | */ | |
3a081e85 | 70 | public HistoryTreeClassicStub(File existingStateFile, int expProviderVersion) throws IOException { |
068641fa GB |
71 | super(existingStateFile, expProviderVersion); |
72 | } | |
73 | ||
b2ca67ca PT |
74 | @Override |
75 | public List<HTNode> getLatestBranch() { | |
76 | return checkNotNull(super.getLatestBranch()); | |
77 | } | |
78 | ||
f3476b68 GB |
79 | /** |
80 | * Get the latest leaf of the tree | |
81 | * | |
82 | * @return The current leaf node of the tree | |
83 | */ | |
84 | public HTNode getLatestLeaf() { | |
85 | List<HTNode> latest = getLatestBranch(); | |
0e4f957e | 86 | return Iterables.getLast(latest); |
f3476b68 GB |
87 | } |
88 | ||
89 | /** | |
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. | |
92 | * | |
93 | * @param pos | |
94 | * The position at which to return the node | |
95 | * @return The node at position pos | |
96 | */ | |
97 | public HTNode getNodeAt(int pos) { | |
98 | List<HTNode> latest = getLatestBranch(); | |
0e4f957e | 99 | return latest.get(pos); |
f3476b68 GB |
100 | } |
101 | ||
7c247a0f GB |
102 | /** |
103 | * Get the depth of the tree | |
104 | * | |
105 | * @return The depth of the tree | |
106 | */ | |
107 | public int getDepth() { | |
108 | return getLatestBranch().size(); | |
109 | } | |
110 | ||
a10a38ae GB |
111 | private void assertChildrenIntegrity(CoreNode node) { |
112 | try { | |
113 | /* | |
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 | |
116 | */ | |
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()); | |
125 | } | |
126 | } | |
127 | ||
128 | /* | |
129 | * Test that the childStartTimes[] array matches the real nodes' | |
130 | * start times | |
131 | * | |
132 | * Also test that children range is within the parent's range | |
133 | */ | |
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()); | |
143 | } | |
144 | } | |
145 | ||
146 | } catch (ClosedChannelException e) { | |
147 | fail(e.getMessage()); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Debugging method to make sure all intervals contained in the given node | |
153 | * have valid start and end times. | |
154 | * | |
155 | * @param node | |
156 | * The node to check | |
157 | */ | |
158 | private void assertNodeIntegrity(HTNode node) { | |
159 | if (node instanceof CoreNode) { | |
160 | assertChildrenIntegrity((CoreNode) node); | |
161 | } | |
162 | ||
163 | /* Check that all intervals are within the node's range */ | |
164 | // TODO: Get the intervals of a node | |
165 | ||
166 | } | |
167 | ||
168 | /** | |
169 | * Check the integrity of all the nodes in the tree. Calls | |
170 | * {@link #assertNodeIntegrity} for every node in the tree. | |
171 | */ | |
172 | public void assertIntegrity() { | |
173 | try { | |
174 | for (int i = 0; i < getNodeCount(); i++) { | |
175 | assertNodeIntegrity(getNode(i)); | |
176 | } | |
177 | } catch (ClosedChannelException e) { | |
178 | fail(e.getMessage()); | |
179 | } | |
180 | } | |
181 | ||
f3476b68 | 182 | } |