ctf: Attempt to gracefully recover from a malformed packet context
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / SequenceDeclaration2Test.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.types;
13
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;
19
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22
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;
38
39 /**
40 * The class <code>SequenceDeclarationTest</code> contains tests for the class
41 * <code>{@link SequenceDeclaration}</code>.
42 *
43 * @author ematkho
44 * @version $Revision: 1.0 $
45 */
46 @SuppressWarnings("javadoc")
47 public class SequenceDeclaration2Test {
48
49 @NonNull
50 private static final String FIELD_NAME = "LengthName";
51
52 private SequenceDeclaration fixture;
53 @NonNull
54 private BitBuffer input = new BitBuffer();
55
56 @Before
57 public void setUp() {
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);
62 }
63
64 /**
65 * Run the SequenceDeclaration(String,Declaration) constructor test.
66 */
67 @Test
68 public void testSequenceDeclaration() {
69 String lengthName = "";
70 IDeclaration elemType = StringDeclaration.getStringDeclaration(Encoding.UTF8);
71
72 SequenceDeclaration result = new SequenceDeclaration(lengthName, elemType);
73 assertNotNull(result);
74 String string = "[declaration] sequence[";
75 assertEquals(string, result.toString().substring(0, string.length()));
76 }
77
78 /**
79 * Run the SequenceDefinition createDefinition(DefinitionScope,String)
80 * method test.
81 *
82 * @throws CTFException
83 * an error in the bitbuffer
84 */
85 @Test
86 public void testCreateDefinition() throws CTFException {
87 long seqLen = 2;
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(
93 structDec,
94 null,
95 "x",
96 new Definition[] {
97 new IntegerDefinition(
98 id,
99 null,
100 FIELD_NAME,
101 seqLen)
102 });
103 AbstractArrayDefinition result = fixture.createDefinition(structDef, FIELD_NAME, input);
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();
122 String left = "[declaration] sequence[";
123 assertEquals(left, result.substring(0, left.length()));
124 }
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);
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());
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);
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);
154 assertEquals(a, d);
155 assertNotEquals(b, a);
156 assertNotEquals(c, a);
157 assertEquals(d, a);
158 assertEquals(a, 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));
166 }
167
168 }
This page took 0.035018 seconds and 5 git commands to generate.