Commit | Line | Data |
---|---|---|
4bd7f2db AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 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 | ||
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 FC |
13 | |
14 | import static org.junit.Assert.assertEquals; | |
e00e6663 | 15 | import static org.junit.Assert.assertNotEquals; |
866e5b51 | 16 | import static org.junit.Assert.assertNotNull; |
2db699c2 | 17 | import static org.junit.Assert.assertNull; |
866e5b51 FC |
18 | import static org.junit.Assert.assertTrue; |
19 | ||
a4fa4e36 | 20 | import java.nio.ByteBuffer; |
866e5b51 | 21 | |
f357bcd4 AM |
22 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
23 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
e00e6663 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; |
f357bcd4 AM |
25 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
28 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
866e5b51 FC |
29 | import org.junit.Before; |
30 | import org.junit.Test; | |
31 | ||
32 | /** | |
33 | * The class <code>StructDeclarationTest</code> contains tests for the class | |
34 | * <code>{@link StructDeclaration}</code>. | |
459ebacd | 35 | * |
866e5b51 FC |
36 | * @author ematkho |
37 | * @version $Revision: 1.0 $ | |
38 | */ | |
39 | public class StructDeclarationTest { | |
40 | ||
41 | private StructDeclaration fixture; | |
42 | ||
866e5b51 FC |
43 | /** |
44 | * Perform pre-test initialization. | |
45 | */ | |
46 | @Before | |
47 | public void setUp() { | |
48 | fixture = new StructDeclaration(1L); | |
49 | } | |
50 | ||
866e5b51 FC |
51 | /** |
52 | * Run the StructDeclaration(long) constructor test. | |
53 | */ | |
54 | @Test | |
55 | public void testStructDeclaration() { | |
56 | assertNotNull(fixture); | |
459ebacd | 57 | assertEquals(1L, fixture.getMaxAlign()); |
866e5b51 | 58 | |
4a9c1f07 | 59 | String regex = "^\\[declaration\\] struct\\[[0-9a-f]{1,8}\\]$"; |
866e5b51 FC |
60 | assertTrue(fixture.toString().matches(regex)); |
61 | } | |
62 | ||
63 | /** | |
64 | * Run the void addField(String,Declaration) method test. | |
65 | */ | |
66 | @Test | |
67 | public void testAddField() { | |
4a9c1f07 | 68 | String name = ""; |
866e5b51 FC |
69 | IDeclaration declaration = new StringDeclaration(); |
70 | fixture.addField(name, declaration); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Run the StructDefinition createDefinition(DefinitionScope,String) method | |
75 | * test. | |
a4fa4e36 MK |
76 | * |
77 | * @throws CTFReaderException | |
78 | * out of bounds | |
866e5b51 FC |
79 | */ |
80 | @Test | |
a4fa4e36 | 81 | public void testCreateDefinition() throws CTFReaderException { |
4a9c1f07 | 82 | String fieldName = ""; |
aefc5c83 | 83 | ByteBuffer allocate = ByteBuffer.allocate(100); |
e00e6663 | 84 | if (allocate == null) { |
aefc5c83 MK |
85 | throw new IllegalStateException("Failed to allocate memory"); |
86 | } | |
87 | BitBuffer bb = new BitBuffer(allocate); | |
a4fa4e36 | 88 | StructDefinition result = fixture.createDefinition(null, fieldName, bb); |
866e5b51 FC |
89 | assertNotNull(result); |
90 | } | |
91 | ||
92 | /** | |
2db699c2 | 93 | * Run the Declaration getField(String) method test. |
866e5b51 FC |
94 | */ |
95 | @Test | |
2db699c2 AM |
96 | public void testGetField() { |
97 | IDeclaration result = fixture.getField("test"); | |
866e5b51 | 98 | |
2db699c2 | 99 | assertNull(result); |
866e5b51 FC |
100 | } |
101 | ||
102 | /** | |
103 | * Run the List<String> getFieldsList() method test. | |
104 | */ | |
105 | @Test | |
106 | public void testGetFieldsList() { | |
a4fa4e36 | 107 | Iterable<String> result = fixture.getFieldsList(); |
866e5b51 FC |
108 | |
109 | assertNotNull(result); | |
a4fa4e36 | 110 | assertEquals(false, result.iterator().hasNext()); |
866e5b51 FC |
111 | } |
112 | ||
113 | /** | |
114 | * Run the long getMinAlign() method test. | |
115 | */ | |
116 | @Test | |
117 | public void testGetMinAlign() { | |
459ebacd | 118 | long result = fixture.getMaxAlign(); |
866e5b51 FC |
119 | assertEquals(1L, result); |
120 | } | |
121 | ||
122 | /** | |
123 | * Run the boolean hasField(String) method test. | |
124 | */ | |
125 | @Test | |
126 | public void testHasField() { | |
4a9c1f07 | 127 | String name = ""; |
866e5b51 FC |
128 | boolean result = fixture.hasField(name); |
129 | ||
130 | assertEquals(false, result); | |
131 | } | |
132 | ||
866e5b51 FC |
133 | /** |
134 | * Run the String toString() method test. | |
135 | */ | |
136 | @Test | |
137 | public void testToString() { | |
138 | String result = fixture.toString(); | |
139 | String trunc = result.substring(0, 21); | |
140 | ||
4a9c1f07 | 141 | assertEquals("[declaration] struct[", trunc); |
866e5b51 | 142 | } |
e00e6663 MK |
143 | |
144 | /** | |
145 | * Test the hashcode | |
146 | */ | |
147 | @Test | |
148 | public void hashcodeTest() { | |
149 | assertEquals(32, fixture.hashCode()); | |
150 | StructDeclaration a = new StructDeclaration(8); | |
151 | fixture.addField("hello", a); | |
152 | a.addField("Time", IntegerDeclaration.INT_32B_DECL); | |
153 | assertEquals(-864123628, fixture.hashCode()); | |
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", new StringDeclaration()); | |
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", new StringDeclaration()); | |
185 | f.addField("hi", new StringDeclaration()); | |
186 | e.addField("hello", new StringDeclaration()); | |
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", new StringDeclaration()); | |
193 | e.addField("hi", new StringDeclaration()); | |
194 | f.addField("hello", IntegerDeclaration.INT_32B_DECL); | |
195 | assertNotEquals(a, e); | |
196 | assertNotEquals(a, f); | |
197 | } | |
866e5b51 | 198 | } |