Merge commit '2c789b58062b5bf3bb16ca1654ccf9cf87f3c444' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / ctfadaptor / CtfTmfEventFieldTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertArrayEquals;
18
19 import java.nio.ByteOrder;
20
21 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
22 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
23 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
24 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
25 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
26 import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
28 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
29 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
30 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
31 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
32 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
33 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
34 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventField;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 /**
39 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
40 * <code>{@link CtfTmfEventField}</code>.
41 *
42 * @author ematkho
43 * @version 1.0
44 */
45 public class CtfTmfEventFieldTest {
46
47 private static final String ROOT = "root";
48 private static final String SEQ = "seq";
49 private static final String ARRAY = "array";
50 private static final String STR = "str";
51 private static final String FLOAT = "float";
52 private static final String LEN = "len";
53 private static final String INT = "int";
54 private static final String NAME = "test";
55 private static final String STRUCT = "struct";
56 private static final String VARIANT = "variant";
57 private static final String ENUM = "enum";
58
59 private StructDefinition fixture;
60
61 /**
62 * Perform pre-test initialization.
63 */
64 @Before
65 public void setUp() {
66 StructDeclaration sDec = new StructDeclaration(1l);
67 StringDeclaration strDec = new StringDeclaration();
68 IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8,
69 ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
70 FloatDeclaration flDec = new FloatDeclaration(8, 24,
71 ByteOrder.BIG_ENDIAN, 8);
72 ArrayDeclaration arrDec = new ArrayDeclaration(2, intDec);
73 SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec);
74 StructDeclaration structDec = new StructDeclaration(32);
75 EnumDeclaration enumDec = new EnumDeclaration(intDec);
76 VariantDeclaration varDec = new VariantDeclaration();
77 sDec.addField(INT, intDec);
78 sDec.addField(LEN, intDec);
79 sDec.addField(FLOAT, flDec);
80 sDec.addField(STR, strDec);
81 sDec.addField(ARRAY, arrDec);
82 sDec.addField(SEQ, seqDec);
83 structDec.addField(STR, strDec);
84 structDec.addField(INT, intDec);
85 sDec.addField(STRUCT, structDec);
86 enumDec.add(0, 1, LEN);
87 enumDec.add(2, 3, FLOAT);
88 sDec.addField(ENUM, enumDec);
89 varDec.addField(LEN, intDec);
90 varDec.addField(FLOAT, flDec);
91 varDec.setTag(ENUM);
92 sDec.addField(VARIANT, varDec);
93 fixture = sDec.createDefinition(fixture, ROOT);
94 int capacity = 2048;
95 java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity);
96 for (int i = 0; i < capacity; i++) {
97 bb.put((byte) 2);
98 }
99 bb.position(20);
100 bb.put((byte) 0);
101 bb.position(40);
102 bb.put((byte) 0);
103 bb.position(60);
104 bb.put((byte) 0);
105 bb.position(0);
106 fixture.read(new BitBuffer(bb));
107 }
108
109 /**
110 * Run the CtfTmfEventField parseField(Definition,String) method test.
111 */
112 @Test
113 public void testParseField_float() {
114 FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT);
115 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME);
116 assertEquals("test=9.551467814359616E-38", result.toString());
117 }
118
119 /**
120 * Run the CtfTmfEventField parseField(Definition,String) method test.
121 */
122 @Test
123 public void testParseField_array() {
124 Definition fieldDef = fixture.lookupArray(ARRAY);
125 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
126 assertEquals("test=[02, 02]", result.toString());
127 }
128
129 /**
130 * Run the CtfTmfEventField parseField(Definition,String) method test.
131 */
132 @Test
133 public void testParseField_int() {
134 Definition fieldDef = fixture.lookupDefinition(INT);
135 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
136 assertEquals("test=02", result.toString());
137 }
138
139 /**
140 * Run the CtfTmfEventField parseField(Definition,String) method test.
141 */
142 @Test
143 public void testParseField_sequence() {
144 Definition fieldDef = fixture.lookupDefinition(SEQ);
145 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
146 assertEquals("test=[02, 02]", result.toString());
147 }
148
149 /**
150 * Run the CtfTmfEventField parseField(Definition,String) method test.
151 */
152 @Test
153 public void testParseField_sequence_value() {
154 Definition fieldDef = fixture.lookupDefinition(SEQ);
155 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
156 long[] values = (long[]) result.getValue();
157 long[] expected = new long[] { 2, 2 };
158 assertArrayEquals(expected, values);
159 }
160
161 /**
162 * Run the CtfTmfEventField parseField(Definition,String) method test.
163 */
164 @Test
165 public void testParseField_string() {
166 Definition fieldDef = fixture.lookupDefinition(STR);
167 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
168 assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result.toString());
169 }
170
171 /**
172 * Run the CtfTmfEventField parseField(Definition,String) method test.
173 */
174 @Test
175 public void testParseField_struct() {
176 Definition fieldDef = fixture.lookupDefinition(STRUCT);
177 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
178 assertEquals("test=[str=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2, int=02]", result.toString());
179 }
180
181 /**
182 * Run the CtfTmfEventField parseField(Definition,String) method test.
183 */
184 @Test
185 public void testParseField_enum() {
186 Definition fieldDef = fixture.lookupDefinition(ENUM);
187 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
188 assertEquals("test=float", result.toString());
189 }
190
191 /**
192 * Run the CtfTmfEventField parseField(Definition,String) method test.
193 */
194 @Test
195 public void testParseField_variant() {
196 Definition fieldDef = fixture.lookupDefinition(VARIANT);
197 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
198 assertEquals("test=float=9.551467814359616E-38", result.toString());
199 }
200 }
This page took 0.036596 seconds and 6 git commands to generate.