ctf: rename CTFReaderException to CTFException
[deliverable/tracecompass.git] / 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 import com.google.common.collect.ImmutableList;
40
41 /**
42 * The class <code>SequenceDeclarationTest</code> contains tests for the class
43 * <code>{@link SequenceDeclaration}</code>.
44 *
45 * @author ematkho
46 * @version $Revision: 1.0 $
47 */
48 @SuppressWarnings("javadoc")
49 public class SequenceDeclaration2Test {
50
51 @NonNull
52 private static final String FIELD_NAME = "LengthName";
53
54 private SequenceDeclaration fixture;
55 @NonNull
56 private BitBuffer input = new BitBuffer();
57
58 @Before
59 public void setUp() {
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);
63 if (byb == null) {
64 throw new IllegalStateException("Failed to allocate memory");
65 }
66 input = new BitBuffer(byb);
67 }
68
69 /**
70 * Run the SequenceDeclaration(String,Declaration) constructor test.
71 */
72 @Test
73 public void testSequenceDeclaration() {
74 String lengthName = "";
75 IDeclaration elemType = StringDeclaration.getStringDeclaration(Encoding.UTF8);
76
77 SequenceDeclaration result = new SequenceDeclaration(lengthName, elemType);
78 assertNotNull(result);
79 String string = "[declaration] sequence[";
80 assertEquals(string, result.toString().substring(0, string.length()));
81 }
82
83 /**
84 * Run the SequenceDefinition createDefinition(DefinitionScope,String)
85 * method test.
86 *
87 * @throws CTFException
88 * an error in the bitbuffer
89 */
90 @Test
91 public void testCreateDefinition() throws CTFException {
92 long seqLen = 2;
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(
98 structDec,
99 null,
100 "x",
101 ImmutableList.of(FIELD_NAME),
102 new Definition[] {
103 new IntegerDefinition(
104 id,
105 null,
106 FIELD_NAME,
107 seqLen)
108 });
109 AbstractArrayDefinition result = fixture.createDefinition(structDef, FIELD_NAME, input);
110 assertNotNull(result);
111 }
112
113 /**
114 * Run the Declaration getElementType() method test.
115 */
116 @Test
117 public void testGetElementType() {
118 IDeclaration result = fixture.getElementType();
119 assertNotNull(result);
120 }
121
122 /**
123 * Run the String toString() method test.
124 */
125 @Test
126 public void testToString() {
127 String result = fixture.toString();
128 String left = "[declaration] sequence[";
129 assertEquals(left, result.substring(0, left.length()));
130 }
131
132 /**
133 * Test the hashcode
134 */
135 @Test
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());
145 }
146
147 /**
148 * Test the equals
149 */
150 @Test
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);
160 assertEquals(a, d);
161 assertNotEquals(b, a);
162 assertNotEquals(c, a);
163 assertEquals(d, a);
164 assertEquals(a, 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));
172 }
173
174 }
This page took 0.04268 seconds and 5 git commands to generate.