btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / ArrayDefinition2Test.java
CommitLineData
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
12package org.eclipse.linuxtools.ctf.core.tests.types;
13
866e5b51 14import static org.junit.Assert.assertNotNull;
866e5b51
FC
15
16import java.nio.ByteBuffer;
17import java.nio.ByteOrder;
a4fa4e36
MK
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.List;
866e5b51 21
a4fa4e36 22import org.eclipse.jdt.annotation.NonNull;
486efb2e 23import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
a4fa4e36
MK
24import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
25import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
7b4f13e6 26import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration;
866e5b51
FC
27import org.eclipse.linuxtools.ctf.core.event.types.Definition;
28import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
ce2388e0 29import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
cc98c947 30import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
866e5b51
FC
31import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
33import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
34import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
aefc5c83 35import org.eclipse.linuxtools.ctf.core.tests.io.Util;
db8e8f7d 36import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
866e5b51 37import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
7b4f13e6
MK
38import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
39import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition;
866e5b51
FC
40import org.junit.Before;
41import org.junit.Test;
42
43/**
7b4f13e6 44 * The class <code>ArrayDefinition2Test</code> contains tests for the class
866e5b51 45 * <code>{@link ArrayDefinition}</code>.
284fdee8 46 *
866e5b51 47 */
7b4f13e6 48public class ArrayDefinition2Test {
866e5b51 49
aefc5c83 50 private @NonNull CTFTrace trace = new CTFTrace();
ce2388e0
FC
51 private ArrayDefinition charArrayFixture;
52 private ArrayDefinition stringArrayFixture;
53 private ArrayDefinition longArrayFixture;
866e5b51 54
866e5b51
FC
55 /**
56 * Perform pre-test initialization.
284fdee8 57 *
866e5b51
FC
58 * structDef shouldn't be null after parsing the CTFTraceReader object, so
59 * we can ignore the warning.
60 */
866e5b51 61 @Before
be6df2d8 62 public void setUp() {
ce2388e0
FC
63 charArrayFixture = createCharArray();
64 stringArrayFixture = createStringArray();
65 longArrayFixture = createLongArray();
866e5b51
FC
66 }
67
ce2388e0 68 private ArrayDefinition createLongArray() {
a4fa4e36
MK
69 IntegerDeclaration decl = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none", 8);
70 List<Definition> defs = createIntDefs(10, 32);
ce2388e0
FC
71 ArrayDefinition temp = setUpDeclaration(decl, defs);
72 return temp;
73 }
74
75 private ArrayDefinition createCharArray() {
a4fa4e36
MK
76 IntegerDeclaration decl = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none", 8);
77 List<Definition> defs = createIntDefs(4, 8);
ce2388e0
FC
78 ArrayDefinition temp = setUpDeclaration(decl, defs);
79 return temp;
80 }
81
ce2388e0
FC
82 private ArrayDefinition createStringArray() {
83 StringDeclaration strDecl = new StringDeclaration();
a4fa4e36 84 List<Definition> defs = createDefs();
ce2388e0
FC
85 ArrayDefinition temp = setUpDeclaration(strDecl, defs);
86 return temp;
87 }
a34d8eee 88
ce2388e0 89 private ArrayDefinition setUpDeclaration(IDeclaration decl,
a4fa4e36 90 @NonNull List<Definition> defs) {
9377d173 91 CompoundDeclaration ad = new ArrayDeclaration(0, decl);
a4fa4e36 92 ArrayDefinition temp = new ArrayDefinition(ad, this.trace, "Testx", defs);
ce2388e0
FC
93 return temp;
94 }
a34d8eee 95
aefc5c83
MK
96 @NonNull
97 private static List<Definition> createIntDefs(int size, int bits) {
a4fa4e36 98 List<Definition> defs = new ArrayList<>(size);
ce2388e0 99 for (int i = 0; i < size; i++) {
4a9c1f07 100 String content = "test" + i;
a4fa4e36
MK
101 defs.add(new IntegerDefinition(IntegerDeclaration.createDeclaration(bits, false,
102 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content, i));
ce2388e0
FC
103 }
104 return defs;
105 }
866e5b51 106
aefc5c83
MK
107 @NonNull
108 private static List<Definition> createDefs() {
866e5b51 109 int size = 4;
a4fa4e36 110 List<Definition> defs = new ArrayList<>();
866e5b51 111 for (int i = 0; i < size; i++) {
4a9c1f07 112 String content = "test" + i;
a4fa4e36
MK
113 defs.add(new StringDefinition(
114 new StringDeclaration(Encoding.UTF8), null, content, content));
866e5b51
FC
115 }
116 return defs;
117 }
118
119 /**
120 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
121 * constructor test.
122 */
123 @Test
124 public void testArrayDefinition_baseDeclaration() {
9377d173 125 CompoundDeclaration declaration = (CompoundDeclaration) charArrayFixture.getDeclaration();
4a9c1f07 126 String fieldName = "";
866e5b51 127
a4fa4e36
MK
128 @SuppressWarnings("null")
129 ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName, Arrays.asList(new Definition[0]));
866e5b51
FC
130 assertNotNull(result);
131 }
132
133 /**
134 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
135 * constructor test.
136 */
137 @Test
138 public void testArrayDefinition_newDeclaration() {
9377d173 139 CompoundDeclaration declaration = new ArrayDeclaration(0,
866e5b51 140 new StringDeclaration());
a4fa4e36 141 IDefinitionScope definitionScope = getDefinitionScope();
866e5b51 142
a4fa4e36
MK
143 String fieldName = "";
144 @SuppressWarnings("null")
aefc5c83 145 ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName, Arrays.asList(new Definition[0]));
866e5b51
FC
146 assertNotNull(result);
147 }
148
149 /**
150 * Run the ArrayDeclaration getDeclaration() method test.
151 */
152 @Test
153 public void testGetDeclaration() {
9377d173 154 CompoundDeclaration result = (CompoundDeclaration) charArrayFixture.getDeclaration();
866e5b51
FC
155
156 assertNotNull(result);
157 }
158
159 /**
7b4f13e6 160 * Run the Definition getDefinitions().get(int) method test.
866e5b51
FC
161 */
162 @Test
7b4f13e6 163 public void testgetElem_noDefs() {
866e5b51 164 int i = 0;
cc98c947 165 IDefinition result = charArrayFixture.getDefinitions().get(i);
866e5b51
FC
166
167 assertNotNull(result);
168 }
169
170 /**
7b4f13e6 171 * Run the Definition getDefinitions().get(int) method test.
866e5b51
FC
172 */
173 @Test
7b4f13e6 174 public void testgetElem_withDefs() {
a4fa4e36
MK
175 List<Definition> defs = createDefs();
176 IDefinitionScope definitionScope = getDefinitionScope();
7b4f13e6 177 ArrayDefinition ad = new ArrayDefinition((CompoundDeclaration) charArrayFixture.getDeclaration(), definitionScope, "test", defs);
866e5b51
FC
178 int j = 1;
179
cc98c947 180 IDefinition result = ad.getDefinitions().get(j);
866e5b51
FC
181
182 assertNotNull(result);
183 }
184
aefc5c83
MK
185 @NonNull
186 private static IDefinitionScope getDefinitionScope() {
a4fa4e36
MK
187 return new IDefinitionScope() {
188
189 @Override
190 public Definition lookupDefinition(String lookupPath) {
191 return null;
192 }
866e5b51 193
a4fa4e36
MK
194 @Override
195 public LexicalScope getScopePath() {
196 return null;
197 }
198 };
866e5b51
FC
199 }
200
201 /**
202 * Run the void read(BitBuffer) method test.
a4fa4e36
MK
203 *
204 * @throws CTFReaderException
205 * error
866e5b51
FC
206 */
207 @Test
a4fa4e36 208 public void testRead_noDefs() throws CTFReaderException {
aefc5c83 209 BitBuffer input = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(128)));
a4fa4e36 210 charArrayFixture.getDeclaration().createDefinition(null, "test", input);
866e5b51
FC
211 }
212
213 /**
214 * Run the String toString() method test.
215 */
216 @Test
ce2388e0
FC
217 public void testToString_char() {
218 String result = charArrayFixture.toString();
219 assertNotNull(result);
220 }
a4fa4e36 221
ce2388e0
FC
222 /**
223 * Run the String toString() method test.
224 */
225 @Test
226 public void testToString_long() {
227 String result = longArrayFixture.toString();
866e5b51
FC
228 assertNotNull(result);
229 }
230
ce2388e0
FC
231 /**
232 * Run the String toString() method test.
233 */
234 @Test
235 public void testToString_string() {
236 String result = stringArrayFixture.toString();
237 assertNotNull(result);
238 }
a4fa4e36 239
866e5b51
FC
240 /**
241 * Run the String toString() method test.
242 */
243 @Test
244 public void testToString_withDefs() {
ce2388e0
FC
245 String result = charArrayFixture.toString();
246
247 assertNotNull(result);
248 }
a4fa4e36 249
ce2388e0
FC
250 /**
251 * Run the String toString() method test.
252 */
253 @Test
254 public void testToStringStringArray() {
255 String result = stringArrayFixture.toString();
866e5b51
FC
256
257 assertNotNull(result);
258 }
259}
This page took 0.059412 seconds and 5 git commands to generate.