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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.types
;
14 import static org
.junit
.Assert
.*;
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
;
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
;
44 * The class <code>ArrayDefinition2Test</code> contains tests for the class
45 * <code>{@link ArrayDefinition}</code>.
48 public class ArrayDefinition2Test
{
50 private @NonNull CTFTrace trace
= new CTFTrace();
51 private ArrayDefinition charArrayFixture
;
52 private ArrayDefinition stringArrayFixture
;
53 private ArrayDefinition longArrayFixture
;
56 * Perform pre-test initialization.
58 * structDef shouldn't be null after parsing the CTFTraceReader object, so
59 * we can ignore the warning.
63 charArrayFixture
= createCharArray();
64 stringArrayFixture
= createStringArray();
65 longArrayFixture
= createLongArray();
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
);
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
);
82 private ArrayDefinition
createStringArray() {
83 StringDeclaration strDecl
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
84 List
<@NonNull Definition
> defs
= createDefs();
85 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
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
);
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
));
106 private static @NonNull List
<@NonNull Definition
> createDefs() {
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
));
118 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
122 public void testArrayDefinition_baseDeclaration() {
123 CompoundDeclaration declaration
= (CompoundDeclaration
) charArrayFixture
.getDeclaration();
124 String fieldName
= "";
126 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
, fieldName
, Arrays
.asList(new @NonNull Definition
[0]));
127 assertNotNull(result
);
131 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
135 public void testArrayDefinition_newDeclaration() {
136 CompoundDeclaration declaration
= new ArrayDeclaration(0,
137 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
138 IDefinitionScope definitionScope
= getDefinitionScope();
140 String fieldName
= "";
141 ArrayDefinition result
= new ArrayDefinition(declaration
, definitionScope
, fieldName
, Arrays
.asList(new @NonNull Definition
[0]));
142 assertNotNull(result
);
146 * Run the ArrayDeclaration getDeclaration() method test.
149 public void testGetDeclaration() {
150 CompoundDeclaration result
= (CompoundDeclaration
) charArrayFixture
.getDeclaration();
152 assertNotNull(result
);
156 * Run the Definition getDefinitions().get(int) method test.
159 public void testgetElem_noDefs() {
161 IDefinition result
= charArrayFixture
.getDefinitions().get(i
);
163 assertNotNull(result
);
167 * Run the Definition getDefinitions().get(int) method 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
);
176 IDefinition result
= ad
.getDefinitions().get(j
);
178 assertNotNull(result
);
182 private static IDefinitionScope
getDefinitionScope() {
183 return new IDefinitionScope() {
186 public Definition
lookupDefinition(String lookupPath
) {
191 public LexicalScope
getScopePath() {
198 * Run the void read(BitBuffer) method test.
200 * @throws CTFException
204 public void testRead_noDefs() throws CTFException
{
205 BitBuffer input
= new BitBuffer(Util
.testMemory(ByteBuffer
.allocateDirect(128)));
206 charArrayFixture
.getDeclaration().createDefinition(null, "test", input
);
210 * Run the String toString() method test.
213 public void testToString_char() {
214 String result
= charArrayFixture
.toString();
215 assertNotNull(result
);
219 * Run the String toString() method test.
222 public void testToString_long() {
223 String result
= longArrayFixture
.toString();
224 assertNotNull(result
);
228 * Run the String toString() method test.
231 public void testToString_string() {
232 String result
= stringArrayFixture
.toString();
233 assertNotNull(result
);
237 * Run the String toString() method test.
240 public void testToString_withDefs() {
241 String result
= charArrayFixture
.toString();
243 assertNotNull(result
);
247 * Run the String toString() method test.
250 public void testToStringStringArray() {
251 String result
= stringArrayFixture
.toString();
253 assertNotNull(result
);