Commit | Line | Data |
---|---|---|
19ed6598 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ericsson | |
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.analysis.os.linux.ui.tests.view.resources; | |
11 | ||
12 | import static org.junit.Assert.assertEquals; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.Comparator; | |
16 | import java.util.Iterator; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.jdt.annotation.NonNull; | |
20 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
eeff806b | 21 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.resources.AggregateEventIterator; |
19ed6598 MK |
22 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
23 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
24 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent; | |
25 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent; | |
26 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry; | |
27 | import org.junit.Test; | |
28 | ||
29 | import com.google.common.collect.ImmutableList; | |
30 | import com.google.common.collect.Iterables; | |
31 | import com.google.common.collect.Iterators; | |
32 | ||
33 | /** | |
34 | * Test class to verify the main cases of the aggregate iterator | |
35 | * | |
36 | * @author Matthew Khouzam | |
37 | * | |
38 | */ | |
39 | @NonNullByDefault | |
40 | public class AggregateIteratorTest { | |
41 | ||
42 | /** | |
43 | * <pre> | |
44 | * ----------X--------- | |
45 | * </pre> | |
46 | */ | |
47 | private final ITimeGraphEntry t1 = new StubTimeGraphEntry(ImmutableList.of( | |
48 | new NullTimeEvent(null, 0, 10), | |
49 | new TimeEvent(null, 10, 1, 1), | |
50 | new NullTimeEvent(null, 11, 9))); | |
51 | /** | |
52 | * <pre> | |
53 | * -----------X-------- | |
54 | * </pre> | |
55 | */ | |
56 | private final ITimeGraphEntry t2 = new StubTimeGraphEntry(ImmutableList.of( | |
57 | new NullTimeEvent(null, 0, 12), | |
58 | new TimeEvent(null, 12, 1, 1), | |
59 | new NullTimeEvent(null, 13, 7))); | |
60 | /** | |
61 | * <pre> | |
62 | * --------X----------- | |
63 | * </pre> | |
64 | */ | |
65 | private final ITimeGraphEntry t3 = new StubTimeGraphEntry(ImmutableList.of( | |
66 | new NullTimeEvent(null, 0, 9), | |
67 | new TimeEvent(null, 9, 1, 1), | |
68 | new NullTimeEvent(null, 10, 10))); | |
69 | ||
70 | /** | |
71 | * <pre> | |
72 | * --------XXX--------- | |
73 | * </pre> | |
74 | */ | |
75 | private final ITimeGraphEntry t4 = new StubTimeGraphEntry(ImmutableList.of( | |
76 | new NullTimeEvent(null, 0, 9), | |
77 | new TimeEvent(null, 9, 3, 1), | |
78 | new NullTimeEvent(null, 12, 8))); | |
79 | ||
80 | /** | |
81 | * <pre> | |
82 | * ----------XXX------- | |
83 | * </pre> | |
84 | */ | |
85 | private final ITimeGraphEntry t5 = new StubTimeGraphEntry(ImmutableList.of( | |
86 | new NullTimeEvent(null, 0, 10), | |
87 | new TimeEvent(null, 10, 3, 1), | |
88 | new NullTimeEvent(null, 13, 7))); | |
89 | ||
90 | /** | |
91 | * <pre> | |
92 | * ----------XXX------- | |
93 | * </pre> | |
94 | */ | |
95 | private final ITimeGraphEntry t6 = new StubTimeGraphEntry(ImmutableList.of( | |
96 | new NullTimeEvent(null, 0, 10), | |
97 | new TimeEvent(null, 10, 3, 1), | |
98 | new NullTimeEvent(null, 13, 7))); | |
99 | ||
100 | /** | |
101 | * <pre> | |
102 | * XXXXXXXXXXXXXXXXXXXX | |
103 | * </pre> | |
104 | */ | |
105 | private final ITimeGraphEntry t7 = new StubTimeGraphEntry(ImmutableList.of( | |
106 | new TimeEvent(null, 0, 20, 1))); | |
107 | ||
108 | /** | |
109 | * Test non-overlapping intervals | |
110 | */ | |
111 | @Test | |
112 | public void testNoOverlap() { | |
113 | List<ITimeEvent> expected = ImmutableList.of(new NullTimeEvent(null, 0, 9), new TimeEvent(null, 9, 1, 1), new TimeEvent(null, 10, 1, 1), new NullTimeEvent(null, 11, 1), new TimeEvent(null, 12, 1, 1), new NullTimeEvent(null, 13, 7)); | |
114 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t1, t2, t3), COMPARATOR); | |
115 | runTest(expected, fixture); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Test intervals with 2 events that partially overlap | |
120 | */ | |
121 | @Test | |
122 | public void testPartialOverlap() { | |
123 | List<ITimeEvent> expected = ImmutableList.of(new NullTimeEvent(null, 0, 9), new TimeEvent(null, 9, 1, 1), new TimeEvent(null, 10, 2, 1), new TimeEvent(null, 12, 1, 1), new NullTimeEvent(null, 13, 7)); | |
124 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t4, t5), COMPARATOR); | |
125 | runTest(expected, fixture); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test two iterators that are identical, it will give the same result | |
130 | */ | |
131 | @Test | |
132 | public void testFullOverlap() { | |
133 | List<ITimeEvent> expected = ImmutableList.of(new NullTimeEvent(null, 0, 10), new TimeEvent(null, 10, 3, 1), new NullTimeEvent(null, 13, 7)); | |
134 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t6, t5), COMPARATOR); | |
135 | runTest(expected, fixture); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Test two iterators that are identical, it will give the same result | |
140 | */ | |
141 | @Test | |
142 | public void testSameStartOverlap() { | |
143 | List<ITimeEvent> expected = ImmutableList.of(new NullTimeEvent(null, 0, 9), new TimeEvent(null, 9, 1, 1), new TimeEvent(null, 10, 2, 1), new NullTimeEvent(null, 12, 8)); | |
144 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t3, t4), COMPARATOR); | |
145 | runTest(expected, fixture); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Test two iterators that are identical, it will give the same result | |
150 | */ | |
151 | @Test | |
152 | public void testSameEndOverlap() { | |
153 | List<ITimeEvent> expected = ImmutableList.of(new NullTimeEvent(null, 0, 10), new TimeEvent(null, 10, 2, 1), new TimeEvent(null, 12, 1, 1), new NullTimeEvent(null, 13, 7)); | |
154 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t5, t2), COMPARATOR); | |
155 | runTest(expected, fixture); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test two iterators where one only has one HUGE event | |
160 | */ | |
161 | @Test | |
162 | public void testOverlapEnglobing() { | |
163 | List<ITimeEvent> expected = ImmutableList.of(new TimeEvent(null, 0, 10, 1), new TimeEvent(null, 10, 1, 1), new TimeEvent(null, 11, 9, 1)); | |
164 | AggregateEventIterator fixture = new AggregateEventIterator(ImmutableList.of(t1, t7), COMPARATOR); | |
165 | runTest(expected, fixture); | |
166 | } | |
167 | ||
168 | private static void runTest(List<ITimeEvent> expected, AggregateEventIterator fixture) { | |
169 | List<ITimeEvent> results = new ArrayList<>(); | |
170 | Iterators.addAll(results, fixture); | |
171 | assertEquals(expected.size(), results.size()); | |
172 | for (int i = 0; i < expected.size(); i++) { | |
173 | final @NonNull TimeEvent actual = (@NonNull TimeEvent) results.get(i); | |
174 | final @NonNull TimeEvent expected2 = (@NonNull TimeEvent) expected.get(i); | |
175 | final @NonNull String name = Integer.toString(i); | |
176 | assertEquals(name, expected2.getClass(), actual.getClass()); | |
177 | assertEquals(name, expected2.getDuration(), actual.getDuration()); | |
178 | assertEquals(name, expected2.getTime(), actual.getTime()); | |
179 | assertEquals(name, expected2.getEntry(), actual.getEntry()); | |
180 | assertEquals(name, expected2.getValue(), actual.getValue()); | |
181 | } | |
182 | } | |
183 | ||
184 | static class StubTimeGraphEntry extends TimeGraphEntry { | |
185 | ||
186 | private final Iterable<@NonNull ITimeEvent> fEvents; | |
187 | ||
188 | public StubTimeGraphEntry(Iterable<ITimeEvent> events) { | |
189 | super("stub", Long.MIN_VALUE, Long.MAX_VALUE); | |
190 | fEvents = events; | |
191 | } | |
192 | ||
193 | @Override | |
194 | public boolean hasTimeEvents() { | |
195 | return !Iterables.isEmpty(fEvents); | |
196 | } | |
197 | ||
198 | // for a stub, not worth making it work. | |
199 | @SuppressWarnings("null") | |
200 | @Override | |
201 | public Iterator<@NonNull ITimeEvent> getTimeEventsIterator() { | |
202 | return fEvents.iterator(); | |
203 | } | |
204 | ||
205 | @Override | |
206 | public Iterator<@NonNull ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) { | |
207 | if (startTime != Long.MIN_VALUE || stopTime != Long.MAX_VALUE) { | |
208 | throw new IllegalArgumentException("startTime must be Long.MIN_VALUE, stopTime must be Long.MAX_VALUE"); | |
209 | } | |
210 | return getTimeEventsIterator(); | |
211 | } | |
212 | ||
213 | } | |
214 | ||
215 | /** | |
216 | * Same as in AggregateResourcesEntry but we want a snapshot and to not | |
217 | * update the tests at every new mode of resources view | |
218 | */ | |
219 | private static final Comparator<ITimeEvent> COMPARATOR = new Comparator<ITimeEvent>() { | |
220 | @Override | |
221 | public int compare(ITimeEvent o1, ITimeEvent o2) { | |
222 | // largest value | |
223 | return Integer.compare(getValue(o2), getValue(o1)); | |
224 | } | |
225 | ||
226 | private int getValue(ITimeEvent element) { | |
227 | return (element instanceof TimeEvent) ? ((TimeEvent) element).getValue() : Integer.MIN_VALUE; | |
228 | } | |
229 | }; | |
230 | ||
231 | } |