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
.assertEquals
;
15 import static org
.junit
.Assert
.assertFalse
;
16 import static org
.junit
.Assert
.assertNotEquals
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
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
.IntegerDeclaration
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
35 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.SequenceDeclaration
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * The class <code>SequenceDeclarationTest</code> contains tests for the class
41 * <code>{@link SequenceDeclaration}</code>.
44 * @version $Revision: 1.0 $
46 @SuppressWarnings("javadoc")
47 public class SequenceDeclaration2Test
{
50 private static final String FIELD_NAME
= "LengthName";
52 private SequenceDeclaration fixture
;
54 private BitBuffer input
= new BitBuffer();
58 fixture
= new SequenceDeclaration(FIELD_NAME
, StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
59 byte array
[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
60 ByteBuffer byb
= ByteBuffer
.wrap(array
);
61 input
= new BitBuffer(byb
);
65 * Run the SequenceDeclaration(String,Declaration) constructor test.
68 public void testSequenceDeclaration() {
69 String lengthName
= "";
70 IDeclaration elemType
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
72 SequenceDeclaration result
= new SequenceDeclaration(lengthName
, elemType
);
73 assertNotNull(result
);
74 String string
= "[declaration] sequence[";
75 assertEquals(string
, result
.toString().substring(0, string
.length()));
79 * Run the SequenceDefinition createDefinition(DefinitionScope,String)
82 * @throws CTFException
83 * an error in the bitbuffer
86 public void testCreateDefinition() throws CTFException
{
88 IntegerDeclaration id
= IntegerDeclaration
.createDeclaration(8, false, 8,
89 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, "", 32);
90 StructDeclaration structDec
= new StructDeclaration(0);
91 structDec
.addField(FIELD_NAME
, id
);
92 StructDefinition structDef
= new StructDefinition(
97 new IntegerDefinition(
103 AbstractArrayDefinition result
= fixture
.createDefinition(structDef
, FIELD_NAME
, input
);
104 assertNotNull(result
);
108 * Run the Declaration getElementType() method test.
111 public void testGetElementType() {
112 IDeclaration result
= fixture
.getElementType();
113 assertNotNull(result
);
117 * Run the String toString() method test.
120 public void testToString() {
121 String result
= fixture
.toString();
122 String left
= "[declaration] sequence[";
123 assertEquals(left
, result
.substring(0, left
.length()));
130 public void hashcodeTest() {
131 assertEquals(-1140774256, fixture
.hashCode());
132 SequenceDeclaration a
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
133 SequenceDeclaration b
= new SequenceDeclaration("Hello", IntegerDeclaration
.INT_32B_DECL
);
134 SequenceDeclaration c
= new SequenceDeclaration("Hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
135 SequenceDeclaration d
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
136 assertNotEquals(a
.hashCode(), b
.hashCode());
137 assertNotEquals(a
.hashCode(), c
.hashCode());
138 assertEquals(a
.hashCode(), d
.hashCode());
145 public void equalsTest() {
146 SequenceDeclaration a
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
147 SequenceDeclaration b
= new SequenceDeclaration("Hello", IntegerDeclaration
.INT_32B_DECL
);
148 SequenceDeclaration c
= new SequenceDeclaration("Hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
149 SequenceDeclaration d
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
150 assertNotEquals(a
, null);
151 assertNotEquals(a
, new Object());
152 assertNotEquals(a
, b
);
153 assertNotEquals(a
, c
);
155 assertNotEquals(b
, a
);
156 assertNotEquals(c
, a
);
159 assertFalse(a
.isBinaryEquivalent(b
));
160 assertFalse(a
.isBinaryEquivalent(c
));
161 assertTrue(a
.isBinaryEquivalent(d
));
162 assertFalse(b
.isBinaryEquivalent(a
));
163 assertFalse(c
.isBinaryEquivalent(a
));
164 assertTrue(d
.isBinaryEquivalent(a
));
165 assertTrue(a
.isBinaryEquivalent(a
));