TMF: Move the fully incremental synchronization algorithm to internal
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / synchronization / SyncTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.synchronization;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.util.Collection;
18 import java.util.LinkedList;
19
20 import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventDependency;
21 import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
22 import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm;
23 import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality;
24 import org.eclipse.linuxtools.tmf.core.synchronization.SynchronizationAlgorithmFactory;
25 import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
28 import org.eclipse.linuxtools.tmf.tests.stubs.event.TmfSyncEventStub;
29 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34 * Tests for {@link SynchronizationAlgorithm} and its descendants
35 *
36 * @author Geneviève Bastien
37 */
38 @SuppressWarnings("nls")
39 public class SyncTest {
40
41 private TmfTraceStub t1, t2;
42 private Collection<ITmfTrace> fTraces;
43
44 /**
45 * Initializing the traces
46 */
47 @Before
48 public void init() {
49 t1 = new TmfTraceStub();
50 t1.init("t1");
51 t2 = new TmfTraceStub();
52 t2.init("t2");
53
54 Collection<ITmfTrace> traces = new LinkedList<>();
55 traces.add(t1);
56 traces.add(t2);
57 fTraces = traces;
58 }
59
60 /**
61 * Testing fully incremental algorithm with communication between the two
62 * traces
63 */
64 @Test
65 public void testFullyIncremental() {
66
67 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
68
69 syncAlgo.init(fTraces);
70
71 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
72 syncAlgo.addMatch(
73 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(1)),
74 new TmfSyncEventStub(t1, new TmfTimestamp(1))
75 ));
76 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
77 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
78
79 syncAlgo.addMatch(
80 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
81 new TmfSyncEventStub(t2, new TmfTimestamp(3))
82 ));
83 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
84 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
85
86 syncAlgo.addMatch(
87 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(2)),
88 new TmfSyncEventStub(t1, new TmfTimestamp(3))
89 ));
90 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo.toString());
91 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
92
93 syncAlgo.addMatch(
94 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
95 new TmfSyncEventStub(t2, new TmfTimestamp(5))
96 ));
97 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
98 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
99
100 syncAlgo.addMatch(
101 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
102 new TmfSyncEventStub(t2, new TmfTimestamp(8))
103 ));
104 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
105 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
106
107 syncAlgo.addMatch(
108 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)),
109 new TmfSyncEventStub(t1, new TmfTimestamp(5))
110 ));
111 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
112 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
113
114 syncAlgo.addMatch(
115 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)),
116 new TmfSyncEventStub(t1, new TmfTimestamp(6))
117 ));
118 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
119 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
120
121 syncAlgo.addMatch(
122 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(6)),
123 new TmfSyncEventStub(t2, new TmfTimestamp(7))
124 ));
125 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo.toString());
126 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
127
128 ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
129 ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
130
131 assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
132 assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
133 assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
134
135 /* Make the two hulls intersect */
136 syncAlgo.addMatch(
137 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(7)),
138 new TmfSyncEventStub(t2, new TmfTimestamp(4))
139 ));
140 syncAlgo.addMatch(
141 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)),
142 new TmfSyncEventStub(t1, new TmfTimestamp(3))
143 ));
144 assertEquals(SyncQuality.FAIL, syncAlgo.getSynchronizationQuality(t1, t2));
145 }
146
147 /**
148 * Testing the fully incremental synchronization algorithm when
149 * communication goes in only one direction
150 */
151 @Test
152 public void testOneHull() {
153
154 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
155
156 syncAlgo.init(fTraces);
157
158 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
159
160 syncAlgo.addMatch(
161 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
162 new TmfSyncEventStub(t2, new TmfTimestamp(3)))
163 );
164 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
165
166 syncAlgo.addMatch(
167 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)),
168 new TmfSyncEventStub(t2, new TmfTimestamp(5)))
169 );
170
171 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
172
173 syncAlgo.addMatch(
174 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
175 new TmfSyncEventStub(t2, new TmfTimestamp(5)))
176 );
177 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
178
179 syncAlgo.addMatch(
180 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
181 new TmfSyncEventStub(t2, new TmfTimestamp(7)))
182 );
183 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
184 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
185
186 }
187
188 /**
189 * Testing the fully incremental synchronization algorithm when all
190 * communication from trace1 to trace2 happens before all communication from
191 * trace2 to trace1
192 */
193 @Test
194 public void testDisjoint() {
195
196 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
197
198 syncAlgo.init(fTraces);
199
200 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
201
202 syncAlgo.addMatch(
203 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
204 new TmfSyncEventStub(t2, new TmfTimestamp(3)))
205 );
206 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
207
208 syncAlgo.addMatch(
209 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)),
210 new TmfSyncEventStub(t2, new TmfTimestamp(5)))
211 );
212
213 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
214
215 syncAlgo.addMatch(
216 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
217 new TmfSyncEventStub(t2, new TmfTimestamp(5)))
218 );
219 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
220
221 syncAlgo.addMatch(
222 new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
223 new TmfSyncEventStub(t2, new TmfTimestamp(7)))
224 );
225 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
226 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
227
228 syncAlgo.addMatch(
229 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)),
230 new TmfSyncEventStub(t1, new TmfTimestamp(6)))
231 );
232 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
233
234 syncAlgo.addMatch(
235 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(8)),
236 new TmfSyncEventStub(t1, new TmfTimestamp(6)))
237 );
238 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
239
240 syncAlgo.addMatch(
241 new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(10)),
242 new TmfSyncEventStub(t1, new TmfTimestamp(8)))
243 );
244 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
245 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo.toString());
246 }
247 }
This page took 0.038749 seconds and 6 git commands to generate.