ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / StructDefinitionTest.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.types;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17
18 import java.nio.ByteBuffer;
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.ArrayDefinition;
23 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
24 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
28 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
29 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
30 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
31 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
32 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
33 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
34 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
35 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
36 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
37 import org.junit.Before;
38 import org.junit.Test;
39
40 /**
41 * The class <code>StructDefinitionTest</code> contains tests for the class
42 * <code>{@link StructDefinition}</code>.
43 *
44 * @author ematkho
45 * @version $Revision: 1.0 $
46 */
47 public class StructDefinitionTest {
48
49 @NonNull private static final String TEST_STRUCT_ID = "testStruct";
50 @NonNull private static final String ENUM_2 = "y";
51 @NonNull private static final String ENUM_1 = "x";
52 @NonNull private static final String TAG_ID = "Tag";
53 @NonNull private static final String INT_ID = "_id";
54 @NonNull private static final String STRING_ID = "_args";
55 @NonNull private static final String ENUM_ID = "_enumArgs";
56 @NonNull private static final String SEQUENCE_ID = "_seq";
57 @NonNull private static final String LENGTH_SEQ = "_len";
58 @NonNull private static final String VAR_FIELD_NAME = "SomeVariant";
59
60 private StructDefinition fixture;
61 private StructDefinition emptyStruct;
62 private StructDefinition simpleStruct;
63
64 /**
65 * Perform pre-test initialization.
66 *
67 * @throws CTFReaderException
68 * won't happen
69 */
70 @Before
71 public void setUp() throws CTFReaderException {
72 StructDeclaration sDec = new StructDeclaration(12);
73 IntegerDeclaration id = IntegerDeclaration.INT_32B_DECL;
74 IntegerDeclaration lenDec = IntegerDeclaration.UINT_8_DECL;
75 StringDeclaration sd = new StringDeclaration();
76 EnumDeclaration ed = new EnumDeclaration(id);
77 SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, id);
78 VariantDeclaration varDec = new VariantDeclaration();
79 EnumDeclaration tagDec = new EnumDeclaration(id);
80 tagDec.add(0, 1, ENUM_1);
81 tagDec.add(2, 3, ENUM_2);
82 varDec.addField(ENUM_2, id);
83 varDec.addField(ENUM_1, sd);
84 varDec.setTag(TAG_ID);
85 sDec.addField(INT_ID, id);
86 sDec.addField(STRING_ID, sd);
87 sDec.addField(ENUM_ID, ed);
88 sDec.addField(TAG_ID, tagDec);
89 sDec.addField(LENGTH_SEQ, lenDec);
90 sDec.addField(SEQUENCE_ID, seqDec);
91 sDec.addField(VAR_FIELD_NAME, varDec);
92 byte bytes[] = new byte[100];
93 bytes[4] = 1;
94 bytes[8] = 2;
95 bytes[13] = 3;
96 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
97 BitBuffer bb = new BitBuffer(byteBuffer);
98 fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
99 EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb);
100 assertNotNull(eDef);
101 VariantDefinition vd = varDec.createDefinition(fixture, VAR_FIELD_NAME, bb);
102 assertNotNull(vd);
103 // Create an empty struct
104 StructDeclaration esDec = new StructDeclaration(32);
105 emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID, bb);
106
107 // Create a simple struct with two items
108 StructDeclaration ssDec = new StructDeclaration(32);
109 ssDec.addField(INT_ID, id);
110 ssDec.addField(STRING_ID, sd);
111 simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID, bb);
112 }
113
114 /**
115 * Run the StructDeclaration getDeclaration() method test.
116 */
117 @Test
118 public void testGetDeclaration() {
119 StructDeclaration result = fixture.getDeclaration();
120 assertNotNull(result);
121 }
122
123 /**
124 * Run the HashMap<String, Definition> getDefinitions() method test.
125 */
126 @Test
127 public void testGetDefinitions_1() {
128 Definition result = fixture.getDefinition("_id");
129 assertNotNull(result);
130 }
131
132 /**
133 * Run the ArrayDefinition lookupArray(String) method test.
134 */
135 @Test
136 public void testLookupArray() {
137 String name = INT_ID;
138 ArrayDefinition result = fixture.lookupArray(name);
139
140 assertNull(result);
141 }
142
143 /**
144 * Run the Definition lookupDefinition(String) method test.
145 */
146 @Test
147 public void testLookupDefinition() {
148 String lookupPath = "args";
149 Definition result = fixture.lookupDefinition(lookupPath);
150
151 assertNotNull(result);
152 }
153
154 /**
155 * Run the EnumDefinition lookupEnum(String) method test.
156 */
157 @Test
158 public void testLookupEnum() {
159 String name = ENUM_ID;
160 EnumDefinition result = fixture.lookupEnum(name);
161 assertNotNull(result);
162 }
163
164 /**
165 * Run the IntegerDefinition lookupInteger(String) method test.
166 */
167 @Test
168 public void testLookupInteger_1() {
169 String name = "_id";
170 IntegerDefinition result = fixture.lookupInteger(name);
171 assertNotNull(result);
172 }
173
174 /**
175 * Run the IntegerDefinition lookupInteger(String) method test.
176 */
177 @Test
178 public void testLookupInteger_2() {
179 String name = VAR_FIELD_NAME;
180 IntegerDefinition result = fixture.lookupInteger(name);
181 assertNull(result);
182 }
183
184 /**
185 * Run the SequenceDefinition lookupSequence(String) method test.
186 */
187 @Test
188 public void testLookupSequence() {
189 String name = SEQUENCE_ID;
190 SequenceDefinition result = fixture.lookupSequence(name);
191 assertNotNull(result);
192 }
193
194 /**
195 * Run the StringDefinition lookupString(String) method test.
196 */
197 @Test
198 public void testLookupString() {
199 String name = VAR_FIELD_NAME;
200 StringDefinition result = fixture.lookupString(name);
201
202 assertNull(result);
203 }
204
205 /**
206 * Run the StructDefinition lookupStruct(String) method test.
207 */
208 @Test
209 public void testLookupStruct() {
210 String name = VAR_FIELD_NAME;
211 StructDefinition result = fixture.lookupStruct(name);
212
213 assertNull(result);
214 }
215
216 /**
217 * Run the VariantDefinition lookupVariant(String) method test.
218 */
219 @Test
220 public void testLookupVariant() {
221 String name = VAR_FIELD_NAME;
222 VariantDefinition result = fixture.lookupVariant(name);
223
224 assertNotNull(result);
225 }
226
227 /**
228 * Run the String toString() method test.
229 */
230 @Test
231 public void testToString() {
232 String result = fixture.toString();
233 assertNotNull(result);
234
235 result = emptyStruct.toString();
236 assertEquals("{ }", result);
237
238 result = simpleStruct.toString();
239 assertEquals("{ _id = 0, _args = \"\" }", result);
240 }
241 }
This page took 0.036128 seconds and 5 git commands to generate.