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
.backend
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.fail
;
17 import java
.io
.IOException
;
19 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTConfig
;
20 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTInterval
;
21 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HTNode
;
22 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.HistoryTree
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
24 import org
.eclipse
.tracecompasss
.statesystem
.core
.tests
.stubs
.backend
.HistoryTreeStub
;
25 import org
.junit
.After
;
26 import org
.junit
.Before
;
27 import org
.junit
.Test
;
30 * Tests the history tree
32 * @author Geneviève Bastien
34 public class HistoryTreeTest
{
36 /* Minimal allowed blocksize */
37 private static final int BLOCK_SIZE
= HistoryTree
.TREE_HEADER_SIZE
;
38 /* The extra size used by long and double values */
39 private static final int LONG_DOUBLE_SIZE
= 8;
40 /* The number of extra characters to store a string interval */
41 private static final int STRING_PADDING
= 2;
43 /* String with 23 characters, interval in file will be 25 bytes long */
44 private static final String TEST_STRING
= "abcdefghifklmnopqrstuvw";
45 private static final TmfStateValue STRING_VALUE
= TmfStateValue
.newValueString(TEST_STRING
);
46 private static final TmfStateValue LONG_VALUE
= TmfStateValue
.newValueLong(10L);
47 private static final TmfStateValue INT_VALUE
= TmfStateValue
.newValueInt(1);
49 private File fTempFile
;
52 * Create the temporary file for this history tree
55 public void setupTest() {
57 fTempFile
= File
.createTempFile("tmpStateSystem", null);
58 } catch (IOException e
) {
64 * Delete the temporary history tree file after the test
67 public void cleanup() {
71 private HistoryTreeStub
setupSmallTree() {
72 HistoryTreeStub ht
= null;
74 File newFile
= fTempFile
;
75 assertNotNull(newFile
);
76 HTConfig config
= new HTConfig(newFile
,
78 3, /* Number of children */
79 1, /* Provider version */
81 ht
= new HistoryTreeStub(config
);
83 } catch (IOException e
) {
91 private static long fillValues(HistoryTree ht
, TmfStateValue value
, int nbValues
, long start
) {
92 for (int i
= 0; i
< nbValues
; i
++) {
93 ht
.insertInterval(new HTInterval(start
+ i
, start
+ i
+ 1, 1, value
));
95 return start
+ nbValues
;
99 * Test that nodes are filled
101 * It fills nodes with sequential intervals from one attribute only, so that
102 * leafs should be filled.
105 public void testSequentialFill() {
106 HistoryTreeStub ht
= setupSmallTree();
108 HTNode node
= ht
.getLatestLeaf();
109 assertEquals(0, node
.getNodeUsagePercent());
111 /* Add null intervals up to ~10% */
112 int nodeFreeSpace
= node
.getNodeFreeSpace();
113 int nbIntervals
= nodeFreeSpace
/ 10 / HTInterval
.DATA_ENTRY_SIZE
;
114 long start
= fillValues(ht
, TmfStateValue
.nullValue(), nbIntervals
, 1);
115 assertEquals(nodeFreeSpace
- nbIntervals
* HTInterval
.DATA_ENTRY_SIZE
, node
.getNodeFreeSpace());
117 /* Add integer intervals up to ~20% */
118 nodeFreeSpace
= node
.getNodeFreeSpace();
119 nbIntervals
= nodeFreeSpace
/ 10 / HTInterval
.DATA_ENTRY_SIZE
;
120 start
= fillValues(ht
, INT_VALUE
, nbIntervals
, start
);
121 assertEquals(nodeFreeSpace
- nbIntervals
* HTInterval
.DATA_ENTRY_SIZE
, node
.getNodeFreeSpace());
123 /* Add long intervals up to ~30% */
124 nodeFreeSpace
= node
.getNodeFreeSpace();
125 nbIntervals
= nodeFreeSpace
/ 10 / (HTInterval
.DATA_ENTRY_SIZE
+ LONG_DOUBLE_SIZE
);
126 start
= fillValues(ht
, LONG_VALUE
, nbIntervals
, start
);
127 assertEquals(nodeFreeSpace
- nbIntervals
* (HTInterval
.DATA_ENTRY_SIZE
+ LONG_DOUBLE_SIZE
), node
.getNodeFreeSpace());
129 /* Add string intervals up to ~40% */
130 nodeFreeSpace
= node
.getNodeFreeSpace();
131 nbIntervals
= nodeFreeSpace
/ 10 / (HTInterval
.DATA_ENTRY_SIZE
+ TEST_STRING
.length() + STRING_PADDING
);
132 start
= fillValues(ht
, STRING_VALUE
, nbIntervals
, start
);
133 assertEquals(nodeFreeSpace
- nbIntervals
* (HTInterval
.DATA_ENTRY_SIZE
+ TEST_STRING
.length() + STRING_PADDING
), node
.getNodeFreeSpace());