common: Annotate some methods in ByteBuffer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / StructDeclarationTest.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;
e00e6663 15import static org.junit.Assert.assertNotEquals;
866e5b51 16import static org.junit.Assert.assertNotNull;
2db699c2 17import static org.junit.Assert.assertNull;
866e5b51
FC
18import static org.junit.Assert.assertTrue;
19
a4fa4e36 20import java.nio.ByteBuffer;
866e5b51 21
680f9173 22import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4 23import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
d890ec37 24import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
f357bcd4 25import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
e00e6663 26import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
f357bcd4
AM
27import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
28import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
29import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
866e5b51
FC
30import org.junit.Before;
31import org.junit.Test;
32
33/**
34 * The class <code>StructDeclarationTest</code> contains tests for the class
35 * <code>{@link StructDeclaration}</code>.
459ebacd 36 *
866e5b51
FC
37 * @author ematkho
38 * @version $Revision: 1.0 $
39 */
40public class StructDeclarationTest {
41
42 private StructDeclaration fixture;
43
866e5b51
FC
44 /**
45 * Perform pre-test initialization.
46 */
47 @Before
48 public void setUp() {
49 fixture = new StructDeclaration(1L);
50 }
51
866e5b51
FC
52 /**
53 * Run the StructDeclaration(long) constructor test.
54 */
55 @Test
56 public void testStructDeclaration() {
57 assertNotNull(fixture);
459ebacd 58 assertEquals(1L, fixture.getMaxAlign());
866e5b51 59
66aa25f0 60 String regex = "^\\[declaration\\] struct\\[*.\\]$";
866e5b51
FC
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() {
4a9c1f07 69 String name = "";
d890ec37 70 IDeclaration declaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
866e5b51
FC
71 fixture.addField(name, declaration);
72 }
73
74 /**
75 * Run the StructDefinition createDefinition(DefinitionScope,String) method
76 * test.
a4fa4e36 77 *
680f9173 78 * @throws CTFException
a4fa4e36 79 * out of bounds
866e5b51
FC
80 */
81 @Test
680f9173 82 public void testCreateDefinition() throws CTFException {
4a9c1f07 83 String fieldName = "";
aefc5c83 84 ByteBuffer allocate = ByteBuffer.allocate(100);
aefc5c83 85 BitBuffer bb = new BitBuffer(allocate);
a4fa4e36 86 StructDefinition result = fixture.createDefinition(null, fieldName, bb);
866e5b51
FC
87 assertNotNull(result);
88 }
89
90 /**
2db699c2 91 * Run the Declaration getField(String) method test.
866e5b51
FC
92 */
93 @Test
2db699c2
AM
94 public void testGetField() {
95 IDeclaration result = fixture.getField("test");
866e5b51 96
2db699c2 97 assertNull(result);
866e5b51
FC
98 }
99
100 /**
101 * Run the List<String> getFieldsList() method test.
102 */
103 @Test
104 public void testGetFieldsList() {
a4fa4e36 105 Iterable<String> result = fixture.getFieldsList();
866e5b51
FC
106
107 assertNotNull(result);
a4fa4e36 108 assertEquals(false, result.iterator().hasNext());
866e5b51
FC
109 }
110
111 /**
112 * Run the long getMinAlign() method test.
113 */
114 @Test
115 public void testGetMinAlign() {
459ebacd 116 long result = fixture.getMaxAlign();
866e5b51
FC
117 assertEquals(1L, result);
118 }
119
120 /**
121 * Run the boolean hasField(String) method test.
122 */
123 @Test
124 public void testHasField() {
4a9c1f07 125 String name = "";
866e5b51
FC
126 boolean result = fixture.hasField(name);
127
128 assertEquals(false, result);
129 }
130
866e5b51
FC
131 /**
132 * Run the String toString() method test.
133 */
134 @Test
135 public void testToString() {
136 String result = fixture.toString();
137 String trunc = result.substring(0, 21);
138
4a9c1f07 139 assertEquals("[declaration] struct[", trunc);
866e5b51 140 }
e00e6663
MK
141
142 /**
143 * Test the hashcode
144 */
145 @Test
146 public void hashcodeTest() {
147 assertEquals(32, fixture.hashCode());
148 StructDeclaration a = new StructDeclaration(8);
149 fixture.addField("hello", a);
150 a.addField("Time", IntegerDeclaration.INT_32B_DECL);
e00e6663
MK
151 StructDeclaration b = new StructDeclaration(8);
152 StructDeclaration c = new StructDeclaration(8);
153 b.addField("hello", c);
154 c.addField("Time", IntegerDeclaration.INT_32B_DECL);
155 assertEquals(b.hashCode(), fixture.hashCode());
156 c.addField("Space", IntegerDeclaration.INT_32L_DECL);
157 assertNotEquals(b.hashCode(), fixture.hashCode());
158 }
159
160 /**
161 * Test the equals
162 */
163 @Test
164 public void equalsTest() {
165 StructDeclaration a = new StructDeclaration(8);
166 StructDeclaration b = new StructDeclaration(16);
167 StructDeclaration c = new StructDeclaration(8);
168 StructDeclaration d = new StructDeclaration(8);
169 StructDeclaration e = new StructDeclaration(8);
170 StructDeclaration f = new StructDeclaration(8);
d890ec37 171 c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
172 assertNotEquals(a, null);
173 assertNotEquals(a, new Object());
174 assertNotEquals(a, b);
175 assertNotEquals(a, c);
176 assertEquals(a, d);
177 assertNotEquals(b, a);
178 assertNotEquals(c, a);
179 assertEquals(d, a);
180 assertEquals(a, a);
d890ec37
MK
181 a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
182 f.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
183 e.addField("hello", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
184 assertEquals(a, c);
185 assertEquals(c, a);
186 assertNotEquals(a, d);
187 d.addField("hi", IntegerDeclaration.INT_32B_DECL);
188 assertNotEquals(a, d);
d890ec37
MK
189 a.addField("hello", StringDeclaration.getStringDeclaration(Encoding.UTF8));
190 e.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
191 f.addField("hello", IntegerDeclaration.INT_32B_DECL);
192 assertNotEquals(a, e);
193 assertNotEquals(a, f);
194 }
866e5b51 195}
This page took 0.07131 seconds and 5 git commands to generate.