Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
ed902a2b | 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 | ||
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 | 13 | |
419f09a8 | 14 | import static org.junit.Assert.assertEquals; |
866e5b51 FC |
15 | import static org.junit.Assert.assertNotNull; |
16 | import static org.junit.Assert.assertNull; | |
17 | ||
18 | import java.nio.ByteBuffer; | |
866e5b51 | 19 | |
a4fa4e36 | 20 | import org.eclipse.jdt.annotation.NonNull; |
453a59f0 | 21 | import org.eclipse.tracecompass.ctf.core.CTFReaderException; |
f357bcd4 AM |
22 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
23 | import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition; | |
d890ec37 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
f357bcd4 AM |
25 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration; |
26 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition; | |
32 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
33 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
34 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration; | |
35 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition; | |
36 | import org.eclipse.tracecompass.ctf.core.tests.io.Util; | |
f357bcd4 | 37 | import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration; |
866e5b51 FC |
38 | import org.junit.Before; |
39 | import org.junit.Test; | |
40 | ||
41 | /** | |
42 | * The class <code>StructDefinitionTest</code> contains tests for the class | |
43 | * <code>{@link StructDefinition}</code>. | |
ce2388e0 | 44 | * |
866e5b51 FC |
45 | * @author ematkho |
46 | * @version $Revision: 1.0 $ | |
47 | */ | |
48 | public class StructDefinitionTest { | |
49 | ||
aefc5c83 MK |
50 | private static final @NonNull String TEST_STRUCT_ID = "testStruct"; |
51 | private static final @NonNull String ENUM_2 = "y"; | |
52 | private static final @NonNull String ENUM_1 = "x"; | |
53 | private static final @NonNull String TAG_ID = "Tag"; | |
54 | private static final @NonNull String INT_ID = "_id"; | |
55 | private static final @NonNull String STRING_ID = "_args"; | |
56 | private static final @NonNull String ENUM_ID = "_enumArgs"; | |
57 | private static final @NonNull String SEQUENCE_ID = "_seq"; | |
58 | private static final @NonNull String LENGTH_SEQ = "_len"; | |
59 | private static final @NonNull String VAR_FIELD_NAME = "SomeVariant"; | |
024373d7 | 60 | |
866e5b51 | 61 | private StructDefinition fixture; |
419f09a8 | 62 | private StructDefinition emptyStruct; |
419f09a8 | 63 | private StructDefinition simpleStruct; |
866e5b51 | 64 | |
866e5b51 FC |
65 | /** |
66 | * Perform pre-test initialization. | |
a4fa4e36 MK |
67 | * |
68 | * @throws CTFReaderException | |
69 | * won't happen | |
866e5b51 FC |
70 | */ |
71 | @Before | |
a4fa4e36 | 72 | public void setUp() throws CTFReaderException { |
024373d7 | 73 | StructDeclaration sDec = new StructDeclaration(12); |
a4fa4e36 MK |
74 | IntegerDeclaration id = IntegerDeclaration.INT_32B_DECL; |
75 | IntegerDeclaration lenDec = IntegerDeclaration.UINT_8_DECL; | |
d890ec37 | 76 | StringDeclaration sd = StringDeclaration.getStringDeclaration(Encoding.UTF8); |
024373d7 MK |
77 | EnumDeclaration ed = new EnumDeclaration(id); |
78 | SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, id); | |
79 | VariantDeclaration varDec = new VariantDeclaration(); | |
80 | EnumDeclaration tagDec = new EnumDeclaration(id); | |
81 | tagDec.add(0, 1, ENUM_1); | |
82 | tagDec.add(2, 3, ENUM_2); | |
83 | varDec.addField(ENUM_2, id); | |
84 | varDec.addField(ENUM_1, sd); | |
85 | varDec.setTag(TAG_ID); | |
86 | sDec.addField(INT_ID, id); | |
87 | sDec.addField(STRING_ID, sd); | |
88 | sDec.addField(ENUM_ID, ed); | |
89 | sDec.addField(TAG_ID, tagDec); | |
90 | sDec.addField(LENGTH_SEQ, lenDec); | |
91 | sDec.addField(SEQUENCE_ID, seqDec); | |
92 | sDec.addField(VAR_FIELD_NAME, varDec); | |
a4fa4e36 MK |
93 | byte bytes[] = new byte[100]; |
94 | bytes[4] = 1; | |
95 | bytes[8] = 2; | |
96 | bytes[13] = 3; | |
aefc5c83 | 97 | BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(bytes))); |
a4fa4e36 MK |
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); | |
419f09a8 SM |
103 | // Create an empty struct |
104 | StructDeclaration esDec = new StructDeclaration(32); | |
a4fa4e36 | 105 | emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID, bb); |
419f09a8 SM |
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); | |
a4fa4e36 | 111 | simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID, bb); |
866e5b51 FC |
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() { | |
cc98c947 | 128 | IDefinition result = fixture.getDefinition("_id"); |
866e5b51 FC |
129 | assertNotNull(result); |
130 | } | |
131 | ||
866e5b51 FC |
132 | /** |
133 | * Run the ArrayDefinition lookupArray(String) method test. | |
134 | */ | |
135 | @Test | |
136 | public void testLookupArray() { | |
024373d7 | 137 | String name = INT_ID; |
70f60307 | 138 | AbstractArrayDefinition result = fixture.lookupArrayDefinition(name); |
866e5b51 FC |
139 | assertNull(result); |
140 | } | |
141 | ||
142 | /** | |
143 | * Run the Definition lookupDefinition(String) method test. | |
144 | */ | |
145 | @Test | |
146 | public void testLookupDefinition() { | |
4a9c1f07 | 147 | String lookupPath = "args"; |
cc98c947 | 148 | IDefinition result = fixture.lookupDefinition(lookupPath); |
866e5b51 FC |
149 | |
150 | assertNotNull(result); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Run the EnumDefinition lookupEnum(String) method test. | |
155 | */ | |
156 | @Test | |
157 | public void testLookupEnum() { | |
024373d7 | 158 | String name = ENUM_ID; |
866e5b51 | 159 | EnumDefinition result = fixture.lookupEnum(name); |
024373d7 | 160 | assertNotNull(result); |
866e5b51 FC |
161 | } |
162 | ||
163 | /** | |
164 | * Run the IntegerDefinition lookupInteger(String) method test. | |
165 | */ | |
166 | @Test | |
167 | public void testLookupInteger_1() { | |
4a9c1f07 | 168 | String name = "_id"; |
866e5b51 | 169 | IntegerDefinition result = fixture.lookupInteger(name); |
866e5b51 FC |
170 | assertNotNull(result); |
171 | } | |
172 | ||
173 | /** | |
174 | * Run the IntegerDefinition lookupInteger(String) method test. | |
175 | */ | |
176 | @Test | |
177 | public void testLookupInteger_2() { | |
178 | String name = VAR_FIELD_NAME; | |
179 | IntegerDefinition result = fixture.lookupInteger(name); | |
866e5b51 FC |
180 | assertNull(result); |
181 | } | |
182 | ||
183 | /** | |
184 | * Run the SequenceDefinition lookupSequence(String) method test. | |
185 | */ | |
186 | @Test | |
7b4f13e6 | 187 | public void testLookupFixedStringDefinition() { |
024373d7 | 188 | String name = SEQUENCE_ID; |
70f60307 | 189 | AbstractArrayDefinition result = fixture.lookupArrayDefinition(name); |
024373d7 | 190 | assertNotNull(result); |
866e5b51 FC |
191 | } |
192 | ||
193 | /** | |
194 | * Run the StringDefinition lookupString(String) method test. | |
195 | */ | |
196 | @Test | |
197 | public void testLookupString() { | |
198 | String name = VAR_FIELD_NAME; | |
199 | StringDefinition result = fixture.lookupString(name); | |
200 | ||
201 | assertNull(result); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Run the StructDefinition lookupStruct(String) method test. | |
206 | */ | |
207 | @Test | |
208 | public void testLookupStruct() { | |
209 | String name = VAR_FIELD_NAME; | |
210 | StructDefinition result = fixture.lookupStruct(name); | |
211 | ||
212 | assertNull(result); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Run the VariantDefinition lookupVariant(String) method test. | |
217 | */ | |
218 | @Test | |
219 | public void testLookupVariant() { | |
220 | String name = VAR_FIELD_NAME; | |
221 | VariantDefinition result = fixture.lookupVariant(name); | |
222 | ||
024373d7 | 223 | assertNotNull(result); |
866e5b51 FC |
224 | } |
225 | ||
866e5b51 FC |
226 | /** |
227 | * Run the String toString() method test. | |
228 | */ | |
229 | @Test | |
230 | public void testToString() { | |
231 | String result = fixture.toString(); | |
232 | assertNotNull(result); | |
419f09a8 SM |
233 | |
234 | result = emptyStruct.toString(); | |
4a9c1f07 | 235 | assertEquals("{ }", result); |
419f09a8 SM |
236 | |
237 | result = simpleStruct.toString(); | |
4a9c1f07 | 238 | assertEquals("{ _id = 0, _args = \"\" }", result); |
866e5b51 FC |
239 | } |
240 | } |