ss: Add a StateSystemUtils for advanced queries
[deliverable/tracecompass.git] / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / StateSystemUtilsTest.java
CommitLineData
1dd75589
AM
1/*******************************************************************************
2 * Copyright (c) 2014 É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 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.statesystem.core.tests;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.fail;
19
20import org.eclipse.jdt.annotation.NonNull;
21import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
22import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
23import org.eclipse.tracecompass.statesystem.core.StateSystemFactory;
24import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
25import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
26import org.eclipse.tracecompass.statesystem.core.backend.InMemoryBackend;
27import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
28import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
29import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
30import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
31import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Test;
35
36/**
37 * Test the {@link StateSystemUtils} class
38 *
39 * @author Geneviève Bastien
40 */
41public class StateSystemUtilsTest {
42
43 private static final long START_TIME = 1000L;
44 private static final @NonNull String DUMMY_STRING = "test";
45 private static final int INT_VAL = 10;
46
47 private ITmfStateSystemBuilder fStateSystem;
48
49 /**
50 * Build a small test state system in memory
51 */
52 @Before
53 public void setupStateSystem() {
54 try {
55 IStateHistoryBackend backend = new InMemoryBackend(START_TIME);
56 fStateSystem = StateSystemFactory.newStateSystem(DUMMY_STRING, backend);
57 int quark = fStateSystem.getQuarkAbsoluteAndAdd(DUMMY_STRING);
58
59 fStateSystem.modifyAttribute(1200L, TmfStateValue.newValueInt(INT_VAL), quark);
60 fStateSystem.modifyAttribute(1500L, TmfStateValue.newValueInt(20), quark);
61 fStateSystem.closeHistory(2000L);
62 } catch (StateValueTypeException | AttributeNotFoundException e) {
63 fail(e.getMessage());
64 }
65 }
66
67 /**
68 * Clean-up
69 */
70 @After
71 public void tearDown() {
72 fStateSystem.dispose();
73 }
74
75 /**
76 * Test the {@link StateSystemUtils#queryUntilNonNullValue} method.
77 */
78 @Test
79 public void testQueryUntilNonNullValue() {
80 ITmfStateSystem ss = fStateSystem;
81 assertNotNull(ss);
82
83 int quark;
84 try {
85 quark = ss.getQuarkAbsolute(DUMMY_STRING);
86
87 /* Should return null if requested range is not within range */
88 assertNull(StateSystemUtils.queryUntilNonNullValue(ss, quark, 0, 999L));
89 assertNull(StateSystemUtils.queryUntilNonNullValue(ss, quark, 2001L, 5000L));
90
91 /*
92 * Should return null if request within range, but condition is
93 * false
94 */
95 assertNull(StateSystemUtils.queryUntilNonNullValue(ss, quark, 1000L, 1199L));
96
97 /*
98 * Should return the right interval if an interval is within range,
99 * even if the range starts or ends outside state system range
100 */
101 ITmfStateInterval interval = StateSystemUtils.queryUntilNonNullValue(ss, quark, 1000L, 1300L);
102 assertNotNull(interval);
103 assertEquals(ITmfStateValue.Type.INTEGER, interval.getStateValue().getType());
104 assertEquals(INT_VAL, interval.getStateValue().unboxInt());
105
106 interval = StateSystemUtils.queryUntilNonNullValue(ss, quark, 800L, 2500L);
107 assertNotNull(interval);
108 assertEquals(ITmfStateValue.Type.INTEGER, interval.getStateValue().getType());
109 assertEquals(INT_VAL, interval.getStateValue().unboxInt());
110
111 interval = StateSystemUtils.queryUntilNonNullValue(ss, quark, 1500L, 2500L);
112 assertNull(interval);
113
114 } catch (AttributeNotFoundException e) {
115 fail(e.getMessage());
116 }
117
118 }
119
120}
This page took 0.028394 seconds and 5 git commands to generate.