tmf.core: Introduce TmfTimestamp factory methods
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / trace / CtfTmfTraceTest.java
CommitLineData
95bf10e7 1/*******************************************************************************
da707390 2 * Copyright (c) 2012, 2015 Ericsson
95bf10e7
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 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
4a1f6c41 12 * Patrick Tasse - Fix location ratio
95bf10e7
AM
13 *******************************************************************************/
14
9722e5d7 15package org.eclipse.tracecompass.tmf.ctf.core.tests.trace;
81c8e6f7
MK
16
17import static org.junit.Assert.assertEquals;
3480bf12 18import static org.junit.Assert.assertFalse;
81c8e6f7
MK
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertNull;
21import static org.junit.Assert.assertTrue;
22
409bea20
GB
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.Set;
26
81c8e6f7
MK
27import org.eclipse.core.resources.IProject;
28import org.eclipse.core.resources.IResource;
a94410d9 29import org.eclipse.core.runtime.IStatus;
c4d57ac1
AM
30import org.eclipse.jdt.annotation.NonNull;
31import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
2bdf0193 32import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
2bdf0193
AM
33import org.eclipse.tracecompass.tmf.core.signal.TmfEndSynchSignal;
34import org.eclipse.tracecompass.tmf.core.signal.TmfSignal;
35import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
36import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
37import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
38import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
39import org.eclipse.tracecompass.tmf.core.trace.TmfEventTypeCollectionHelper;
9722e5d7
AM
40import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
41import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
42import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
c4d57ac1 43import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
9722e5d7 44import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
81c8e6f7
MK
45import org.junit.After;
46import org.junit.Before;
47import org.junit.Test;
48
49/**
95bf10e7
AM
50 * The class <code>CtfTmfTraceTest</code> contains tests for the class
51 * <code>{@link CtfTmfTrace}</code>.
81c8e6f7 52 *
81c8e6f7 53 * @author ematkho
95bf10e7 54 * @version 1.0
81c8e6f7
MK
55 */
56public class CtfTmfTraceTest {
95bf10e7 57
c4d57ac1 58 private static final @NonNull CtfTestTrace testTrace = CtfTestTrace.KERNEL;
81c8e6f7 59
95bf10e7
AM
60 private CtfTmfTrace fixture;
61
95bf10e7
AM
62 /**
63 * Perform pre-test initialization.
81c8e6f7 64 */
95bf10e7 65 @Before
c4d57ac1
AM
66 public void setUp() {
67 fixture = CtfTmfTestTraceUtils.getTrace(testTrace);
95bf10e7 68 }
81c8e6f7 69
95bf10e7
AM
70 /**
71 * Perform post-test clean-up.
72 */
73 @After
74 public void tearDown() {
5dd1fa65
AM
75 if (fixture != null) {
76 fixture.dispose();
77 }
95bf10e7
AM
78 }
79
80 /**
81 * Run the CtfTmfTrace() constructor test.
82 */
83 @Test
84 public void testCtfTmfTrace() {
0ff9e595
AM
85 CtfTmfTrace result = new CtfTmfTrace();
86
87 assertNotNull(result);
88 assertEquals(1000, result.getCacheSize());
89 assertEquals(0L, result.getNbEvents());
90 assertEquals(0L, result.getStreamingInterval());
91 assertNull(result.getResource());
ea652979 92 assertNull(result.getEventType());
0ff9e595
AM
93
94 result.dispose();
81c8e6f7
MK
95 }
96
95bf10e7
AM
97 /**
98 * Test the parseEvent() method
99 */
788ddcbc 100 @Test
95bf10e7 101 public void testParseEvent() {
788ddcbc
MK
102 ITmfContext ctx = fixture.seekEvent(0);
103 fixture.getNext(ctx);
104 CtfTmfEvent event = fixture.parseEvent(ctx);
105 assertNotNull(event);
4a1f6c41 106 ctx.dispose();
788ddcbc
MK
107 }
108
81c8e6f7
MK
109 /**
110 * Run the void broadcast(TmfSignal) method test.
81c8e6f7
MK
111 */
112 @Test
95bf10e7 113 public void testBroadcast() {
81c8e6f7 114 TmfSignal signal = new TmfEndSynchSignal(1);
81c8e6f7 115 fixture.broadcast(signal);
81c8e6f7
MK
116 }
117
409bea20
GB
118 /**
119 * Run the void dispose() method test.
120 */
121 @Test
0ff9e595
AM
122 public void testDispose() {
123 CtfTmfTrace emptyFixture = new CtfTmfTrace();
124 emptyFixture.dispose();
409bea20
GB
125 }
126
81c8e6f7
MK
127 /**
128 * Run the int getCacheSize() method test.
81c8e6f7
MK
129 */
130 @Test
95bf10e7 131 public void testGetCacheSize() {
0ff9e595
AM
132 CtfTmfTrace emptyFixture = new CtfTmfTrace();
133 int result = emptyFixture.getCacheSize();
134 assertEquals(1000, result);
135
136 emptyFixture.dispose();
81c8e6f7
MK
137 }
138
139 /**
140 * Run the ITmfLocation<Comparable> getCurrentLocation() method test.
81c8e6f7
MK
141 */
142 @Test
95bf10e7 143 public void testGetCurrentLocation() {
81c8e6f7 144 CtfLocation result = (CtfLocation) fixture.getCurrentLocation();
f474d36b 145 assertNull(result);
81c8e6f7
MK
146 }
147
95bf10e7
AM
148 /**
149 * Test the seekEvent() method with a null location.
150 */
81c8e6f7 151 @Test
95bf10e7 152 public void testSeekEventLoc_null() {
81c8e6f7
MK
153 CtfLocation loc = null;
154 fixture.seekEvent(loc);
155 assertNotNull(fixture);
156 }
157
95bf10e7
AM
158 /**
159 * Test the seekEvent() method with a location from a timestamp.
160 */
81c8e6f7 161 @Test
409bea20 162 public void testSeekEventLoc_timetamp() {
b2c971ec 163 CtfLocation loc = new CtfLocation(TmfTimestamp.fromNanos(0L));
81c8e6f7
MK
164 fixture.seekEvent(loc);
165 assertNotNull(fixture);
166 }
167
81c8e6f7
MK
168 /**
169 * Run the ITmfTimestamp getEndTime() method test.
81c8e6f7
MK
170 */
171 @Test
95bf10e7 172 public void testGetEndTime() {
81c8e6f7
MK
173 ITmfTimestamp result = fixture.getEndTime();
174 assertNotNull(result);
175 }
176
177 /**
299e494e 178 * Run the String getEnvironment method test.
81c8e6f7
MK
179 */
180 @Test
95bf10e7 181 public void testGetEnvValue() {
cad06250 182 String key = "tracer_name";
234838b2 183 String result = fixture.getProperties().get(key);
409bea20 184 assertEquals("\"lttng-modules\"", result);
81c8e6f7
MK
185 }
186
187 /**
409bea20 188 * Test the {@link CtfTmfTrace#getEventType()} method.
81c8e6f7
MK
189 */
190 @Test
95bf10e7 191 public void testGetEventType() {
409bea20
GB
192 Class<?> result = fixture.getEventType();
193 assertNotNull(result);
194 assertEquals(CtfTmfEvent.class, result);
195 }
196
197 /**
198 * Run the Class<CtfTmfEvent> getContainedEventTypes() method test.
199 */
200 @Test
201 public void testGetContainedEventTypes() {
e600c338 202 Set<? extends ITmfEventType> result = fixture.getContainedEventTypes();
bfe038ff 203 assertNotNull(result);
409bea20 204 assertFalse(result.isEmpty());
81c8e6f7
MK
205 }
206
207 /**
208 * Run the double getLocationRatio(ITmfLocation<?>) method test.
81c8e6f7
MK
209 */
210 @Test
95bf10e7 211 public void testGetLocationRatio() {
4a1f6c41
PT
212 ITmfContext context = fixture.seekEvent(0);
213 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
214 fixture.getNext(context);
215 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
216 fixture.getNext(context);
217 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
218 fixture.getNext(context);
219 context.dispose();
220 double ratio1 = fixture.getLocationRatio(new CtfLocation(t1, 0));
221 assertEquals(0.0, ratio1, 0.01);
222 double ratio2 = fixture.getLocationRatio(new CtfLocation(t2, 0));
223 assertEquals((double) (t2 - t1) / (t3 - t1), ratio2, 0.01);
224 double ratio3 = fixture.getLocationRatio(new CtfLocation(t3, 0));
225 assertEquals(1.0, ratio3, 0.01);
81c8e6f7
MK
226 }
227
228 /**
229 * Run the String getName() method test.
81c8e6f7
MK
230 */
231 @Test
95bf10e7 232 public void testGetName() {
81c8e6f7 233 String result = fixture.getName();
81c8e6f7
MK
234 assertNotNull(result);
235 }
236
237 /**
2db2b43a 238 * Run the getTraceProperties() method test.
81c8e6f7
MK
239 */
240 @Test
2db2b43a 241 public void testGetTraceProperties() {
234838b2 242 int result = fixture.getProperties().size();
06a93628 243 assertEquals(10, result);
234838b2 244 assertEquals(String.valueOf(1332166405241713987L), fixture.getProperties().get("clock_offset"));
81c8e6f7
MK
245 }
246
247 /**
248 * Run the long getNbEvents() method test.
81c8e6f7
MK
249 */
250 @Test
95bf10e7 251 public void testGetNbEvents() {
81c8e6f7 252 long result = fixture.getNbEvents();
132a02b0 253 assertEquals(1L, result);
81c8e6f7
MK
254 }
255
256 /**
257 * Run the CtfTmfEvent getNext(ITmfContext) method test.
81c8e6f7
MK
258 */
259 @Test
95bf10e7 260 public void testGetNext() {
f474d36b 261 ITmfContext context = fixture.seekEvent(0);
81c8e6f7 262 CtfTmfEvent result = fixture.getNext(context);
81c8e6f7 263 assertNotNull(result);
4a1f6c41 264 context.dispose();
81c8e6f7
MK
265 }
266
267 /**
268 * Run the String getPath() method test.
81c8e6f7
MK
269 */
270 @Test
95bf10e7 271 public void testGetPath() {
81c8e6f7 272 String result = fixture.getPath();
81c8e6f7
MK
273 assertNotNull(result);
274 }
275
276 /**
277 * Run the IResource getResource() method test.
81c8e6f7
MK
278 */
279 @Test
95bf10e7 280 public void testGetResource() {
81c8e6f7 281 IResource result = fixture.getResource();
81c8e6f7
MK
282 assertNull(result);
283 }
284
285 /**
286 * Run the ITmfTimestamp getStartTime() method test.
81c8e6f7
MK
287 */
288 @Test
95bf10e7 289 public void testGetStartTime() {
81c8e6f7 290 ITmfTimestamp result = fixture.getStartTime();
81c8e6f7
MK
291 assertNotNull(result);
292 }
293
81c8e6f7
MK
294 /**
295 * Run the long getStreamingInterval() method test.
81c8e6f7
MK
296 */
297 @Test
95bf10e7 298 public void testGetStreamingInterval() {
81c8e6f7 299 long result = fixture.getStreamingInterval();
81c8e6f7
MK
300 assertEquals(0L, result);
301 }
302
303 /**
304 * Run the TmfTimeRange getTimeRange() method test.
81c8e6f7
MK
305 */
306 @Test
95bf10e7 307 public void testGetTimeRange() {
81c8e6f7 308 TmfTimeRange result = fixture.getTimeRange();
81c8e6f7
MK
309 assertNotNull(result);
310 }
311
81c8e6f7
MK
312 /**
313 * Run the CtfTmfEvent readNextEvent(ITmfContext) method test.
81c8e6f7
MK
314 */
315 @Test
95bf10e7 316 public void testReadNextEvent() {
f474d36b 317 ITmfContext context = fixture.seekEvent(0);
c32744d6 318 CtfTmfEvent result = fixture.getNext(context);
81c8e6f7 319 assertNotNull(result);
4a1f6c41 320 context.dispose();
81c8e6f7
MK
321 }
322
323 /**
324 * Run the ITmfContext seekEvent(double) method test.
81c8e6f7
MK
325 */
326 @Test
95bf10e7 327 public void testSeekEvent_ratio() {
4a1f6c41
PT
328 ITmfContext context = fixture.seekEvent(0);
329 long t1 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
330 fixture.getNext(context);
331 long t2 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
332 fixture.getNext(context);
333 long t3 = ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp();
334 fixture.getNext(context);
335 context.dispose();
336 context = fixture.seekEvent(0.0);
337 assertEquals(t1, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
338 context.dispose();
339 context = fixture.seekEvent(0.5);
340 assertEquals(t2, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
341 context.dispose();
342 context = fixture.seekEvent(1.0);
343 assertEquals(t3, ((CtfLocationInfo) context.getLocation().getLocationInfo()).getTimestamp());
344 context.dispose();
81c8e6f7
MK
345 }
346
347 /**
348 * Run the ITmfContext seekEvent(long) method test.
81c8e6f7
MK
349 */
350 @Test
95bf10e7 351 public void testSeekEvent_rank() {
81c8e6f7 352 long rank = 1L;
81c8e6f7 353 ITmfContext result = fixture.seekEvent(rank);
81c8e6f7 354 assertNotNull(result);
4a1f6c41 355 result.dispose();
81c8e6f7
MK
356 }
357
358 /**
359 * Run the ITmfContext seekEvent(ITmfTimestamp) method test.
81c8e6f7
MK
360 */
361 @Test
95bf10e7 362 public void testSeekEvent_timestamp() {
b2c971ec 363 ITmfTimestamp timestamp = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE);
81c8e6f7 364 ITmfContext result = fixture.seekEvent(timestamp);
81c8e6f7 365 assertNotNull(result);
4a1f6c41 366 result.dispose();
81c8e6f7
MK
367 }
368
81c8e6f7 369 /**
95bf10e7 370 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test.
81c8e6f7
MK
371 */
372 @Test
95bf10e7 373 public void testSeekEvent_location() {
f5df94f8 374 final CtfLocationInfo location2 = new CtfLocationInfo(1L, 0L);
132a02b0 375 CtfLocation ctfLocation = new CtfLocation(location2);
95bf10e7
AM
376 ITmfContext result = fixture.seekEvent(ctfLocation);
377 assertNotNull(result);
4a1f6c41 378 result.dispose();
81c8e6f7
MK
379 }
380
21924e03
MAL
381 /**
382 * Run the ITmfContext seekEvent(ITmfLocation<?>) method test with invalid location.
383 */
384 @Test
385 public void testSeekEventInvalidLocation() {
386 CtfLocation ctfLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
387 ITmfContext result = fixture.seekEvent(ctfLocation);
388 assertNull(fixture.getNext(result));
389 assertEquals(CtfLocation.INVALID_LOCATION, result.getLocation().getLocationInfo());
390 result.dispose();
391
392 // Not using CtfLocation.INVALID_LOCATION directly on purpose, to make sure CtfLocationInfo.equals is properly used
393 CtfLocationInfo invalidLocation = new CtfLocationInfo(CtfLocation.INVALID_LOCATION.getTimestamp(), CtfLocation.INVALID_LOCATION.getIndex());
394 ctfLocation = new CtfLocation(invalidLocation);
395 result = fixture.seekEvent(ctfLocation);
396 assertNull(fixture.getNext(result));
397 assertEquals(CtfLocation.INVALID_LOCATION, result.getLocation().getLocationInfo());
398 result.dispose();
399 }
400
81c8e6f7
MK
401 /**
402 * Run the boolean validate(IProject,String) method test.
81c8e6f7
MK
403 */
404 @Test
95bf10e7 405 public void testValidate() {
81c8e6f7 406 IProject project = null;
c4d57ac1 407 IStatus result = fixture.validate(project, fixture.getPath());
a94410d9 408 assertTrue(result.isOK());
81c8e6f7 409 }
3480bf12
GB
410
411 /**
412 * Run the boolean hasEvent(final String) method test
413 */
414 @Test
415 public void testEventLookup() {
aa353506 416 Set<@NonNull ? extends ITmfEventType> eventTypes = fixture.getContainedEventTypes();
409bea20
GB
417 Set<String> eventNames = TmfEventTypeCollectionHelper.getEventNames(eventTypes);
418 assertTrue(eventNames.contains("sched_switch"));
419 assertFalse(eventNames.contains("Sched_switch"));
3480bf12 420 String[] events = { "sched_switch", "sched_wakeup", "timer_init" };
409bea20
GB
421 assertTrue(eventNames.containsAll(Arrays.asList(events)));
422 Set<String> copy = new HashSet<>(eventNames);
423 copy.retainAll(Arrays.asList(events));
424 assertFalse(copy.isEmpty());
3480bf12 425 String[] names = { "inexistent", "sched_switch", "SomeThing" };
409bea20
GB
426 copy = new HashSet<>(eventNames);
427 copy.retainAll(Arrays.asList(names));
428 assertTrue(!copy.isEmpty());
429 assertFalse(eventNames.containsAll(Arrays.asList(names)));
3480bf12 430 }
bb52f9bc
GB
431
432 /**
433 * Run the String getHostId() method test
434 */
435 @Test
436 public void testCtfHostId() {
437 String a = fixture.getHostId();
438 assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a);
439 }
440
2d223a34 441}
This page took 0.101606 seconds and 5 git commands to generate.