tmf: Add a few tests to TmfTraceUtilsSearchingTest
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / trace / TmfTraceUtilsSearchingTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
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.tmf.ctf.core.tests.trace;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertNotEquals;
16
17 import java.util.function.Predicate;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
23 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
24 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
25 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 /**
31 * Tests for the event-searching methods in {@link TmfTraceUtils}.
32 *
33 * @author Alexandre Montplaisir
34 */
35 public class TmfTraceUtilsSearchingTest {
36
37 private static final @NonNull CtfTestTrace TEST_TRACE = CtfTestTrace.TRACE2;
38 private static final long START_RANK = 500L;
39
40 private ITmfTrace fTrace;
41 private ITmfEvent fStartEvent;
42
43 /**
44 * Test setup
45 */
46 @Before
47 public void setUp() {
48 fTrace = CtfTmfTestTraceUtils.getTrace(TEST_TRACE);
49
50 ITmfContext ctx = fTrace.seekEvent(START_RANK);
51 fStartEvent = fTrace.getNext(ctx);
52 assertEquals("softirq_raise", fStartEvent.getName());
53 }
54
55 /**
56 * Test cleanup
57 */
58 @After
59 public void tearDown() {
60 if (fTrace != null) {
61 fTrace.dispose();
62 }
63 }
64
65 /**
66 * Test the {@link TmfTraceUtils#getNextEventMatching} method.
67 */
68 @Test
69 public void testNextMatchingEvent() {
70 ITmfTrace trace = fTrace;
71 assertNotNull(trace);
72
73 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sched_switch");
74
75 ITmfEvent actualEvent = TmfTraceUtils.getNextEventMatching(trace, START_RANK, predicate);
76
77 ITmfContext ctx = trace.seekEvent(508L); // following sched_switch event
78 ITmfEvent expectedEvent = trace.getNext(ctx);
79
80 assertEquals(expectedEvent, actualEvent);
81 }
82
83 /**
84 * Test the {@link TmfTraceUtils#getNextEventMatching} method where no event
85 * matches the passed predicate. It should return null.
86 */
87 @Test
88 public void testNextMatchingEventNoMatch() {
89 ITmfTrace trace = fTrace;
90 assertNotNull(trace);
91
92 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("non-existent-event");
93
94 ITmfEvent actualEvent = TmfTraceUtils.getNextEventMatching(trace, START_RANK, predicate);
95 assertNull(actualEvent);
96 }
97
98 /**
99 * Test the {@link TmfTraceUtils#getNextEventMatching} method, where the
100 * event from which we start the search already matches the criterion (it
101 * should be correctly skipped so we can advance).
102 */
103 @Test
104 public void testNextMatchingEventStartMatches() {
105 ITmfTrace trace = fTrace;
106 assertNotNull(trace);
107
108 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("softirq_raise");
109 ITmfEvent foundEvent = TmfTraceUtils.getNextEventMatching(trace, START_RANK, predicate);
110
111 assertNotNull(foundEvent);
112 assertNotEquals(fStartEvent, foundEvent);
113 }
114
115 /**
116 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method.
117 */
118 @Test
119 public void testPreviousMatchingEvent() {
120 ITmfTrace trace = fTrace;
121 assertNotNull(trace);
122
123 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sched_switch");
124
125 ITmfEvent actualEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
126
127 ITmfContext ctx = trace.seekEvent(455L); // previous sched_switch event
128 ITmfEvent expectedEvent = trace.getNext(ctx);
129
130 assertEquals(expectedEvent, actualEvent);
131 }
132
133 /**
134 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method where no event
135 * matches the passed predicate. It should return null.
136 */
137 @Test
138 public void testPreviousMatchingEventNoMatch() {
139 ITmfTrace trace = fTrace;
140 assertNotNull(trace);
141
142 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("non-existent-event");
143
144 ITmfEvent actualEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
145 assertNull(actualEvent);
146 }
147
148 /**
149 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method, where the
150 * event from which we start the search already matches the criterion (it
151 * should be correctly skipped).
152 */
153 @Test
154 public void testPreviousMatchingEventStartMatches() {
155 ITmfTrace trace = fTrace;
156 assertNotNull(trace);
157
158 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("softirq_raise");
159 ITmfEvent foundEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
160
161 assertNotNull(foundEvent);
162 assertNotEquals(fStartEvent, foundEvent);
163 }
164
165 /**
166 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method with an
167 * event that is expected to take more than one inner request.
168 */
169 @Test
170 public void testPreviousMatchingEventFar() {
171 ITmfTrace trace = fTrace;
172 assertNotNull(trace);
173
174 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sys_write");
175 ITmfEvent actualEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
176
177 ITmfContext ctx = trace.seekEvent(387L); // previous sched_switch event
178 ITmfEvent expectedEvent = trace.getNext(ctx);
179
180 assertEquals(expectedEvent, actualEvent);
181 }
182
183 /**
184 * When doing a backwards search near the beginning of the trace (when
185 * startRank < step), make sure that we do not go beyond the start rank.
186 */
187 @Test
188 public void testPreviousMatchingBeginningOfTrace() {
189 ITmfTrace trace = fTrace;
190 assertNotNull(trace);
191
192 final int startRank = 3;
193 ITmfContext ctx = fTrace.seekEvent(startRank);
194 ITmfEvent startEvent = fTrace.getNext(ctx);
195 assertEquals("exit_syscall", startEvent.getName());
196
197 /* There is a sys_mmap at rank 6, it should not be matched! */
198 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sys_mmap");
199 ITmfEvent foundEvent = TmfTraceUtils.getPreviousEventMatching(trace, startRank, predicate);
200 assertNull(foundEvent);
201
202 /* Do not match the event itself either, or any later "exit_syscall" */
203 predicate = event -> event.getName().equals("exit_syscall");
204 foundEvent = TmfTraceUtils.getPreviousEventMatching(trace, startRank, predicate);
205 assertNull(foundEvent);
206 }
207 }
This page took 0.038811 seconds and 5 git commands to generate.