1 /*******************************************************************************
2 * Copyright (c) 2013 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
.linuxtools
.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
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
32 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.SequenceDeclaration
;
33 import org
.junit
.Test
;
35 import com
.google
.common
.collect
.ImmutableList
;
38 * The class <code>CTFEventFieldTest</code> contains tests for the class
39 * <code>{@link CTFEventField}</code>.
42 * @version $Revision: 1.0 $
44 @SuppressWarnings("javadoc")
45 public class CTFEventFieldTest
{
48 private static final String fieldName
= "id";
51 * Run the CTFEventField parseField(Definition,String) method test.
53 * @throws CTFReaderException
56 public void testParseField_complex() throws CTFReaderException
{
58 IntegerDeclaration id
= IntegerDeclaration
.createDeclaration(
62 ByteOrder
.LITTLE_ENDIAN
,
66 String lengthName
= "LengthName";
67 StructDeclaration structDec
= new StructDeclaration(0);
68 structDec
.addField(lengthName
, id
);
69 StructDefinition structDef
= new StructDefinition(
73 ImmutableList
.of(lengthName
),
75 new IntegerDefinition(
82 SequenceDeclaration sd
= new SequenceDeclaration(lengthName
, id
);
83 ByteBuffer byb
= testMemory(ByteBuffer
.allocate(1024));
84 for (int i
= 0; i
< 1024; i
++) {
87 BitBuffer bb
= new BitBuffer(byb
);
88 IDefinition fieldDef
= sd
.createDefinition(structDef
, "fff-fffield", bb
);
90 assertNotNull(fieldDef
);
94 private static ByteBuffer
testMemory(ByteBuffer buffer
) {
96 throw new IllegalStateException("Failed to allocate memory");
102 * Run the CTFEventField parseField(Definition,String) method test.
104 * @throws CTFReaderException
107 public void testParseField_simple() throws CTFReaderException
{
108 final StringDeclaration elemType
= new StringDeclaration();
109 byte[] bytes
= { 'T', 'e', 's', 't', '\0' };
110 ByteBuffer bb
= testMemory(ByteBuffer
.wrap(bytes
));
111 IDefinition fieldDef
= elemType
.createDefinition(null, fieldName
, new BitBuffer(bb
));
113 assertNotNull(fieldDef
);
117 * Run the CTFEventField parseField(Definition,String) method test.
120 public void testParseField_simple2() {
121 IntegerDefinition fieldDef
= new IntegerDefinition(
122 IntegerDeclaration
.createDeclaration(1, false, 1, ByteOrder
.BIG_ENDIAN
,
123 Encoding
.ASCII
, "", 8), null, fieldName
, 1L);
125 assertNotNull(fieldDef
);
132 public void testParseField_simple3() {
133 StringDefinition fieldDef
= new StringDefinition(
134 new StringDeclaration(), null, fieldName
, "Hello World");
136 String other
= "\"Hello World\"";
137 assertNotNull(fieldDef
);
138 assertEquals(fieldDef
.toString(), other
);