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 sd
.addField("Enum", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
88 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
89 bb
.getByteBuffer().putInt(100);
90 bb
.getByteBuffer().putInt(200);
91 bb
.getByteBuffer().putFloat((float) .75);
92 bb
.getByteBuffer().put(("Test").getBytes());
93 bb
.getByteBuffer().put((byte) 0);
94 bb
.getByteBuffer().put((byte) 0);
95 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
96 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
97 assertNull(sipie
.getTarget());
98 assertEquals(100, sipie
.getTimestampBegin());
99 assertEquals(200, sipie
.getTimestampEnd());
103 * Test the constructor
105 * @throws CTFException
109 public void testStreamInputPacketIndexEntryConstructor2() throws CTFException
{
110 StructDeclaration sd
= new StructDeclaration(8);
111 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
112 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
113 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
114 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
115 sd
.addField("Enum", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
116 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
117 bb
.getByteBuffer().putInt(100);
118 bb
.getByteBuffer().putInt(200);
119 bb
.getByteBuffer().putInt(128);
120 bb
.getByteBuffer().put(("Test").getBytes());
121 bb
.getByteBuffer().put((byte) 0);
122 bb
.getByteBuffer().put((byte) 0);
123 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
124 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
125 assertNull(sipie
.getTarget());
126 assertEquals(100, sipie
.getTimestampBegin());
127 assertEquals(200, sipie
.getTimestampEnd());
131 * Test the constructor
133 * @throws CTFException
137 public void testStreamInputPacketIndexEntryConstructor3() throws CTFException
{
138 StructDeclaration sd
= new StructDeclaration(8);
139 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
140 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
141 sd
.addField("packet_size", IntegerDeclaration
.INT_32B_DECL
);
142 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
143 sd
.addField("Enum", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
144 sd
.addField("intruder", new StructDeclaration(8));
145 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
146 bb
.getByteBuffer().putInt(100);
147 bb
.getByteBuffer().putInt(200);
148 bb
.getByteBuffer().putInt(128);
149 bb
.getByteBuffer().put(("Test").getBytes());
150 bb
.getByteBuffer().put((byte) 0);
151 bb
.getByteBuffer().put((byte) 0);
152 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
153 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
154 assertNull(sipie
.getTarget());
155 assertEquals(100, sipie
.getTimestampBegin());
156 assertEquals(200, sipie
.getTimestampEnd());
157 assertTrue(sipie
.includes(150));
158 assertFalse(sipie
.includes(10));
159 assertFalse(sipie
.includes(250));
163 * Test the constructor
165 * @throws CTFException
169 public void testStreamInputPacketIndexEntryConstructor4() throws CTFException
{
170 StructDeclaration sd
= new StructDeclaration(8);
171 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
172 sd
.addField("target", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
173 sd
.addField("Enum", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
174 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
175 bb
.getByteBuffer().putInt(0);
176 bb
.getByteBuffer().put(("Test").getBytes());
177 bb
.getByteBuffer().put((byte) 0);
178 bb
.getByteBuffer().put((byte) 0);
179 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
180 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
181 assertNull(sipie
.getTarget());
182 assertEquals(0, sipie
.getTimestampBegin());
183 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
187 * Test the constructor
189 * @throws CTFException
193 public void testStreamInputPacketIndexEntryConstructor5() throws CTFException
{
194 StructDeclaration sd
= new StructDeclaration(8);
195 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
196 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
197 sd
.addField("device", StringDeclaration
.getStringDeclaration(Encoding
.ASCII
));
198 sd
.addField("Enum", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
199 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
200 bb
.getByteBuffer().putInt(-1);
201 bb
.getByteBuffer().putInt(0);
202 bb
.getByteBuffer().put(("Test66").getBytes());
203 bb
.getByteBuffer().put((byte) 0);
204 bb
.getByteBuffer().put((byte) 0);
205 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
206 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
207 assertEquals(0, sipie
.getTimestampBegin());
208 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
209 assertEquals("Test66", sipie
.getTarget());
210 assertEquals(66, sipie
.getTargetId());
214 * Test the constructor
216 * @throws CTFException
220 public void testStreamInputPacketIndexEntryConstructor6() throws CTFException
{
221 StructDeclaration sd
= new StructDeclaration(8);
222 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
223 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
224 sd
.addField("cpu_id", IntegerDeclaration
.INT_32B_DECL
);
225 sd
.addField("events_discarded", IntegerDeclaration
.INT_32B_DECL
);
226 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
227 bb
.getByteBuffer().putInt(-1);
228 bb
.getByteBuffer().putInt(0);
229 bb
.getByteBuffer().putInt(66);
230 bb
.getByteBuffer().putInt(300);
231 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
232 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 100);
233 assertEquals(0, sipie
.getTimestampBegin());
234 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
235 assertEquals("CPU66", sipie
.getTarget());
236 assertEquals(66, sipie
.getTargetId());
237 assertEquals(200, sipie
.getLostEvents());
238 assertEquals(0, sipie
.getOffsetBits());
239 assertEquals(1024, sipie
.getPacketSizeBits());
243 * Test the constructor
245 * @throws CTFException
249 public void testStreamInputPacketIndexEntryConstructor7() throws CTFException
{
250 StructDeclaration sd
= new StructDeclaration(8);
251 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
252 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
253 final EnumDeclaration declaration
= new EnumDeclaration(IntegerDeclaration
.createDeclaration(16, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8));
254 declaration
.add(313, 315, "CPU-PI");
255 sd
.addField("device", declaration
);
256 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
257 bb
.getByteBuffer().putInt(-1);
258 bb
.getByteBuffer().putInt(0);
259 bb
.getByteBuffer().putShort((short) 314);
260 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
261 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
262 assertEquals(0, sipie
.getTimestampBegin());
263 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
264 assertEquals("CPU-PI", sipie
.getTarget());
265 assertEquals(314, sipie
.getTargetId());
269 * Test the constructor
271 * @throws CTFException
275 public void testStreamInputPacketIndexEntryConstructor8() throws CTFException
{
276 StructDeclaration sd
= new StructDeclaration(8);
277 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
278 sd
.addField("content_size", IntegerDeclaration
.INT_32B_DECL
);
279 final IDeclaration declaration
= IntegerDeclaration
.createDeclaration(16, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
280 sd
.addField("device", declaration
);
281 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
282 bb
.getByteBuffer().putInt(-1);
283 bb
.getByteBuffer().putInt(0);
284 bb
.getByteBuffer().putShort((short) 314);
285 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
286 ICTFPacketDescriptor sipie
= new StreamInputPacketIndexEntry(0, sdef
, 128, 0);
287 assertEquals(0, sipie
.getTimestampBegin());
288 assertEquals(Long
.MAX_VALUE
, sipie
.getTimestampEnd());
289 assertEquals("314", sipie
.getTarget());
290 assertEquals(314, sipie
.getTargetId());
294 * Run the String toString() method test.
296 * @throws CTFException
300 public void testToString() throws CTFException
{
302 String expectedResult
= "StreamInputPacketIndexEntry [offsetBits=0, timestampBegin=0, timestampEnd=0]";
303 StructDeclaration sd
= new StructDeclaration(8);
304 sd
.addField("timestamp_begin", IntegerDeclaration
.INT_32B_DECL
);
305 sd
.addField("timestamp_end", IntegerDeclaration
.INT_32B_DECL
);
306 BitBuffer bb
= new BitBuffer(ByteBuffer
.allocate(128));
308 StructDefinition sdef
= sd
.createDefinition(null, ILexicalScope
.PACKET_HEADER
, bb
);
309 assertEquals(expectedResult
, new StreamInputPacketIndexEntry(0, sdef
, 10000, 0).toString());