LTTNG: Bug 436857: Keep process selection in CPU Usage tree viewer
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / VariantDefinitionTest.java
CommitLineData
4bd7f2db 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
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
866e5b51
FC
12package org.eclipse.linuxtools.ctf.core.tests.types;
13
419f09a8 14import static org.junit.Assert.assertEquals;
866e5b51
FC
15import static org.junit.Assert.assertNotNull;
16import static org.junit.Assert.assertNull;
17
a4fa4e36 18import java.nio.ByteBuffer;
866e5b51 19import java.nio.ByteOrder;
866e5b51 20
a4fa4e36
MK
21import org.eclipse.jdt.annotation.NonNull;
22import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
23import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
7b4f13e6 24import org.eclipse.linuxtools.ctf.core.event.types.AbstractArrayDefinition;
866e5b51
FC
25import org.eclipse.linuxtools.ctf.core.event.types.Definition;
26import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
27import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
28import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
024373d7 29import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
866e5b51
FC
30import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
31import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
024373d7 32import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
866e5b51
FC
33import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
34import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
35import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
36import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
37import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
a4fa4e36 38import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
7b4f13e6 39import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
866e5b51
FC
40import org.junit.Before;
41import org.junit.Test;
42
a4fa4e36
MK
43import com.google.common.collect.ImmutableList;
44
866e5b51
FC
45/**
46 * The class <code>VariantDefinitionTest</code> contains tests for the class
47 * <code>{@link VariantDefinition}</code>.
284fdee8 48 *
866e5b51
FC
49 * @author ematkho
50 * @version $Revision: 1.0 $
51 */
52public class VariantDefinitionTest {
53
54 private VariantDefinition fixture;
55
a4fa4e36 56 StructDefinition fStructDefinition;
163354ef
MK
57 private static final @NonNull String TEST_STRUCT_ID = "testStruct";
58 private static final @NonNull String ENUM_7 = "g";
59 private static final @NonNull String ENUM_6 = "f";
60 private static final @NonNull String ENUM_5 = "e";
61 private static final @NonNull String ENUM_4 = "d";
62 private static final @NonNull String ENUM_3 = "c";
63 private static final @NonNull String ENUM_2 = "b";
64 private static final @NonNull String ENUM_1 = "a";
65 private static final @NonNull String TAG_ID = "a";
66 private static final @NonNull String LENGTH_SEQ = "_len";
67 private static final @NonNull String VAR_FIELD_NAME = "var";
68 private static final @NonNull String ENUM_8 = "bbq ribs";
a34d8eee 69
866e5b51
FC
70 /**
71 * Perform pre-test initialization.
284fdee8 72 *
866e5b51 73 * Not sure it needs to be that complicated, oh well...
a4fa4e36
MK
74 *
75 * @throws CTFReaderException
76 * won't happen
866e5b51
FC
77 */
78 @Before
a4fa4e36 79 public void setUp() throws CTFReaderException {
024373d7
MK
80 StructDeclaration sDec = new StructDeclaration(12);
81 StructDeclaration smallStruct = new StructDeclaration(8);
a4fa4e36
MK
82 IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
83 IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
024373d7
MK
84 StringDeclaration strDec = new StringDeclaration();
85 EnumDeclaration enDec = new EnumDeclaration(iDec);
024373d7
MK
86 VariantDeclaration varDec = new VariantDeclaration();
87 EnumDeclaration tagDec = new EnumDeclaration(iDec);
88 ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec);
07002e0a 89 FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
024373d7
MK
90 tagDec.add(0, 1, ENUM_1);
91 tagDec.add(2, 3, ENUM_2);
92 tagDec.add(4, 5, ENUM_3);
024373d7
MK
93 tagDec.add(8, 9, ENUM_5);
94 tagDec.add(10, 11, ENUM_6);
95 tagDec.add(12, 13, ENUM_7);
96 varDec.addField(ENUM_4, lenDec);
97 varDec.addField(ENUM_7, fDec);
98 varDec.addField(ENUM_6, smallStruct);
99 varDec.addField(ENUM_5, enDec);
024373d7
MK
100 varDec.addField(ENUM_3, arrDec);
101 varDec.addField(ENUM_2, iDec);
102 varDec.addField(ENUM_1, strDec);
103
104 sDec.addField(TAG_ID, tagDec);
105 sDec.addField(LENGTH_SEQ, lenDec);
024373d7
MK
106
107 sDec.addField(VAR_FIELD_NAME, varDec);
108 varDec.setTag(TAG_ID);
109
a4fa4e36
MK
110 ByteBuffer byteBuffer = ByteBuffer.allocate(100);
111 BitBuffer bb = new BitBuffer(byteBuffer);
112 byteBuffer.mark();
113 byteBuffer.putInt(1);
114 byteBuffer.putInt(2);
115 byteBuffer.putInt(3);
116 byteBuffer.reset();
117 fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
118 fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
866e5b51
FC
119 }
120
866e5b51
FC
121 /**
122 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
a4fa4e36
MK
123 *
124 * @throws CTFReaderException
125 * should not happen
866e5b51
FC
126 */
127 @Test
a4fa4e36 128 public void testVariantDefinition() throws CTFReaderException {
866e5b51 129 VariantDeclaration declaration = new VariantDeclaration();
4a9c1f07 130 declaration.setTag("");
866e5b51 131 VariantDeclaration variantDeclaration = new VariantDeclaration();
a4fa4e36
MK
132 variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
133 variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
134 declaration.addField(ENUM_3, new StringDeclaration());
135 variantDeclaration.setTag("a");
136
137 byte[] bytes = new byte[128];
138 ByteBuffer byb = ByteBuffer.wrap(bytes);
139 byb.mark();
140 byb.putInt(0);
141 byb.putShort((short) 2);
142 byb.put(new String("hello").getBytes());
143 byb.reset();
144 BitBuffer bb = new BitBuffer(byb);
145 VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
146 EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
147 declaration2.add(0, 2, ENUM_3);
148 EnumDefinition enumDefinition = new EnumDefinition(
149 declaration2,
150 null,
151 "a",
152 new IntegerDefinition(
153 IntegerDeclaration.INT_8_DECL,
154 null,
155 "A",
156 1
157 ));
866e5b51 158 IDefinitionScope definitionScope = new StructDefinition(
a4fa4e36
MK
159 new StructDeclaration(1L),
160 variantDefinition,
161 "",
162 ImmutableList.<String> of("", "variant"),
163 new Definition[] { enumDefinition, variantDefinition }
164 );
4a9c1f07 165 String fieldName = "";
a4fa4e36
MK
166 declaration.setTag("");
167 VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb);
866e5b51
FC
168 assertNotNull(result);
169 }
170
171 /**
172 * Run the Definition getCurrentField() method test.
173 */
174 @Test
175 public void testGetCurrentField() {
176 Definition result = fixture.getCurrentField();
024373d7 177 assertNotNull(result);
866e5b51
FC
178 }
179
180 /**
181 * Run the String getCurrentFieldName() method test.
182 */
183 @Test
184 public void testGetCurrentFieldName() {
185 String result = fixture.getCurrentFieldName();
186 assertNotNull(result);
187 }
188
189 /**
190 * Run the VariantDeclaration getDeclaration() method test.
191 */
192 @Test
193 public void testGetDeclaration() {
194 VariantDeclaration result = fixture.getDeclaration();
195 assertNotNull(result);
196 }
197
198 /**
199 * Run the HashMap<String, Definition> getDefinitions() method test.
200 */
201 @Test
202 public void testGetDefinitions() {
a4fa4e36 203 Definition result = fixture.getCurrentField();
866e5b51
FC
204 assertNotNull(result);
205 }
206
207 /**
208 * Run the String getPath() method test.
209 */
210 @Test
211 public void testGetPath() {
a4fa4e36 212 String result = fixture.getScopePath().toString();
866e5b51
FC
213 assertNotNull(result);
214 }
215
216 /**
217 * Run the ArrayDefinition lookupArray(String) method test.
218 */
219 @Test
220 public void testLookupArray() {
7b4f13e6 221 AbstractArrayDefinition result = fixture.lookupArray2(ENUM_3);
a4fa4e36 222 assertNull(result);
866e5b51
FC
223 }
224
225 /**
226 * Run the Definition lookupDefinition(String) method test.
227 */
228 @Test
229 public void testLookupDefinition() {
024373d7
MK
230 Definition result = fixture.lookupDefinition(ENUM_1);
231 assertNotNull(result);
a4fa4e36 232 assertEquals("a", ((EnumDefinition) result).getStringValue());
866e5b51
FC
233 }
234
235 /**
236 * Run the EnumDefinition lookupEnum(String) method test.
237 */
238 @Test
239 public void testLookupEnum() {
024373d7 240 EnumDefinition result = fixture.lookupEnum(ENUM_5);
a4fa4e36 241 assertNull(result);
866e5b51
FC
242 }
243
244 /**
245 * Run the IntegerDefinition lookupInteger(String) method test.
246 */
247 @Test
248 public void testLookupInteger() {
024373d7 249 IntegerDefinition result = fixture.lookupInteger(ENUM_2);
a4fa4e36 250 assertNull(result);
866e5b51
FC
251 }
252
866e5b51
FC
253 /**
254 * Run the StringDefinition lookupString(String) method test.
255 */
256 @Test
257 public void testLookupString() {
024373d7 258 StringDefinition result = fixture.lookupString(ENUM_1);
a4fa4e36 259 assertNull(result);
866e5b51
FC
260 }
261
262 /**
263 * Run the StructDefinition lookupStruct(String) method test.
264 */
265 @Test
266 public void testLookupStruct() {
024373d7 267 StructDefinition result = fixture.lookupStruct(ENUM_6);
a4fa4e36 268 assertNull(result);
866e5b51
FC
269 }
270
271 /**
272 * Run the VariantDefinition lookupVariant(String) method test.
273 */
274 @Test
275 public void testLookupVariant() {
024373d7 276 VariantDefinition result = fixture.lookupVariant(ENUM_8);
866e5b51
FC
277 assertNull(result);
278 }
279
419f09a8
SM
280 /**
281 * Run the String toString() method test.
282 */
283 @Test
284 public void testToString() {
285 String result = fixture.toString();
a4fa4e36 286 assertEquals("{ a = \"\" }", result);
419f09a8 287 }
866e5b51 288}
This page took 0.073327 seconds and 5 git commands to generate.