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
;
39 import com
.google
.common
.collect
.ImmutableList
;
42 * The class <code>SequenceDeclarationTest</code> contains tests for the class
43 * <code>{@link SequenceDeclaration}</code>.
46 * @version $Revision: 1.0 $
48 @SuppressWarnings("javadoc")
49 public class SequenceDeclaration2Test
{
52 private static final String FIELD_NAME
= "LengthName";
54 private SequenceDeclaration fixture
;
56 private BitBuffer input
= new BitBuffer();
60 fixture
= new SequenceDeclaration(FIELD_NAME
, StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
61 byte array
[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
62 ByteBuffer byb
= ByteBuffer
.wrap(array
);
64 throw new IllegalStateException("Failed to allocate memory");
66 input
= new BitBuffer(byb
);
70 * Run the SequenceDeclaration(String,Declaration) constructor test.
73 public void testSequenceDeclaration() {
74 String lengthName
= "";
75 IDeclaration elemType
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
77 SequenceDeclaration result
= new SequenceDeclaration(lengthName
, elemType
);
78 assertNotNull(result
);
79 String string
= "[declaration] sequence[";
80 assertEquals(string
, result
.toString().substring(0, string
.length()));
84 * Run the SequenceDefinition createDefinition(DefinitionScope,String)
87 * @throws CTFException
88 * an error in the bitbuffer
91 public void testCreateDefinition() throws CTFException
{
93 IntegerDeclaration id
= IntegerDeclaration
.createDeclaration(8, false, 8,
94 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, "", 32);
95 StructDeclaration structDec
= new StructDeclaration(0);
96 structDec
.addField(FIELD_NAME
, id
);
97 StructDefinition structDef
= new StructDefinition(
101 ImmutableList
.of(FIELD_NAME
),
103 new IntegerDefinition(
109 AbstractArrayDefinition result
= fixture
.createDefinition(structDef
, FIELD_NAME
, input
);
110 assertNotNull(result
);
114 * Run the Declaration getElementType() method test.
117 public void testGetElementType() {
118 IDeclaration result
= fixture
.getElementType();
119 assertNotNull(result
);
123 * Run the String toString() method test.
126 public void testToString() {
127 String result
= fixture
.toString();
128 String left
= "[declaration] sequence[";
129 assertEquals(left
, result
.substring(0, left
.length()));
136 public void hashcodeTest() {
137 assertEquals(-1140774256, fixture
.hashCode());
138 SequenceDeclaration a
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
139 SequenceDeclaration b
= new SequenceDeclaration("Hello", IntegerDeclaration
.INT_32B_DECL
);
140 SequenceDeclaration c
= new SequenceDeclaration("Hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
141 SequenceDeclaration d
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
142 assertNotEquals(a
.hashCode(), b
.hashCode());
143 assertNotEquals(a
.hashCode(), c
.hashCode());
144 assertEquals(a
.hashCode(), d
.hashCode());
151 public void equalsTest() {
152 SequenceDeclaration a
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
153 SequenceDeclaration b
= new SequenceDeclaration("Hello", IntegerDeclaration
.INT_32B_DECL
);
154 SequenceDeclaration c
= new SequenceDeclaration("Hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
155 SequenceDeclaration d
= new SequenceDeclaration("Hi", IntegerDeclaration
.INT_32B_DECL
);
156 assertNotEquals(a
, null);
157 assertNotEquals(a
, new Object());
158 assertNotEquals(a
, b
);
159 assertNotEquals(a
, c
);
161 assertNotEquals(b
, a
);
162 assertNotEquals(c
, a
);
165 assertFalse(a
.isBinaryEquivalent(b
));
166 assertFalse(a
.isBinaryEquivalent(c
));
167 assertTrue(a
.isBinaryEquivalent(d
));
168 assertFalse(b
.isBinaryEquivalent(a
));
169 assertFalse(c
.isBinaryEquivalent(a
));
170 assertTrue(d
.isBinaryEquivalent(a
));
171 assertTrue(a
.isBinaryEquivalent(a
));