Commit | Line | Data |
---|---|---|
4bd7f2db AM |
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 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
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; |
486efb2e | 21 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
7b4f13e6 | 22 | import org.eclipse.linuxtools.ctf.core.event.types.AbstractArrayDefinition; |
866e5b51 | 23 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
024373d7 | 24 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; |
866e5b51 | 25 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
024373d7 | 26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
866e5b51 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
024373d7 | 28 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
866e5b51 FC |
29 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
30 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
31 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
024373d7 | 32 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; |
866e5b51 | 33 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; |
aefc5c83 | 34 | import org.eclipse.linuxtools.ctf.core.tests.io.Util; |
db8e8f7d | 35 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
7b4f13e6 | 36 | import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration; |
866e5b51 FC |
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>. | |
ce2388e0 | 43 | * |
866e5b51 FC |
44 | * @author ematkho |
45 | * @version $Revision: 1.0 $ | |
46 | */ | |
47 | public class StructDefinitionTest { | |
48 | ||
aefc5c83 MK |
49 | private static final @NonNull String TEST_STRUCT_ID = "testStruct"; |
50 | private static final @NonNull String ENUM_2 = "y"; | |
51 | private static final @NonNull String ENUM_1 = "x"; | |
52 | private static final @NonNull String TAG_ID = "Tag"; | |
53 | private static final @NonNull String INT_ID = "_id"; | |
54 | private static final @NonNull String STRING_ID = "_args"; | |
55 | private static final @NonNull String ENUM_ID = "_enumArgs"; | |
56 | private static final @NonNull String SEQUENCE_ID = "_seq"; | |
57 | private static final @NonNull String LENGTH_SEQ = "_len"; | |
58 | private static final @NonNull String VAR_FIELD_NAME = "SomeVariant"; | |
024373d7 | 59 | |
866e5b51 | 60 | private StructDefinition fixture; |
419f09a8 | 61 | private StructDefinition emptyStruct; |
419f09a8 | 62 | private StructDefinition simpleStruct; |
866e5b51 | 63 | |
866e5b51 FC |
64 | /** |
65 | * Perform pre-test initialization. | |
a4fa4e36 MK |
66 | * |
67 | * @throws CTFReaderException | |
68 | * won't happen | |
866e5b51 FC |
69 | */ |
70 | @Before | |
a4fa4e36 | 71 | public void setUp() throws CTFReaderException { |
024373d7 | 72 | StructDeclaration sDec = new StructDeclaration(12); |
a4fa4e36 MK |
73 | IntegerDeclaration id = IntegerDeclaration.INT_32B_DECL; |
74 | IntegerDeclaration lenDec = IntegerDeclaration.UINT_8_DECL; | |
024373d7 MK |
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); | |
a4fa4e36 MK |
92 | byte bytes[] = new byte[100]; |
93 | bytes[4] = 1; | |
94 | bytes[8] = 2; | |
95 | bytes[13] = 3; | |
aefc5c83 | 96 | BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(bytes))); |
a4fa4e36 MK |
97 | fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb); |
98 | EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb); | |
99 | assertNotNull(eDef); | |
100 | VariantDefinition vd = varDec.createDefinition(fixture, VAR_FIELD_NAME, bb); | |
101 | assertNotNull(vd); | |
419f09a8 SM |
102 | // Create an empty struct |
103 | StructDeclaration esDec = new StructDeclaration(32); | |
a4fa4e36 | 104 | emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID, bb); |
419f09a8 SM |
105 | |
106 | // Create a simple struct with two items | |
107 | StructDeclaration ssDec = new StructDeclaration(32); | |
108 | ssDec.addField(INT_ID, id); | |
109 | ssDec.addField(STRING_ID, sd); | |
a4fa4e36 | 110 | simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID, bb); |
866e5b51 FC |
111 | } |
112 | ||
113 | /** | |
114 | * Run the StructDeclaration getDeclaration() method test. | |
115 | */ | |
116 | @Test | |
117 | public void testGetDeclaration() { | |
118 | StructDeclaration result = fixture.getDeclaration(); | |
119 | assertNotNull(result); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Run the HashMap<String, Definition> getDefinitions() method test. | |
124 | */ | |
125 | @Test | |
126 | public void testGetDefinitions_1() { | |
a4fa4e36 | 127 | Definition result = fixture.getDefinition("_id"); |
866e5b51 FC |
128 | assertNotNull(result); |
129 | } | |
130 | ||
866e5b51 FC |
131 | /** |
132 | * Run the ArrayDefinition lookupArray(String) method test. | |
133 | */ | |
134 | @Test | |
135 | public void testLookupArray() { | |
024373d7 | 136 | String name = INT_ID; |
70f60307 | 137 | AbstractArrayDefinition result = fixture.lookupArrayDefinition(name); |
866e5b51 FC |
138 | assertNull(result); |
139 | } | |
140 | ||
141 | /** | |
142 | * Run the Definition lookupDefinition(String) method test. | |
143 | */ | |
144 | @Test | |
145 | public void testLookupDefinition() { | |
4a9c1f07 | 146 | String lookupPath = "args"; |
866e5b51 FC |
147 | Definition result = fixture.lookupDefinition(lookupPath); |
148 | ||
149 | assertNotNull(result); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Run the EnumDefinition lookupEnum(String) method test. | |
154 | */ | |
155 | @Test | |
156 | public void testLookupEnum() { | |
024373d7 | 157 | String name = ENUM_ID; |
866e5b51 | 158 | EnumDefinition result = fixture.lookupEnum(name); |
024373d7 | 159 | assertNotNull(result); |
866e5b51 FC |
160 | } |
161 | ||
162 | /** | |
163 | * Run the IntegerDefinition lookupInteger(String) method test. | |
164 | */ | |
165 | @Test | |
166 | public void testLookupInteger_1() { | |
4a9c1f07 | 167 | String name = "_id"; |
866e5b51 | 168 | IntegerDefinition result = fixture.lookupInteger(name); |
866e5b51 FC |
169 | assertNotNull(result); |
170 | } | |
171 | ||
172 | /** | |
173 | * Run the IntegerDefinition lookupInteger(String) method test. | |
174 | */ | |
175 | @Test | |
176 | public void testLookupInteger_2() { | |
177 | String name = VAR_FIELD_NAME; | |
178 | IntegerDefinition result = fixture.lookupInteger(name); | |
866e5b51 FC |
179 | assertNull(result); |
180 | } | |
181 | ||
182 | /** | |
183 | * Run the SequenceDefinition lookupSequence(String) method test. | |
184 | */ | |
185 | @Test | |
7b4f13e6 | 186 | public void testLookupFixedStringDefinition() { |
024373d7 | 187 | String name = SEQUENCE_ID; |
70f60307 | 188 | AbstractArrayDefinition result = fixture.lookupArrayDefinition(name); |
024373d7 | 189 | assertNotNull(result); |
866e5b51 FC |
190 | } |
191 | ||
192 | /** | |
193 | * Run the StringDefinition lookupString(String) method test. | |
194 | */ | |
195 | @Test | |
196 | public void testLookupString() { | |
197 | String name = VAR_FIELD_NAME; | |
198 | StringDefinition result = fixture.lookupString(name); | |
199 | ||
200 | assertNull(result); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Run the StructDefinition lookupStruct(String) method test. | |
205 | */ | |
206 | @Test | |
207 | public void testLookupStruct() { | |
208 | String name = VAR_FIELD_NAME; | |
209 | StructDefinition result = fixture.lookupStruct(name); | |
210 | ||
211 | assertNull(result); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Run the VariantDefinition lookupVariant(String) method test. | |
216 | */ | |
217 | @Test | |
218 | public void testLookupVariant() { | |
219 | String name = VAR_FIELD_NAME; | |
220 | VariantDefinition result = fixture.lookupVariant(name); | |
221 | ||
024373d7 | 222 | assertNotNull(result); |
866e5b51 FC |
223 | } |
224 | ||
866e5b51 FC |
225 | /** |
226 | * Run the String toString() method test. | |
227 | */ | |
228 | @Test | |
229 | public void testToString() { | |
230 | String result = fixture.toString(); | |
231 | assertNotNull(result); | |
419f09a8 SM |
232 | |
233 | result = emptyStruct.toString(); | |
4a9c1f07 | 234 | assertEquals("{ }", result); |
419f09a8 SM |
235 | |
236 | result = simpleStruct.toString(); | |
4a9c1f07 | 237 | assertEquals("{ _id = 0, _args = \"\" }", result); |
866e5b51 FC |
238 | } |
239 | } |