1 /*******************************************************************************
2 * Copyright (c) 2016 É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
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.fail
;
15 import org
.eclipse
.jdt
.annotation
.NonNull
;
16 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
17 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemBuilderUtils
;
18 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemFactory
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
25 import org
.junit
.Before
;
26 import org
.junit
.Test
;
29 * Test the {@link StateSystemBuilderUtils} class
31 * @author Geneviève Bastien
33 public class StateSystemBuilderUtilsTest
{
35 private static final long START_TIME
= 1000L;
36 private static final long TIME_INCREMENT
= 10;
37 private static final @NonNull String DUMMY_STRING
= "test";
39 private ITmfStateSystemBuilder fStateSystem
;
42 * Build a small test state system in memory
45 public void setupStateSystem() {
47 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(DUMMY_STRING
, START_TIME
);
48 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
50 } catch (StateValueTypeException e
) {
57 * {@link StateSystemBuilderUtils#incrementAttributeLong(ITmfStateSystemBuilder, long, int, long)}
61 public void testIncrementLong() {
62 ITmfStateSystemBuilder ss
= fStateSystem
;
63 int quark
= ss
.getQuarkAbsoluteAndAdd(DUMMY_STRING
);
66 /* Value should be null at the beginning */
67 ITmfStateValue value
= ss
.queryOngoingState(quark
);
68 assertEquals(TmfStateValue
.nullValue(), value
);
72 StateSystemBuilderUtils
.incrementAttributeLong(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment
);
73 value
= ss
.queryOngoingState(quark
);
74 assertEquals(TmfStateValue
.newValueLong(increment
), value
);
76 /* Increment by 1000 */
77 Long increment2
= 1000L;
78 StateSystemBuilderUtils
.incrementAttributeLong(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment2
);
79 value
= ss
.queryOngoingState(quark
);
80 assertEquals(TmfStateValue
.newValueLong(increment
+ increment2
), value
);
82 /* Increment by a negative value */
83 Long increment3
= -500L;
84 StateSystemBuilderUtils
.incrementAttributeLong(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment3
);
85 value
= ss
.queryOngoingState(quark
);
86 assertEquals(TmfStateValue
.newValueLong(increment
+ increment2
+ increment3
), value
);
88 } catch (AttributeNotFoundException e
) {
95 * {@link StateSystemBuilderUtils#incrementAttributeInt(ITmfStateSystemBuilder, long, int, int)}
99 public void testIncrementInt() {
100 ITmfStateSystemBuilder ss
= fStateSystem
;
101 int quark
= ss
.getQuarkAbsoluteAndAdd(DUMMY_STRING
);
104 /* Value should be null at the beginning */
105 ITmfStateValue value
= ss
.queryOngoingState(quark
);
106 assertEquals(TmfStateValue
.nullValue(), value
);
110 StateSystemBuilderUtils
.incrementAttributeInt(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment
);
111 value
= ss
.queryOngoingState(quark
);
112 assertEquals(TmfStateValue
.newValueInt(increment
), value
);
114 /* Increment by 1000 */
115 int increment2
= 1000;
116 StateSystemBuilderUtils
.incrementAttributeInt(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment2
);
117 value
= ss
.queryOngoingState(quark
);
118 assertEquals(TmfStateValue
.newValueInt(increment
+ increment2
), value
);
120 /* Increment by a negative value */
121 int increment3
= -500;
122 StateSystemBuilderUtils
.incrementAttributeInt(ss
, START_TIME
+ TIME_INCREMENT
, quark
, increment3
);
123 value
= ss
.queryOngoingState(quark
);
124 assertEquals(TmfStateValue
.newValueInt(increment
+ increment2
+ increment3
), value
);
126 } catch (AttributeNotFoundException e
) {
127 fail(e
.getMessage());