ctf: introducing IDefinition
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / event / CTFEventFieldTest.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.event;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
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;
34
35 import com.google.common.collect.ImmutableList;
36
37 /**
38 * The class <code>CTFEventFieldTest</code> contains tests for the class
39 * <code>{@link CTFEventField}</code>.
40 *
41 * @author ematkho
42 * @version $Revision: 1.0 $
43 */
44 @SuppressWarnings("javadoc")
45 public class CTFEventFieldTest {
46
47 @NonNull
48 private static final String fieldName = "id";
49
50 /**
51 * Run the CTFEventField parseField(Definition,String) method test.
52 *
53 * @throws CTFReaderException
54 */
55 @Test
56 public void testParseField_complex() throws CTFReaderException {
57 int len = 32;
58 IntegerDeclaration id = IntegerDeclaration.createDeclaration(
59 len,
60 false,
61 len,
62 ByteOrder.LITTLE_ENDIAN,
63 Encoding.ASCII,
64 "",
65 len);
66 String lengthName = "LengthName";
67 StructDeclaration structDec = new StructDeclaration(0);
68 structDec.addField(lengthName, id);
69 StructDefinition structDef = new StructDefinition(
70 structDec,
71 null,
72 lengthName,
73 ImmutableList.of(lengthName),
74 new Definition[] {
75 new IntegerDefinition(
76 id,
77 null,
78 lengthName,
79 32)
80 });
81
82 SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
83 ByteBuffer byb = testMemory(ByteBuffer.allocate(1024));
84 for (int i = 0; i < 1024; i++) {
85 byb.put((byte) i);
86 }
87 BitBuffer bb = new BitBuffer(byb);
88 IDefinition fieldDef = sd.createDefinition(structDef, "fff-fffield", bb);
89
90 assertNotNull(fieldDef);
91 }
92
93 @NonNull
94 private static ByteBuffer testMemory(ByteBuffer buffer) {
95 if (buffer == null) {
96 throw new IllegalStateException("Failed to allocate memory");
97 }
98 return buffer;
99 }
100
101 /**
102 * Run the CTFEventField parseField(Definition,String) method test.
103 *
104 * @throws CTFReaderException
105 */
106 @Test
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));
112
113 assertNotNull(fieldDef);
114 }
115
116 /**
117 * Run the CTFEventField parseField(Definition,String) method test.
118 */
119 @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);
124
125 assertNotNull(fieldDef);
126 }
127
128 /**
129 *
130 */
131 @Test
132 public void testParseField_simple3() {
133 StringDefinition fieldDef = new StringDefinition(
134 new StringDeclaration(), null, fieldName, "Hello World");
135
136 String other = "\"Hello World\"";
137 assertNotNull(fieldDef);
138 assertEquals(fieldDef.toString(), other);
139 }
140
141 }
This page took 0.03705 seconds and 6 git commands to generate.