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
.event
.io
.BitBuffer
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.LexicalScope
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.io
.Util
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
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
<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
<Definition
> defs
= createIntDefs(4, 8);
78 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
82 private ArrayDefinition
createStringArray() {
83 StringDeclaration strDecl
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
84 List
<Definition
> defs
= createDefs();
85 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
89 private ArrayDefinition
setUpDeclaration(@NonNull IDeclaration decl
,
90 @NonNull List
<Definition
> defs
) {
91 CompoundDeclaration ad
= new ArrayDeclaration(0, decl
);
92 ArrayDefinition temp
= new ArrayDefinition(ad
, this.trace
, "Testx", defs
);
97 private static List
<Definition
> createIntDefs(int size
, int bits
) {
98 List
<Definition
> defs
= new ArrayList
<>(size
);
99 for (int i
= 0; i
< size
; i
++) {
100 String content
= "test" + i
;
101 defs
.add(new IntegerDefinition(IntegerDeclaration
.createDeclaration(bits
, false,
102 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
, 24), null, content
, i
));
108 private static List
<Definition
> createDefs() {
110 List
<Definition
> defs
= new ArrayList
<>();
111 for (int i
= 0; i
< size
; i
++) {
112 String content
= "test" + i
;
113 defs
.add(new StringDefinition(
114 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
), null, content
, content
));
120 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
124 public void testArrayDefinition_baseDeclaration() {
125 CompoundDeclaration declaration
= (CompoundDeclaration
) charArrayFixture
.getDeclaration();
126 String fieldName
= "";
128 @SuppressWarnings("null")
129 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
, fieldName
, Arrays
.asList(new Definition
[0]));
130 assertNotNull(result
);
134 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
138 public void testArrayDefinition_newDeclaration() {
139 CompoundDeclaration declaration
= new ArrayDeclaration(0,
140 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
141 IDefinitionScope definitionScope
= getDefinitionScope();
143 String fieldName
= "";
144 @SuppressWarnings("null")
145 ArrayDefinition result
= new ArrayDefinition(declaration
, definitionScope
, fieldName
, Arrays
.asList(new Definition
[0]));
146 assertNotNull(result
);
150 * Run the ArrayDeclaration getDeclaration() method test.
153 public void testGetDeclaration() {
154 CompoundDeclaration result
= (CompoundDeclaration
) charArrayFixture
.getDeclaration();
156 assertNotNull(result
);
160 * Run the Definition getDefinitions().get(int) method test.
163 public void testgetElem_noDefs() {
165 IDefinition result
= charArrayFixture
.getDefinitions().get(i
);
167 assertNotNull(result
);
171 * Run the Definition getDefinitions().get(int) method test.
174 public void testgetElem_withDefs() {
175 List
<Definition
> defs
= createDefs();
176 IDefinitionScope definitionScope
= getDefinitionScope();
177 ArrayDefinition ad
= new ArrayDefinition((CompoundDeclaration
) charArrayFixture
.getDeclaration(), definitionScope
, "test", defs
);
180 IDefinition result
= ad
.getDefinitions().get(j
);
182 assertNotNull(result
);
186 private static IDefinitionScope
getDefinitionScope() {
187 return new IDefinitionScope() {
190 public Definition
lookupDefinition(String lookupPath
) {
195 public LexicalScope
getScopePath() {
202 * Run the void read(BitBuffer) method test.
204 * @throws CTFReaderException
208 public void testRead_noDefs() throws CTFReaderException
{
209 BitBuffer input
= new BitBuffer(Util
.testMemory(ByteBuffer
.allocateDirect(128)));
210 charArrayFixture
.getDeclaration().createDefinition(null, "test", input
);
214 * Run the String toString() method test.
217 public void testToString_char() {
218 String result
= charArrayFixture
.toString();
219 assertNotNull(result
);
223 * Run the String toString() method test.
226 public void testToString_long() {
227 String result
= longArrayFixture
.toString();
228 assertNotNull(result
);
232 * Run the String toString() method test.
235 public void testToString_string() {
236 String result
= stringArrayFixture
.toString();
237 assertNotNull(result
);
241 * Run the String toString() method test.
244 public void testToString_withDefs() {
245 String result
= charArrayFixture
.toString();
247 assertNotNull(result
);
251 * Run the String toString() method test.
254 public void testToStringStringArray() {
255 String result
= stringArrayFixture
.toString();
257 assertNotNull(result
);