1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
.event
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
17 import java
.nio
.ByteBuffer
;
18 import java
.nio
.ByteOrder
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
32 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.SequenceDeclaration
;
33 import org
.junit
.Test
;
36 * The class <code>CTFEventFieldTest</code> contains tests for the class
37 * <code>{@link CTFEventField}</code>.
40 * @version $Revision: 1.0 $
42 @SuppressWarnings("javadoc")
43 public class CTFEventFieldTest
{
46 private static final String fieldName
= "id";
49 * Run the CTFEventField parseField(Definition,String) method test.
51 * @throws CTFException
54 public void testParseField_complex() throws CTFException
{
56 IntegerDeclaration id
= IntegerDeclaration
.createDeclaration(
60 ByteOrder
.LITTLE_ENDIAN
,
64 String lengthName
= "LengthName";
65 StructDeclaration structDec
= new StructDeclaration(0);
66 structDec
.addField(lengthName
, id
);
67 StructDefinition structDef
= new StructDefinition(
72 new IntegerDefinition(
79 SequenceDeclaration sd
= new SequenceDeclaration(lengthName
, id
);
80 ByteBuffer byb
= testMemory(ByteBuffer
.allocate(1024));
81 for (int i
= 0; i
< 1024; i
++) {
84 BitBuffer bb
= new BitBuffer(byb
);
85 IDefinition fieldDef
= sd
.createDefinition(structDef
, "fff-fffield", bb
);
87 assertNotNull(fieldDef
);
91 private static ByteBuffer
testMemory(ByteBuffer buffer
) {
93 throw new IllegalStateException("Failed to allocate memory");
99 * Run the CTFEventField parseField(Definition,String) method test.
101 * @throws CTFException
104 public void testParseField_simple() throws CTFException
{
105 final StringDeclaration elemType
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
106 byte[] bytes
= { 'T', 'e', 's', 't', '\0' };
107 ByteBuffer bb
= testMemory(ByteBuffer
.wrap(bytes
));
108 IDefinition fieldDef
= elemType
.createDefinition(null, fieldName
, new BitBuffer(bb
));
110 assertNotNull(fieldDef
);
114 * Run the CTFEventField parseField(Definition,String) method test.
117 public void testParseField_simple2() {
118 IntegerDefinition fieldDef
= new IntegerDefinition(
119 IntegerDeclaration
.createDeclaration(1, false, 1, ByteOrder
.BIG_ENDIAN
,
120 Encoding
.ASCII
, "", 8), null, fieldName
, 1L);
122 assertNotNull(fieldDef
);
129 public void testParseField_simple3() {
130 StringDefinition fieldDef
= new StringDefinition(
131 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
), null, fieldName
, "Hello World");
133 String other
= "\"Hello World\"";
134 assertNotNull(fieldDef
);
135 assertEquals(fieldDef
.toString(), other
);