Commit | Line | Data |
---|---|---|
acec47ce GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 É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 | ||
10 | package org.eclipse.tracecompass.statesystem.core.tests; | |
11 | ||
12 | import static org.junit.Assert.assertEquals; | |
13 | import static org.junit.Assert.fail; | |
14 | ||
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; | |
27 | ||
28 | /** | |
29 | * Test the {@link StateSystemBuilderUtils} class | |
30 | * | |
31 | * @author Geneviève Bastien | |
32 | */ | |
33 | public class StateSystemBuilderUtilsTest { | |
34 | ||
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"; | |
38 | ||
39 | private ITmfStateSystemBuilder fStateSystem; | |
40 | ||
41 | /** | |
42 | * Build a small test state system in memory | |
43 | */ | |
44 | @Before | |
45 | public void setupStateSystem() { | |
46 | try { | |
47 | IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(DUMMY_STRING, START_TIME); | |
48 | fStateSystem = StateSystemFactory.newStateSystem(backend); | |
49 | ||
50 | } catch (StateValueTypeException e) { | |
51 | fail(e.getMessage()); | |
52 | } | |
53 | } | |
54 | ||
55 | /** | |
56 | * Test the | |
57 | * {@link StateSystemBuilderUtils#incrementAttributeLong(ITmfStateSystemBuilder, long, int, long)} | |
58 | * method | |
59 | */ | |
60 | @Test | |
61 | public void testIncrementLong() { | |
62 | ITmfStateSystemBuilder ss = fStateSystem; | |
63 | int quark = ss.getQuarkAbsoluteAndAdd(DUMMY_STRING); | |
64 | ||
65 | try { | |
66 | /* Value should be null at the beginning */ | |
67 | ITmfStateValue value = ss.queryOngoingState(quark); | |
68 | assertEquals(TmfStateValue.nullValue(), value); | |
69 | ||
70 | /* Increment by 3 */ | |
71 | long increment = 3; | |
72 | StateSystemBuilderUtils.incrementAttributeLong(ss, START_TIME + TIME_INCREMENT, quark, increment); | |
73 | value = ss.queryOngoingState(quark); | |
74 | assertEquals(TmfStateValue.newValueLong(increment), value); | |
75 | ||
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); | |
81 | ||
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); | |
87 | ||
88 | } catch (AttributeNotFoundException e) { | |
89 | fail(e.getMessage()); | |
90 | } | |
91 | } | |
92 | ||
93 | /** | |
94 | * Test the | |
95 | * {@link StateSystemBuilderUtils#incrementAttributeInt(ITmfStateSystemBuilder, long, int, int)} | |
96 | * method | |
97 | */ | |
98 | @Test | |
99 | public void testIncrementInt() { | |
100 | ITmfStateSystemBuilder ss = fStateSystem; | |
101 | int quark = ss.getQuarkAbsoluteAndAdd(DUMMY_STRING); | |
102 | ||
103 | try { | |
104 | /* Value should be null at the beginning */ | |
105 | ITmfStateValue value = ss.queryOngoingState(quark); | |
106 | assertEquals(TmfStateValue.nullValue(), value); | |
107 | ||
108 | /* Increment by 3 */ | |
109 | int increment = 3; | |
110 | StateSystemBuilderUtils.incrementAttributeInt(ss, START_TIME + TIME_INCREMENT, quark, increment); | |
111 | value = ss.queryOngoingState(quark); | |
112 | assertEquals(TmfStateValue.newValueInt(increment), value); | |
113 | ||
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); | |
119 | ||
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); | |
125 | ||
126 | } catch (AttributeNotFoundException e) { | |
127 | fail(e.getMessage()); | |
128 | } | |
129 | } | |
130 | } |