Commit | Line | Data |
---|---|---|
95bf10e7 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
95bf10e7 AM |
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 | ||
81c8e6f7 MK |
14 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; |
15 | ||
16 | import static org.junit.Assert.assertEquals; | |
cefe3edf | 17 | import static org.junit.Assert.assertArrayEquals; |
81c8e6f7 MK |
18 | |
19 | import java.nio.ByteOrder; | |
20 | ||
486efb2e | 21 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
81c8e6f7 MK |
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; | |
404b264a | 25 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; |
81c8e6f7 MK |
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; | |
404b264a | 33 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; |
81c8e6f7 | 34 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventField; |
81c8e6f7 MK |
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 | * | |
81c8e6f7 | 42 | * @author ematkho |
95bf10e7 | 43 | * @version 1.0 |
81c8e6f7 MK |
44 | */ |
45 | public class CtfTmfEventFieldTest { | |
46 | ||
68b18f2f AM |
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"; | |
7a6cee1a | 55 | private static final String STRUCT = "struct"; |
404b264a GB |
56 | private static final String VARIANT = "variant"; |
57 | private static final String ENUM = "enum"; | |
81c8e6f7 | 58 | |
95bf10e7 AM |
59 | private StructDefinition fixture; |
60 | ||
95bf10e7 AM |
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); | |
7a6cee1a | 74 | StructDeclaration structDec = new StructDeclaration(32); |
404b264a GB |
75 | EnumDeclaration enumDec = new EnumDeclaration(intDec); |
76 | VariantDeclaration varDec = new VariantDeclaration(); | |
95bf10e7 AM |
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); | |
459f705b | 83 | structDec.addField(STR, strDec); |
7a6cee1a GB |
84 | structDec.addField(INT, intDec); |
85 | sDec.addField(STRUCT, structDec); | |
404b264a GB |
86 | enumDec.add(0, 1, LEN); |
87 | enumDec.add(2, 3, FLOAT); | |
459f705b | 88 | sDec.addField(ENUM, enumDec); |
404b264a GB |
89 | varDec.addField(LEN, intDec); |
90 | varDec.addField(FLOAT, flDec); | |
91 | varDec.setTag(ENUM); | |
92 | sDec.addField(VARIANT, varDec); | |
95bf10e7 | 93 | fixture = sDec.createDefinition(fixture, ROOT); |
7a6cee1a | 94 | int capacity = 2048; |
95bf10e7 AM |
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); | |
7a6cee1a GB |
101 | bb.position(40); |
102 | bb.put((byte) 0); | |
404b264a GB |
103 | bb.position(60); |
104 | bb.put((byte) 0); | |
95bf10e7 AM |
105 | bb.position(0); |
106 | fixture.read(new BitBuffer(bb)); | |
107 | } | |
108 | ||
95bf10e7 AM |
109 | /** |
110 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
111 | */ |
112 | @Test | |
95bf10e7 | 113 | public void testParseField_float() { |
68b18f2f AM |
114 | FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); |
115 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME); | |
116 | assertEquals("test=9.551467814359616E-38", result.toString()); | |
81c8e6f7 MK |
117 | } |
118 | ||
119 | /** | |
120 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
121 | */ |
122 | @Test | |
95bf10e7 | 123 | public void testParseField_array() { |
68b18f2f AM |
124 | Definition fieldDef = fixture.lookupArray(ARRAY); |
125 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
404b264a | 126 | assertEquals("test=[02, 02]", result.toString()); |
81c8e6f7 MK |
127 | } |
128 | ||
129 | /** | |
130 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
131 | */ |
132 | @Test | |
95bf10e7 | 133 | public void testParseField_int() { |
81c8e6f7 MK |
134 | Definition fieldDef = fixture.lookupDefinition(INT); |
135 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
68b18f2f | 136 | assertEquals("test=02", result.toString()); |
81c8e6f7 MK |
137 | } |
138 | ||
139 | /** | |
140 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
141 | */ |
142 | @Test | |
95bf10e7 | 143 | public void testParseField_sequence() { |
81c8e6f7 MK |
144 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
145 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
404b264a | 146 | assertEquals("test=[02, 02]", result.toString()); |
a6223d74 MK |
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); | |
cefe3edf AM |
156 | long[] values = (long[]) result.getValue(); |
157 | long[] expected = new long[] { 2, 2 }; | |
158 | assertArrayEquals(expected, values); | |
81c8e6f7 MK |
159 | } |
160 | ||
161 | /** | |
162 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
163 | */ |
164 | @Test | |
95bf10e7 | 165 | public void testParseField_string() { |
81c8e6f7 MK |
166 | Definition fieldDef = fixture.lookupDefinition(STR); |
167 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
68b18f2f | 168 | assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result.toString()); |
81c8e6f7 | 169 | } |
7a6cee1a GB |
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 | } | |
404b264a GB |
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); | |
51cc7ef4 | 198 | assertEquals("test=float=9.551467814359616E-38", result.toString()); |
404b264a | 199 | } |
95bf10e7 | 200 | } |