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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
14 import static org
.junit
.Assert
.assertNotNull
;
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
.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
;
43 * The class <code>ArrayDefinition2Test</code> contains tests for the class
44 * <code>{@link ArrayDefinition}</code>.
47 public class ArrayDefinition2Test
{
49 private @NonNull CTFTrace trace
= new CTFTrace();
50 private ArrayDefinition charArrayFixture
;
51 private ArrayDefinition stringArrayFixture
;
52 private ArrayDefinition longArrayFixture
;
55 * Perform pre-test initialization.
57 * structDef shouldn't be null after parsing the CTFTraceReader object, so
58 * we can ignore the warning.
62 charArrayFixture
= createCharArray();
63 stringArrayFixture
= createStringArray();
64 longArrayFixture
= createLongArray();
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
);
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
);
81 private ArrayDefinition
createStringArray() {
82 StringDeclaration strDecl
= new StringDeclaration();
83 List
<Definition
> defs
= createDefs();
84 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
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
);
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
));
107 private static List
<Definition
> createDefs() {
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
));
119 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
123 public void testArrayDefinition_baseDeclaration() {
124 ArrayDeclaration declaration
= (ArrayDeclaration
) charArrayFixture
.getDeclaration();
125 String fieldName
= "";
127 @SuppressWarnings("null")
128 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
, fieldName
, Arrays
.asList(new Definition
[0]));
129 assertNotNull(result
);
133 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
137 public void testArrayDefinition_newDeclaration() {
138 ArrayDeclaration declaration
= new ArrayDeclaration(0,
139 new StringDeclaration());
140 IDefinitionScope definitionScope
= getDefinitionScope();
142 String fieldName
= "";
143 @SuppressWarnings("null")
144 ArrayDefinition result
= new ArrayDefinition(declaration
, definitionScope
, fieldName
, Arrays
.asList(new Definition
[0]));
145 assertNotNull(result
);
149 * Run the ArrayDeclaration getDeclaration() method test.
152 public void testGetDeclaration() {
153 ArrayDeclaration result
= (ArrayDeclaration
) charArrayFixture
.getDeclaration();
155 assertNotNull(result
);
159 * Run the Definition getDefinitions().get(int) method test.
162 public void testgetElem_noDefs() {
164 Definition result
= charArrayFixture
.getDefinitions().get(i
);
166 assertNotNull(result
);
170 * Run the Definition getDefinitions().get(int) method 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
);
179 Definition result
= ad
.getDefinitions().get(j
);
181 assertNotNull(result
);
185 private static IDefinitionScope
getDefinitionScope() {
186 return new IDefinitionScope() {
189 public Definition
lookupDefinition(String lookupPath
) {
194 public LexicalScope
getScopePath() {
201 * Run the void read(BitBuffer) method test.
203 * @throws CTFReaderException
207 public void testRead_noDefs() throws CTFReaderException
{
208 BitBuffer input
= new BitBuffer(Util
.testMemory(ByteBuffer
.allocateDirect(128)));
209 charArrayFixture
.getDeclaration().createDefinition(null, "test", input
);
213 * Run the String toString() method test.
216 public void testToString_char() {
217 String result
= charArrayFixture
.toString();
218 assertNotNull(result
);
222 * Run the String toString() method test.
225 public void testToString_long() {
226 String result
= longArrayFixture
.toString();
227 assertNotNull(result
);
231 * Run the String toString() method test.
234 public void testToString_string() {
235 String result
= stringArrayFixture
.toString();
236 assertNotNull(result
);
240 * Run the String toString() method test.
243 public void testToString_withDefs() {
244 String result
= charArrayFixture
.toString();
246 assertNotNull(result
);
250 * Run the String toString() method test.
253 public void testToStringStringArray() {
254 String result
= stringArrayFixture
.toString();
256 assertNotNull(result
);