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
.assertNotEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertNull
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import java
.nio
.ByteBuffer
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
30 import org
.junit
.Before
;
31 import org
.junit
.Test
;
34 * The class <code>StructDeclarationTest</code> contains tests for the class
35 * <code>{@link StructDeclaration}</code>.
38 * @version $Revision: 1.0 $
40 public class StructDeclarationTest
{
42 private StructDeclaration fixture
;
45 * Perform pre-test initialization.
49 fixture
= new StructDeclaration(1L);
53 * Run the StructDeclaration(long) constructor test.
56 public void testStructDeclaration() {
57 assertNotNull(fixture
);
58 assertEquals(1L, fixture
.getMaxAlign());
60 String regex
= "^\\[declaration\\] struct\\[*.\\]$";
61 assertTrue(fixture
.toString().matches(regex
));
65 * Run the void addField(String,Declaration) method test.
68 public void testAddField() {
70 IDeclaration declaration
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
71 fixture
.addField(name
, declaration
);
75 * Run the StructDefinition createDefinition(DefinitionScope,String) method
78 * @throws CTFException
82 public void testCreateDefinition() throws CTFException
{
83 String fieldName
= "";
84 ByteBuffer allocate
= ByteBuffer
.allocate(100);
85 if (allocate
== null) {
86 throw new IllegalStateException("Failed to allocate memory");
88 BitBuffer bb
= new BitBuffer(allocate
);
89 StructDefinition result
= fixture
.createDefinition(null, fieldName
, bb
);
90 assertNotNull(result
);
94 * Run the Declaration getField(String) method test.
97 public void testGetField() {
98 IDeclaration result
= fixture
.getField("test");
104 * Run the List<String> getFieldsList() method test.
107 public void testGetFieldsList() {
108 Iterable
<String
> result
= fixture
.getFieldsList();
110 assertNotNull(result
);
111 assertEquals(false, result
.iterator().hasNext());
115 * Run the long getMinAlign() method test.
118 public void testGetMinAlign() {
119 long result
= fixture
.getMaxAlign();
120 assertEquals(1L, result
);
124 * Run the boolean hasField(String) method test.
127 public void testHasField() {
129 boolean result
= fixture
.hasField(name
);
131 assertEquals(false, result
);
135 * Run the String toString() method test.
138 public void testToString() {
139 String result
= fixture
.toString();
140 String trunc
= result
.substring(0, 21);
142 assertEquals("[declaration] struct[", trunc
);
149 public void hashcodeTest() {
150 assertEquals(32, fixture
.hashCode());
151 StructDeclaration a
= new StructDeclaration(8);
152 fixture
.addField("hello", a
);
153 a
.addField("Time", IntegerDeclaration
.INT_32B_DECL
);
154 StructDeclaration b
= new StructDeclaration(8);
155 StructDeclaration c
= new StructDeclaration(8);
156 b
.addField("hello", c
);
157 c
.addField("Time", IntegerDeclaration
.INT_32B_DECL
);
158 assertEquals(b
.hashCode(), fixture
.hashCode());
159 c
.addField("Space", IntegerDeclaration
.INT_32L_DECL
);
160 assertNotEquals(b
.hashCode(), fixture
.hashCode());
167 public void equalsTest() {
168 StructDeclaration a
= new StructDeclaration(8);
169 StructDeclaration b
= new StructDeclaration(16);
170 StructDeclaration c
= new StructDeclaration(8);
171 StructDeclaration d
= new StructDeclaration(8);
172 StructDeclaration e
= new StructDeclaration(8);
173 StructDeclaration f
= new StructDeclaration(8);
174 c
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
175 assertNotEquals(a
, null);
176 assertNotEquals(a
, new Object());
177 assertNotEquals(a
, b
);
178 assertNotEquals(a
, c
);
180 assertNotEquals(b
, a
);
181 assertNotEquals(c
, a
);
184 a
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
185 f
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
186 e
.addField("hello", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
189 assertNotEquals(a
, d
);
190 d
.addField("hi", IntegerDeclaration
.INT_32B_DECL
);
191 assertNotEquals(a
, d
);
192 a
.addField("hello", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
193 e
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
194 f
.addField("hello", IntegerDeclaration
.INT_32B_DECL
);
195 assertNotEquals(a
, e
);
196 assertNotEquals(a
, f
);