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