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
.CTFReaderException
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
34 import org
.junit
.Before
;
35 import org
.junit
.Test
;
38 * The class <code>ArrayDefinitionTest</code> contains tests for the class
39 * <code>{@link ArrayDefinition}</code>.
42 * @version $Revision: 1.0 $
44 public class ArrayDefinitionTest
{
46 private CTFTrace trace
;
47 private ArrayDefinition charArrayFixture
;
48 private ArrayDefinition stringArrayFixture
;
49 private ArrayDefinition longArrayFixture
;
52 * Perform pre-test initialization.
54 * structDef shouldn't be null after parsing the CTFTraceReader object, so
55 * we can ignore the warning.
59 charArrayFixture
= createCharArray();
60 stringArrayFixture
= createStringArray();
61 longArrayFixture
= createLongArray();
64 private ArrayDefinition
createLongArray() {
65 IntegerDeclaration decl
= new IntegerDeclaration(32, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "none",8);
66 IntegerDefinition
[] defs
= createIntDefs(10, 32);
67 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
71 private ArrayDefinition
createCharArray() {
72 IntegerDeclaration decl
= new IntegerDeclaration(8, false, 10, ByteOrder
.BIG_ENDIAN
, Encoding
.UTF8
, "none",8);
73 IntegerDefinition
[] defs
= createIntDefs(4,8);
74 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
78 private ArrayDefinition
createStringArray() {
79 StringDeclaration strDecl
= new StringDeclaration();
80 StringDefinition
[] defs
= createDefs();
81 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
85 private ArrayDefinition
setUpDeclaration(IDeclaration decl
,
87 ArrayDeclaration ad
= new ArrayDeclaration(0, decl
);
88 ArrayDefinition temp
= new ArrayDefinition(ad
, this.trace
, "Testx");
89 temp
.setDefinitions(defs
);
94 private static IntegerDefinition
[] createIntDefs(int size
, int bits
) {
95 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
96 for (int i
= 0; i
< size
; i
++) {
98 String content
= "test" + i
;
99 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(bits
, false,
100 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
, 24), null, content
);
106 private static StringDefinition
[] createDefs() {
108 StringDefinition
[] defs
= new StringDefinition
[size
];
109 for (int i
= 0; i
< size
; i
++) {
111 String content
= "test" + i
;
112 defs
[i
] = new StringDefinition(
113 new StringDeclaration(Encoding
.UTF8
), null, content
);
114 defs
[i
].setValue(content
);
120 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
124 public void testArrayDefinition_baseDeclaration() {
125 ArrayDeclaration declaration
= charArrayFixture
.getDeclaration();
126 String fieldName
= "";
128 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
, fieldName
);
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
= null;
141 String fieldName
= "";
143 ArrayDefinition result
= new ArrayDefinition(declaration
, definitionScope
, fieldName
);
144 assertNotNull(result
);
148 * Run the ArrayDeclaration getDeclaration() method test.
151 public void testGetDeclaration() {
152 charArrayFixture
.setDefinitions(new Definition
[] {});
153 ArrayDeclaration result
= charArrayFixture
.getDeclaration();
155 assertNotNull(result
);
159 * Run the Definition getElem(int) method test.
162 public void testGetElem_noDefs() {
164 Definition result
= charArrayFixture
.getElem(i
);
166 assertNotNull(result
);
170 * Run the Definition getElem(int) method test.
173 public void testGetElem_withDefs() {
174 Definition defs
[] = createDefs();
175 charArrayFixture
.setDefinitions(defs
);
178 Definition result
= charArrayFixture
.getElem(j
);
180 assertNotNull(result
);
184 * Run the boolean isString() method test.
187 public void testIsString_ownDefs() {
189 boolean result
= stringArrayFixture
.isString();
195 * Run the boolean isString() method test.
198 public void testIsString_complex() {
199 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
200 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, null, 8);
201 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
202 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
, "Testx");
206 IntegerDefinition
[] defs
= createIntDefs(size
, bits
);
208 ownFixture
.setDefinitions(defs
);
209 boolean result
= ownFixture
.isString();
215 * Run the boolean isString() method test.
218 public void testIsString_emptyDef() {
219 charArrayFixture
.setDefinitions(new Definition
[] {});
220 boolean result
= charArrayFixture
.isString();
226 * Run the boolean isString() method test.
229 public void testIsString_emptyDefStrDecl() {
230 ArrayDefinition ownFixture
= createStringArray();
231 boolean result
= ownFixture
.isString();
235 * Run the void read(BitBuffer) method test.
236 * @throws CTFReaderException error
239 public void testRead_noDefs() throws CTFReaderException
{
240 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
242 charArrayFixture
.read(input
);
246 * Run the void read(BitBuffer) method test.
247 * @throws CTFReaderException error
250 public void testRead_withDefs() throws CTFReaderException
{
251 charArrayFixture
.setDefinitions(new Definition
[] {});
252 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
254 charArrayFixture
.read(input
);
258 * Run the String toString() method test.
261 public void testToString_char() {
262 String result
= charArrayFixture
.toString();
263 assertNotNull(result
);
266 * Run the String toString() method test.
269 public void testToString_long() {
270 String result
= longArrayFixture
.toString();
271 assertNotNull(result
);
275 * Run the String toString() method test.
278 public void testToString_string() {
279 String result
= stringArrayFixture
.toString();
280 assertNotNull(result
);
283 * Run the String toString() method test.
286 public void testToString_withDefs() {
287 String result
= charArrayFixture
.toString();
289 assertNotNull(result
);
292 * Run the String toString() method test.
295 public void testToStringStringArray() {
296 String result
= stringArrayFixture
.toString();
298 assertNotNull(result
);