fd33e77c6047e6c571e977c0e345df9cfbdc94ab
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFStreamInputPacketIndexEntryTest.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.trace;
13
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;
19
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22
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;
38
39 /**
40 * The class <code>StreamInputPacketIndexEntryTest</code> contains tests for the
41 * class <code>{@link StreamInputPacketIndexEntry}</code>.
42 *
43 * @author ematkho
44 * @version $Revision: 1.0 $
45 */
46 public class CTFStreamInputPacketIndexEntryTest {
47
48 private ICTFPacketDescriptor fixture;
49
50 /**
51 * Perform pre-test initialization.
52 */
53 @Before
54 public void setUp() {
55 fixture = new StreamInputPacketIndexEntry(1L, 1L);
56 }
57
58 /**
59 * Run the StreamInputPacketIndexEntry(long) constructor test.
60 */
61 @Test
62 public void testStreamInputPacketIndexEntry_1() {
63 String expectedResult = "StreamInputPacketIndexEntry [offsetBits=1, " +
64 "timestampBegin=" + 0 +
65 ", timestampEnd=" + Long.MAX_VALUE +
66 "]";
67
68 assertNotNull(fixture);
69 assertEquals(expectedResult, fixture.toString());
70 assertEquals(1, fixture.getOffsetBits());
71 assertEquals(0, fixture.getOffsetBytes());
72 }
73
74 /**
75 * Test the constructor
76 *
77 * @throws CTFException
78 * exception
79 */
80 @Test
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());
100 }
101
102 /**
103 * Test the constructor
104 *
105 * @throws CTFException
106 * exception
107 */
108 @Test
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());
128 }
129
130 /**
131 * Test the constructor
132 *
133 * @throws CTFException
134 * exception
135 */
136 @Test
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));
160 }
161
162 /**
163 * Test the constructor
164 *
165 * @throws CTFException
166 * exception
167 */
168 @Test
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());
184 }
185
186 /**
187 * Test the constructor
188 *
189 * @throws CTFException
190 * exception
191 */
192 @Test
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());
211 }
212
213 /**
214 * Test the constructor
215 *
216 * @throws CTFException
217 * exception
218 */
219 @Test
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());
240 }
241
242 /**
243 * Test the constructor
244 *
245 * @throws CTFException
246 * exception
247 */
248 @Test
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());
266 }
267
268 /**
269 * Test the constructor
270 *
271 * @throws CTFException
272 * exception
273 */
274 @Test
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());
291 }
292
293 /**
294 * Run the String toString() method test.
295 *
296 * @throws CTFException
297 * won't happen
298 */
299 @Test
300 public void testToString() throws CTFException {
301
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));
307
308 StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
309 assertEquals(expectedResult, new StreamInputPacketIndexEntry(0, sdef, 10000, 0).toString());
310 }
311 }
This page took 0.046534 seconds and 4 git commands to generate.