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
.assertFalse
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.assertTrue
;
18 import java
.nio
.ByteBuffer
;
19 import java
.nio
.ByteOrder
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * The class <code>ArrayDefinitionTest</code> contains tests for the class
38 * <code>{@link ArrayDefinition}</code>.
41 * @version $Revision: 1.0 $
43 public class ArrayDefinitionTest
{
45 private CTFTrace trace
;
46 private ArrayDefinition charArrayFixture
;
47 private ArrayDefinition stringArrayFixture
;
48 private ArrayDefinition longArrayFixture
;
51 * Perform pre-test initialization.
53 * structDef shouldn't be null after parsing the CTFTraceReader object, so
54 * we can ignore the warning.
58 charArrayFixture
= createCharArray();
59 stringArrayFixture
= createStringArray();
60 longArrayFixture
= createLongArray();
63 private ArrayDefinition
createLongArray() {
64 IntegerDeclaration decl
= new IntegerDeclaration(32, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "none",8);
65 IntegerDefinition
[] defs
= createIntDefs(10, 32);
66 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
70 private ArrayDefinition
createCharArray() {
71 IntegerDeclaration decl
= new IntegerDeclaration(8, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.UTF8
, "none",8);
72 IntegerDefinition
[] defs
= createIntDefs(4,8);
73 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
77 private ArrayDefinition
createStringArray() {
78 StringDeclaration strDecl
= new StringDeclaration();
79 StringDefinition
[] defs
= createDefs();
80 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
84 private ArrayDefinition
setUpDeclaration(IDeclaration decl
,
86 ArrayDeclaration ad
= new ArrayDeclaration(0, decl
);
87 ArrayDefinition temp
= new ArrayDefinition(ad
, this.trace
, "Testx");
88 temp
.setDefinitions(defs
);
93 private static IntegerDefinition
[] createIntDefs(int size
, int bits
) {
94 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
95 for (int i
= 0; i
< size
; i
++) {
97 String content
= "test" + i
;
98 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(bits
, false,
99 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
, 24), null, content
);
105 private static StringDefinition
[] createDefs() {
107 StringDefinition
[] defs
= new StringDefinition
[size
];
108 for (int i
= 0; i
< size
; i
++) {
110 String content
= "test" + i
;
111 defs
[i
] = new StringDefinition(
112 new StringDeclaration(Encoding
.UTF8
), null, content
);
113 defs
[i
].setString(new StringBuilder(content
));
119 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
123 public void testArrayDefinition_baseDeclaration() {
124 ArrayDeclaration declaration
= charArrayFixture
.getDeclaration();
125 String fieldName
= "";
127 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
, fieldName
);
128 assertNotNull(result
);
132 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
136 public void testArrayDefinition_newDeclaration() {
137 ArrayDeclaration declaration
= new ArrayDeclaration(0,
138 new StringDeclaration());
139 IDefinitionScope definitionScope
= null;
140 String fieldName
= "";
142 ArrayDefinition result
= new ArrayDefinition(declaration
, definitionScope
, fieldName
);
143 assertNotNull(result
);
147 * Run the ArrayDeclaration getDeclaration() method test.
150 public void testGetDeclaration() {
151 charArrayFixture
.setDefinitions(new Definition
[] {});
152 ArrayDeclaration result
= charArrayFixture
.getDeclaration();
154 assertNotNull(result
);
158 * Run the Definition getElem(int) method test.
161 public void testGetElem_noDefs() {
163 Definition result
= charArrayFixture
.getElem(i
);
165 assertNotNull(result
);
169 * Run the Definition getElem(int) method test.
172 public void testGetElem_withDefs() {
173 Definition defs
[] = createDefs();
174 charArrayFixture
.setDefinitions(defs
);
177 Definition result
= charArrayFixture
.getElem(j
);
179 assertNotNull(result
);
183 * Run the boolean isString() method test.
186 public void testIsString_ownDefs() {
188 boolean result
= stringArrayFixture
.isString();
194 * Run the boolean isString() method test.
197 public void testIsString_complex() {
198 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
199 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, null, 8);
200 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
201 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
, "Testx");
205 IntegerDefinition
[] defs
= createIntDefs(size
, bits
);
207 ownFixture
.setDefinitions(defs
);
208 boolean result
= ownFixture
.isString();
214 * Run the boolean isString() method test.
217 public void testIsString_emptyDef() {
218 charArrayFixture
.setDefinitions(new Definition
[] {});
219 boolean result
= charArrayFixture
.isString();
225 * Run the boolean isString() method test.
228 public void testIsString_emptyDefStrDecl() {
229 ArrayDefinition ownFixture
= createStringArray();
230 boolean result
= ownFixture
.isString();
234 * Run the void read(BitBuffer) method test.
237 public void testRead_noDefs() {
238 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
240 charArrayFixture
.read(input
);
244 * Run the void read(BitBuffer) method test.
247 public void testRead_withDefs() {
248 charArrayFixture
.setDefinitions(new Definition
[] {});
249 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
251 charArrayFixture
.read(input
);
255 * Run the String toString() method test.
258 public void testToString_char() {
259 String result
= charArrayFixture
.toString();
260 assertNotNull(result
);
263 * Run the String toString() method test.
266 public void testToString_long() {
267 String result
= longArrayFixture
.toString();
268 assertNotNull(result
);
272 * Run the String toString() method test.
275 public void testToString_string() {
276 String result
= stringArrayFixture
.toString();
277 assertNotNull(result
);
280 * Run the String toString() method test.
283 public void testToString_withDefs() {
284 String result
= charArrayFixture
.toString();
286 assertNotNull(result
);
289 * Run the String toString() method test.
292 public void testToStringStringArray() {
293 String result
= stringArrayFixture
.toString();
295 assertNotNull(result
);