1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.trace
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertFalse
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertNull
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.ILexicalScope
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.ICTFPacketDescriptor
;
35 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * The class <code>StreamInputPacketIndexEntryTest</code> contains tests for the
41 * class <code>{@link StreamInputPacketIndexEntry}</code>.
44 * @version $Revision: 1.0 $
46 public class CTFStreamInputPacketIndexEntryTest
{
48 private ICTFPacketDescriptor fixture
;
51 * Perform pre-test initialization.
55 fixture
= new StreamInputPacketIndexEntry(1L, 1L);
59 * Run the StreamInputPacketIndexEntry(long) constructor test.
62 public void testStreamInputPacketIndexEntry_1() {
63 String expectedResult
= "StreamInputPacketIndexEntry [offsetBits=1, " +
64 "timestampBegin=" + 0 +
65 ", timestampEnd=" + Long
.MAX_VALUE
+
68 assertNotNull(fixture
);
69 assertEquals(expectedResult
, fixture
.toString());
70 assertEquals(1, fixture
.getOffsetBits());
71 assertEquals(0, fixture
.getOffsetBytes());
75 * Test the constructor
77 * @throws CTFException
81 public void testStreamInputPacketIndexEntryConstructor1() throws CTFException
{
82 StructDeclaration sd
= new StructDeclaration(8);
83 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
84 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
85 sd
.addField("load_factor", new FloatDeclaration(24, 8, ByteOrder
.nativeOrder(), 8));
86 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
87 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
88 declaration
.add(-100, 100, "");
89 sd
.addField("Enum", declaration
);
90 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
91 bb
.getByteBuffer().putInt(100);
92 bb
.getByteBuffer().putInt(200);
93 bb
.getByteBuffer().putFloat((float) .75);
94 bb
.getByteBuffer().put(("Test").getBytes());
95 bb
.getByteBuffer().put((byte) 0);
96 bb
.getByteBuffer().put((byte) 0);
97 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
98 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
99 assertNull(sipie
.getTarget());
100 assertEquals(100, sipie
.getTimestampBegin());
101 assertEquals(200, sipie
.getTimestampEnd());
105 * Test the constructor
107 * @throws CTFException
111 public void testStreamInputPacketIndexEntryConstructor2() throws CTFException
{
112 StructDeclaration sd
= new StructDeclaration(8);
113 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
114 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
115 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
116 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
117 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
118 declaration
.add(-100, 100, "");
119 sd
.addField("Enum", declaration
);
120 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
121 bb
.getByteBuffer().putInt(100);
122 bb
.getByteBuffer().putInt(200);
123 bb
.getByteBuffer().putInt(128);
124 bb
.getByteBuffer().put(("Test").getBytes());
125 bb
.getByteBuffer().put((byte) 0);
126 bb
.getByteBuffer().put((byte) 0);
127 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
128 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
129 assertNull(sipie
.getTarget());
130 assertEquals(100, sipie
.getTimestampBegin());
131 assertEquals(200, sipie
.getTimestampEnd());
135 * Test the constructor
137 * @throws CTFException
141 public void testStreamInputPacketIndexEntryConstructor3() throws CTFException
{
142 StructDeclaration sd
= new StructDeclaration(8);
143 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
144 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
145 sd
.addField("packet_size", IntegerDeclaration
.INT_32B_DECL
);
146 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
147 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
148 declaration
.add(-100, 100, "");
149 sd
.addField("Enum", declaration
);
150 sd
.addField("intruder", new StructDeclaration(8));
151 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
152 bb
.getByteBuffer().putInt(100);
153 bb
.getByteBuffer().putInt(200);
154 bb
.getByteBuffer().putInt(128);
155 bb
.getByteBuffer().put(("Test").getBytes());
156 bb
.getByteBuffer().put((byte) 0);
157 bb
.getByteBuffer().put((byte) 0);
158 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
159 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
160 assertNull(sipie
.getTarget());
161 assertEquals(100, sipie
.getTimestampBegin());
162 assertEquals(200, sipie
.getTimestampEnd());
163 assertTrue(sipie
.includes(150));
164 assertFalse(sipie
.includes(10));
165 assertFalse(sipie
.includes(250));
169 * Test the constructor
171 * @throws CTFException
175 public void testStreamInputPacketIndexEntryConstructor4() throws CTFException
{
176 StructDeclaration sd
= new StructDeclaration(8);
177 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
178 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
179 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
180 declaration
.add(-100, 100, "");
181 sd
.addField("Enum", declaration
);
182 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
183 bb
.getByteBuffer().putInt(0);
184 bb
.getByteBuffer().put(("Test").getBytes());
185 bb
.getByteBuffer().put((byte) 0);
186 bb
.getByteBuffer().put((byte) 0);
187 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
188 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
189 assertNull(sipie
.getTarget());
190 assertEquals(0, sipie
.getTimestampBegin());
191 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
195 * Test the constructor
197 * @throws CTFException
201 public void testStreamInputPacketIndexEntryConstructor5() throws CTFException
{
202 StructDeclaration sd
= new StructDeclaration(8);
203 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
204 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
205 sd
.addField("device", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
206 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
207 declaration
.add(-100, 100, "");
208 sd
.addField("Enum", declaration
);
209 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
210 bb
.getByteBuffer().putInt(-1);
211 bb
.getByteBuffer().putInt(0);
212 bb
.getByteBuffer().put(("Test66").getBytes());
213 bb
.getByteBuffer().put((byte) 0);
214 bb
.getByteBuffer().put((byte) 0);
215 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
216 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
217 assertEquals(0, sipie
.getTimestampBegin());
218 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
219 assertEquals("Test66", sipie
.getTarget());
220 assertEquals(66, sipie
.getTargetId());
224 * Test the constructor
226 * @throws CTFException
230 public void testStreamInputPacketIndexEntryConstructor6() throws CTFException
{
231 StructDeclaration sd
= new StructDeclaration(8);
232 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
233 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
234 sd
.addField("cpu_id", IntegerDeclaration
.INT_32B_DECL
);
235 sd
.addField("events_discarded", IntegerDeclaration
.INT_32B_DECL
);
236 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
237 bb
.getByteBuffer().putInt(-1);
238 bb
.getByteBuffer().putInt(0);
239 bb
.getByteBuffer().putInt(66);
240 bb
.getByteBuffer().putInt(300);
241 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
242 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 100);
243 assertEquals(0, sipie
.getTimestampBegin());
244 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
245 assertEquals("CPU66", sipie
.getTarget());
246 assertEquals(66, sipie
.getTargetId());
247 assertEquals(200, sipie
.getLostEvents());
248 assertEquals(0, sipie
.getOffsetBits());
249 assertEquals(1024, sipie
.getPacketSizeBits());
253 * Test the constructor
255 * @throws CTFException
259 public void testStreamInputPacketIndexEntryConstructor7() throws CTFException
{
260 StructDeclaration sd
= new StructDeclaration(8);
261 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
262 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
263 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.createDeclaration(16, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8));
264 declaration
.add(313, 315, "CPU-PI");
265 sd
.addField("device", declaration
);
266 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
267 bb
.getByteBuffer().putInt(-1);
268 bb
.getByteBuffer().putInt(0);
269 bb
.getByteBuffer().putShort((short) 314);
270 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
271 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
272 assertEquals(0, sipie
.getTimestampBegin());
273 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
274 assertEquals("CPU-PI", sipie
.getTarget());
275 assertEquals(314, sipie
.getTargetId());
279 * Test the constructor
281 * @throws CTFException
285 public void testStreamInputPacketIndexEntryConstructor8() throws CTFException
{
286 StructDeclaration sd
= new StructDeclaration(8);
287 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
288 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
289 final IDeclaration declaration
= IntegerDeclaration
.createDeclaration(16, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
290 sd
.addField("device", declaration
);
291 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
292 bb
.getByteBuffer().putInt(-1);
293 bb
.getByteBuffer().putInt(0);
294 bb
.getByteBuffer().putShort((short) 314);
295 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
296 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
297 assertEquals(0, sipie
.getTimestampBegin());
298 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
299 assertEquals("314", sipie
.getTarget());
300 assertEquals(314, sipie
.getTargetId());
304 * Run the String toString() method test.
306 * @throws CTFException
310 public void testToString() throws CTFException
{
312 String expectedResult
= "StreamInputPacketIndexEntry [offsetBits=0, timestampBegin=0, timestampEnd=0]";
313 StructDeclaration sd
= new StructDeclaration(8);
314 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
315 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
316 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
318 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
319 assertEquals(expectedResult
, new StreamInputPacketIndexEntry(0, sdef
, 10000, 0).toString());