1 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
3 import static org
.junit
.Assert
.assertFalse
;
4 import static org
.junit
.Assert
.assertNotNull
;
5 import static org
.junit
.Assert
.assertTrue
;
7 import java
.nio
.ByteBuffer
;
8 import java
.nio
.ByteOrder
;
10 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
11 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
12 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
13 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
14 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
22 import org
.junit
.After
;
23 import org
.junit
.Before
;
24 import org
.junit
.Test
;
27 * The class <code>ArrayDefinitionTest</code> contains tests for the class
28 * <code>{@link ArrayDefinition}</code>.
31 * @version $Revision: 1.0 $
33 public class ArrayDefinitionTest
{
35 private CTFTrace trace
;
36 private ArrayDefinition charArrayFixture
;
37 private ArrayDefinition stringArrayFixture
;
38 private ArrayDefinition longArrayFixture
;
44 * the command line arguments
46 public static void main(String
[] args
) {
47 new org
.junit
.runner
.JUnitCore().run(ArrayDefinitionTest
.class);
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); //$NON-NLS-1$
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); //$NON-NLS-1$
72 IntegerDefinition
[] defs
= createIntDefs(4,8);
73 ArrayDefinition temp
= setUpDeclaration(decl
, defs
);
81 private ArrayDefinition
createStringArray() {
82 StringDeclaration strDecl
= new StringDeclaration();
83 StringDefinition
[] defs
= createDefs();
84 ArrayDefinition temp
= setUpDeclaration(strDecl
, defs
);
92 private ArrayDefinition
setUpDeclaration(IDeclaration decl
,
94 ArrayDeclaration ad
= new ArrayDeclaration(0, decl
);
95 ArrayDefinition temp
= new ArrayDefinition(ad
, this.trace
, "Testx"); //$NON-NLS-1$
96 temp
.setDefinitions(defs
);
104 private static IntegerDefinition
[] createIntDefs(int size
, int bits
) {
105 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
106 for (int i
= 0; i
< size
; i
++) {
108 String content
= "test" + i
; //$NON-NLS-1$
109 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(bits
, false,
110 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
, 24), null, content
);
116 * Perform post-test clean-up.
119 public void tearDown() {
120 // Add additional tear down code here
123 private static StringDefinition
[] createDefs() {
125 StringDefinition
[] defs
= new StringDefinition
[size
];
126 for (int i
= 0; i
< size
; i
++) {
128 String content
= "test" + i
; //$NON-NLS-1$
129 defs
[i
] = new StringDefinition(
130 new StringDeclaration(Encoding
.UTF8
), null, content
);
131 defs
[i
].setString(new StringBuilder(content
));
137 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
141 public void testArrayDefinition_baseDeclaration() {
142 ArrayDeclaration declaration
= charArrayFixture
.getDeclaration();
143 String fieldName
= ""; //$NON-NLS-1$
145 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
,
148 assertNotNull(result
);
152 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
156 public void testArrayDefinition_newDeclaration() {
157 ArrayDeclaration declaration
= new ArrayDeclaration(0,
158 new StringDeclaration());
159 IDefinitionScope definitionScope
= null;
160 String fieldName
= ""; //$NON-NLS-1$
162 ArrayDefinition result
= new ArrayDefinition(declaration
,
163 definitionScope
, fieldName
);
165 assertNotNull(result
);
169 * Run the ArrayDeclaration getDeclaration() method test.
172 public void testGetDeclaration() {
173 charArrayFixture
.setDefinitions(new Definition
[] {});
174 ArrayDeclaration result
= charArrayFixture
.getDeclaration();
176 assertNotNull(result
);
180 * Run the Definition getElem(int) method test.
183 public void testGetElem_noDefs() {
185 Definition result
= charArrayFixture
.getElem(i
);
187 assertNotNull(result
);
191 * Run the Definition getElem(int) method test.
194 public void testGetElem_withDefs() {
195 Definition defs
[] = createDefs();
196 charArrayFixture
.setDefinitions(defs
);
199 Definition result
= charArrayFixture
.getElem(j
);
201 assertNotNull(result
);
205 * Run the boolean isString() method test.
208 public void testIsString_ownDefs() {
210 boolean result
= stringArrayFixture
.isString();
218 * Run the boolean isString() method test.
221 public void testIsString_complex() {
222 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
223 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, null, 8);
224 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
225 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
,
226 "Testx"); //$NON-NLS-1$
230 IntegerDefinition
[] defs
= createIntDefs(size
, bits
);
232 ownFixture
.setDefinitions(defs
);
233 boolean result
= ownFixture
.isString();
241 * Run the boolean isString() method test.
244 public void testIsString_emptyDef() {
245 charArrayFixture
.setDefinitions(new Definition
[] {});
246 boolean result
= charArrayFixture
.isString();
252 * Run the boolean isString() method test.
255 public void testIsString_emptyDefStrDecl() {
256 ArrayDefinition ownFixture
= createStringArray();
257 boolean result
= ownFixture
.isString();
261 * Run the void read(BitBuffer) method test.
264 public void testRead_noDefs() {
265 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
267 charArrayFixture
.read(input
);
271 * Run the void read(BitBuffer) method test.
274 public void testRead_withDefs() {
275 charArrayFixture
.setDefinitions(new Definition
[] {});
276 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
278 charArrayFixture
.read(input
);
282 * Run the String toString() method test.
285 public void testToString_char() {
286 String result
= charArrayFixture
.toString();
287 assertNotNull(result
);
290 * Run the String toString() method test.
293 public void testToString_long() {
294 String result
= longArrayFixture
.toString();
295 assertNotNull(result
);
299 * Run the String toString() method test.
302 public void testToString_string() {
303 String result
= stringArrayFixture
.toString();
304 assertNotNull(result
);
307 * Run the String toString() method test.
310 public void testToString_withDefs() {
311 String result
= charArrayFixture
.toString();
313 assertNotNull(result
);
316 * Run the String toString() method test.
319 public void testToStringStringArray() {
320 String result
= stringArrayFixture
.toString();
322 assertNotNull(result
);