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