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