ce8d5149f673ef0c11f231b8a519958d84d6883a
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / StructDeclarationTest.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.assertNotEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19
20 import java.nio.ByteBuffer;
21
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;
32
33 /**
34 * The class <code>StructDeclarationTest</code> contains tests for the class
35 * <code>{@link StructDeclaration}</code>.
36 *
37 * @author ematkho
38 * @version $Revision: 1.0 $
39 */
40 public class StructDeclarationTest {
41
42 private StructDeclaration fixture;
43
44 /**
45 * Perform pre-test initialization.
46 */
47 @Before
48 public void setUp() {
49 fixture = new StructDeclaration(1L);
50 }
51
52 /**
53 * Run the StructDeclaration(long) constructor test.
54 */
55 @Test
56 public void testStructDeclaration() {
57 assertNotNull(fixture);
58 assertEquals(1L, fixture.getMaxAlign());
59
60 String regex = "^\\[declaration\\] struct\\[*.\\]$";
61 assertTrue(fixture.toString().matches(regex));
62 }
63
64 /**
65 * Run the void addField(String,Declaration) method test.
66 */
67 @Test
68 public void testAddField() {
69 String name = "";
70 IDeclaration declaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
71 fixture.addField(name, declaration);
72 }
73
74 /**
75 * Run the StructDefinition createDefinition(DefinitionScope,String) method
76 * test.
77 *
78 * @throws CTFException
79 * out of bounds
80 */
81 @Test
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");
87 }
88 BitBuffer bb = new BitBuffer(allocate);
89 StructDefinition result = fixture.createDefinition(null, fieldName, bb);
90 assertNotNull(result);
91 }
92
93 /**
94 * Run the Declaration getField(String) method test.
95 */
96 @Test
97 public void testGetField() {
98 IDeclaration result = fixture.getField("test");
99
100 assertNull(result);
101 }
102
103 /**
104 * Run the List<String> getFieldsList() method test.
105 */
106 @Test
107 public void testGetFieldsList() {
108 Iterable<String> result = fixture.getFieldsList();
109
110 assertNotNull(result);
111 assertEquals(false, result.iterator().hasNext());
112 }
113
114 /**
115 * Run the long getMinAlign() method test.
116 */
117 @Test
118 public void testGetMinAlign() {
119 long result = fixture.getMaxAlign();
120 assertEquals(1L, result);
121 }
122
123 /**
124 * Run the boolean hasField(String) method test.
125 */
126 @Test
127 public void testHasField() {
128 String name = "";
129 boolean result = fixture.hasField(name);
130
131 assertEquals(false, result);
132 }
133
134 /**
135 * Run the String toString() method test.
136 */
137 @Test
138 public void testToString() {
139 String result = fixture.toString();
140 String trunc = result.substring(0, 21);
141
142 assertEquals("[declaration] struct[", trunc);
143 }
144
145 /**
146 * Test the hashcode
147 */
148 @Test
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());
161 }
162
163 /**
164 * Test the equals
165 */
166 @Test
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);
179 assertEquals(a, d);
180 assertNotEquals(b, a);
181 assertNotEquals(c, a);
182 assertEquals(d, a);
183 assertEquals(a, 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));
187 assertEquals(a, c);
188 assertEquals(c, a);
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);
197 }
198 }
This page took 0.034848 seconds and 4 git commands to generate.