common: Annotate some methods in ByteBuffer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / SequenceDeclaration2Test.java
CommitLineData
4bd7f2db 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
f357bcd4 12package org.eclipse.tracecompass.ctf.core.tests.types;
866e5b51
FC
13
14import static org.junit.Assert.assertEquals;
f9d7c59f 15import static org.junit.Assert.assertFalse;
e00e6663 16import static org.junit.Assert.assertNotEquals;
866e5b51 17import static org.junit.Assert.assertNotNull;
f9d7c59f 18import static org.junit.Assert.assertTrue;
866e5b51 19
a4fa4e36 20import java.nio.ByteBuffer;
866e5b51
FC
21import java.nio.ByteOrder;
22
a4fa4e36 23import org.eclipse.jdt.annotation.NonNull;
680f9173 24import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
25import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
26import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
27import org.eclipse.tracecompass.ctf.core.event.types.Definition;
28import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
29import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
30import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
31import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
32import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
33import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
34import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
f357bcd4 35import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration;
866e5b51
FC
36import org.junit.Before;
37import org.junit.Test;
38
39/**
40 * The class <code>SequenceDeclarationTest</code> contains tests for the class
41 * <code>{@link SequenceDeclaration}</code>.
284fdee8 42 *
866e5b51
FC
43 * @author ematkho
44 * @version $Revision: 1.0 $
45 */
be6df2d8 46@SuppressWarnings("javadoc")
7b4f13e6 47public class SequenceDeclaration2Test {
866e5b51 48
e00e6663
MK
49 @NonNull
50 private static final String FIELD_NAME = "LengthName";
866e5b51 51
a4fa4e36 52 private SequenceDeclaration fixture;
e00e6663
MK
53 @NonNull
54 private BitBuffer input = new BitBuffer();
866e5b51
FC
55
56 @Before
57 public void setUp() {
d890ec37 58 fixture = new SequenceDeclaration(FIELD_NAME, StringDeclaration.getStringDeclaration(Encoding.UTF8));
a4fa4e36
MK
59 byte array[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
60 ByteBuffer byb = ByteBuffer.wrap(array);
61 input = new BitBuffer(byb);
866e5b51
FC
62 }
63
866e5b51
FC
64 /**
65 * Run the SequenceDeclaration(String,Declaration) constructor test.
66 */
67 @Test
68 public void testSequenceDeclaration() {
4a9c1f07 69 String lengthName = "";
d890ec37 70 IDeclaration elemType = StringDeclaration.getStringDeclaration(Encoding.UTF8);
866e5b51 71
4a9c1f07 72 SequenceDeclaration result = new SequenceDeclaration(lengthName, elemType);
866e5b51 73 assertNotNull(result);
4a9c1f07 74 String string = "[declaration] sequence[";
866e5b51
FC
75 assertEquals(string, result.toString().substring(0, string.length()));
76 }
77
78 /**
79 * Run the SequenceDefinition createDefinition(DefinitionScope,String)
80 * method test.
a4fa4e36 81 *
680f9173 82 * @throws CTFException
a4fa4e36 83 * an error in the bitbuffer
866e5b51
FC
84 */
85 @Test
680f9173 86 public void testCreateDefinition() throws CTFException {
a4fa4e36
MK
87 long seqLen = 2;
88 IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8,
89 ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 32);
866e5b51 90 StructDeclaration structDec = new StructDeclaration(0);
a4fa4e36
MK
91 structDec.addField(FIELD_NAME, id);
92 StructDefinition structDef = new StructDefinition(
93 structDec,
94 null,
95 "x",
a4fa4e36
MK
96 new Definition[] {
97 new IntegerDefinition(
98 id,
99 null,
100 FIELD_NAME,
101 seqLen)
102 });
7b4f13e6 103 AbstractArrayDefinition result = fixture.createDefinition(structDef, FIELD_NAME, input);
866e5b51
FC
104 assertNotNull(result);
105 }
106
107 /**
108 * Run the Declaration getElementType() method test.
109 */
110 @Test
111 public void testGetElementType() {
112 IDeclaration result = fixture.getElementType();
113 assertNotNull(result);
114 }
115
116 /**
117 * Run the String toString() method test.
118 */
119 @Test
120 public void testToString() {
121 String result = fixture.toString();
4a9c1f07 122 String left = "[declaration] sequence[";
866e5b51
FC
123 assertEquals(left, result.substring(0, left.length()));
124 }
e00e6663
MK
125
126 /**
127 * Test the hashcode
128 */
129 @Test
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);
d890ec37 134 SequenceDeclaration c = new SequenceDeclaration("Hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
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());
139 }
140
141 /**
142 * Test the equals
143 */
144 @Test
145 public void equalsTest() {
146 SequenceDeclaration a = new SequenceDeclaration("Hi", IntegerDeclaration.INT_32B_DECL);
147 SequenceDeclaration b = new SequenceDeclaration("Hello", IntegerDeclaration.INT_32B_DECL);
d890ec37 148 SequenceDeclaration c = new SequenceDeclaration("Hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
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);
154 assertEquals(a, d);
155 assertNotEquals(b, a);
156 assertNotEquals(c, a);
157 assertEquals(d, a);
158 assertEquals(a, a);
f9d7c59f
MK
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));
e00e6663
MK
166 }
167
866e5b51 168}
This page took 0.071574 seconds and 5 git commands to generate.