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 | 22 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
d890ec37 | 23 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
f357bcd4 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; |
e00e6663 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; |
f357bcd4 AM |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; |
27 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
29 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
866e5b51 FC |
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>. | |
459ebacd | 36 | * |
866e5b51 FC |
37 | * @author ematkho |
38 | * @version $Revision: 1.0 $ | |
39 | */ | |
40 | public 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 MK |
77 | * |
78 | * @throws CTFReaderException | |
79 | * out of bounds | |
866e5b51 FC |
80 | */ |
81 | @Test | |
a4fa4e36 | 82 | public void testCreateDefinition() throws CTFReaderException { |
4a9c1f07 | 83 | String fieldName = ""; |
aefc5c83 | 84 | ByteBuffer allocate = ByteBuffer.allocate(100); |
e00e6663 | 85 | if (allocate == null) { |
aefc5c83 MK |
86 | throw new IllegalStateException("Failed to allocate memory"); |
87 | } | |
88 | BitBuffer bb = new BitBuffer(allocate); | |
a4fa4e36 | 89 | StructDefinition result = fixture.createDefinition(null, fieldName, bb); |
866e5b51 FC |
90 | assertNotNull(result); |
91 | } | |
92 | ||
93 | /** | |
2db699c2 | 94 | * Run the Declaration getField(String) method test. |
866e5b51 FC |
95 | */ |
96 | @Test | |
2db699c2 AM |
97 | public void testGetField() { |
98 | IDeclaration result = fixture.getField("test"); | |
866e5b51 | 99 | |
2db699c2 | 100 | assertNull(result); |
866e5b51 FC |
101 | } |
102 | ||
103 | /** | |
104 | * Run the List<String> getFieldsList() method test. | |
105 | */ | |
106 | @Test | |
107 | public void testGetFieldsList() { | |
a4fa4e36 | 108 | Iterable<String> result = fixture.getFieldsList(); |
866e5b51 FC |
109 | |
110 | assertNotNull(result); | |
a4fa4e36 | 111 | assertEquals(false, result.iterator().hasNext()); |
866e5b51 FC |
112 | } |
113 | ||
114 | /** | |
115 | * Run the long getMinAlign() method test. | |
116 | */ | |
117 | @Test | |
118 | public void testGetMinAlign() { | |
459ebacd | 119 | long result = fixture.getMaxAlign(); |
866e5b51 FC |
120 | assertEquals(1L, result); |
121 | } | |
122 | ||
123 | /** | |
124 | * Run the boolean hasField(String) method test. | |
125 | */ | |
126 | @Test | |
127 | public void testHasField() { | |
4a9c1f07 | 128 | String name = ""; |
866e5b51 FC |
129 | boolean result = fixture.hasField(name); |
130 | ||
131 | assertEquals(false, result); | |
132 | } | |
133 | ||
866e5b51 FC |
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 | ||
4a9c1f07 | 142 | assertEquals("[declaration] struct[", trunc); |
866e5b51 | 143 | } |
e00e6663 MK |
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); | |
e00e6663 MK |
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); | |
d890ec37 | 174 | c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 MK |
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); | |
d890ec37 MK |
184 | a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
185 | f.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); | |
186 | e.addField("hello", StringDeclaration.getStringDeclaration(Encoding.UTF8)); | |
e00e6663 MK |
187 | assertEquals(a, c); |
188 | assertEquals(c, a); | |
189 | assertNotEquals(a, d); | |
190 | d.addField("hi", IntegerDeclaration.INT_32B_DECL); | |
191 | assertNotEquals(a, d); | |
d890ec37 MK |
192 | a.addField("hello", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
193 | e.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); | |
e00e6663 MK |
194 | f.addField("hello", IntegerDeclaration.INT_32B_DECL); |
195 | assertNotEquals(a, e); | |
196 | assertNotEquals(a, f); | |
197 | } | |
866e5b51 | 198 | } |