Fix bug when tracefile is not aligned. Now supports exotic architectures.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / ArrayDefinitionTest.java
CommitLineData
866e5b51
FC
1package org.eclipse.linuxtools.ctf.core.tests.types;
2
3import static org.junit.Assert.assertFalse;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertTrue;
6
7import java.nio.ByteBuffer;
8import java.nio.ByteOrder;
9
866e5b51
FC
10import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
11import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
12import org.eclipse.linuxtools.ctf.core.event.types.Definition;
13import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
ce2388e0 14import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
866e5b51
FC
15import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
16import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
17import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
18import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
19import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
866e5b51 20import org.eclipse.linuxtools.ctf.core.tests.TestParams;
13be1a8f 21import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
866e5b51 22import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
ce2388e0 23import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
866e5b51
FC
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27
28/**
29 * The class <code>ArrayDefinitionTest</code> contains tests for the class
30 * <code>{@link ArrayDefinition}</code>.
284fdee8 31 *
866e5b51
FC
32 * @author ematkho
33 * @version $Revision: 1.0 $
34 */
35public class ArrayDefinitionTest {
36
37 private CTFTrace trace;
ce2388e0
FC
38 private ArrayDefinition charArrayFixture;
39 private ArrayDefinition stringArrayFixture;
40 private ArrayDefinition longArrayFixture;
866e5b51
FC
41
42 /**
43 * Launch the test.
284fdee8 44 *
866e5b51
FC
45 * @param args
46 * the command line arguments
47 */
48 public static void main(String[] args) {
49 new org.junit.runner.JUnitCore().run(ArrayDefinitionTest.class);
50 }
51
52 /**
53 * Perform pre-test initialization.
284fdee8 54 *
866e5b51
FC
55 * structDef shouldn't be null after parsing the CTFTraceReader object, so
56 * we can ignore the warning.
ce2388e0
FC
57 *
58 * @throws CTFReaderException
866e5b51 59 */
866e5b51 60 @Before
13be1a8f 61 public void setUp() throws CTFReaderException {
866e5b51
FC
62 this.trace = TestParams.createTrace();
63
ce2388e0
FC
64 charArrayFixture = createCharArray();
65 stringArrayFixture = createStringArray();
66 longArrayFixture = createLongArray();
866e5b51
FC
67 }
68
ce2388e0 69 private ArrayDefinition createLongArray() {
fd74e6c1 70 IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8); //$NON-NLS-1$
ce2388e0
FC
71 IntegerDefinition[] defs = createIntDefs(10, 32);
72 ArrayDefinition temp = setUpDeclaration(decl, defs);
73 return temp;
74 }
75
76 private ArrayDefinition createCharArray() {
fd74e6c1 77 IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8); //$NON-NLS-1$
ce2388e0
FC
78 IntegerDefinition[] defs = createIntDefs(4,8);
79 ArrayDefinition temp = setUpDeclaration(decl, defs);
80 return temp;
81 }
82
83
84 /**
85 * @return
86 */
87 private ArrayDefinition createStringArray() {
88 StringDeclaration strDecl = new StringDeclaration();
89 StringDefinition[] defs = createDefs();
90 ArrayDefinition temp = setUpDeclaration(strDecl, defs);
91 return temp;
92 }
93 /**
94 * @param decl
95 * @param defs
96 * @return
97 */
98 private ArrayDefinition setUpDeclaration(IDeclaration decl,
99 Definition[] defs) {
100 ArrayDeclaration ad = new ArrayDeclaration(0, decl);
101 ArrayDefinition temp = new ArrayDefinition(ad , this.trace , "Testx"); //$NON-NLS-1$
102 temp.setDefinitions(defs);
103 return temp;
104 }
105 /**
106 * @param size
107 * @param bits
108 * @return
109 */
79cb3749 110 private static IntegerDefinition[] createIntDefs(int size, int bits) {
ce2388e0
FC
111 IntegerDefinition[] defs = new IntegerDefinition[size];
112 for (int i = 0; i < size; i++) {
113
114 String content = "test" + i; //$NON-NLS-1$
115 defs[i] = new IntegerDefinition(new IntegerDeclaration(bits, false,
fd74e6c1 116 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content);
ce2388e0
FC
117 defs[i].setValue(i);
118 }
119 return defs;
120 }
866e5b51
FC
121 /**
122 * Perform post-test clean-up.
123 */
124 @After
125 public void tearDown() {
126 // Add additional tear down code here
127 }
128
129 private static StringDefinition[] createDefs() {
130 int size = 4;
131 StringDefinition[] defs = new StringDefinition[size];
132 for (int i = 0; i < size; i++) {
133
134 String content = "test" + i; //$NON-NLS-1$
135 defs[i] = new StringDefinition(
136 new StringDeclaration(Encoding.UTF8), null, content);
137 defs[i].setString(new StringBuilder(content));
138 }
139 return defs;
140 }
141
142 /**
143 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
144 * constructor test.
145 */
146 @Test
147 public void testArrayDefinition_baseDeclaration() {
ce2388e0 148 ArrayDeclaration declaration = charArrayFixture.getDeclaration();
866e5b51
FC
149 String fieldName = ""; //$NON-NLS-1$
150
151 ArrayDefinition result = new ArrayDefinition(declaration, this.trace,
152 fieldName);
153
154 assertNotNull(result);
155 }
156
157 /**
158 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
159 * constructor test.
160 */
161 @Test
162 public void testArrayDefinition_newDeclaration() {
163 ArrayDeclaration declaration = new ArrayDeclaration(0,
164 new StringDeclaration());
165 IDefinitionScope definitionScope = null;
166 String fieldName = ""; //$NON-NLS-1$
167
168 ArrayDefinition result = new ArrayDefinition(declaration,
169 definitionScope, fieldName);
170
171 assertNotNull(result);
172 }
173
174 /**
175 * Run the ArrayDeclaration getDeclaration() method test.
176 */
177 @Test
178 public void testGetDeclaration() {
ce2388e0
FC
179 charArrayFixture.setDefinitions(new Definition[] {});
180 ArrayDeclaration result = charArrayFixture.getDeclaration();
866e5b51
FC
181
182 assertNotNull(result);
183 }
184
185 /**
186 * Run the Definition getElem(int) method test.
187 */
188 @Test
189 public void testGetElem_noDefs() {
190 int i = 0;
ce2388e0 191 Definition result = charArrayFixture.getElem(i);
866e5b51
FC
192
193 assertNotNull(result);
194 }
195
196 /**
197 * Run the Definition getElem(int) method test.
198 */
199 @Test
200 public void testGetElem_withDefs() {
201 Definition defs[] = createDefs();
ce2388e0 202 charArrayFixture.setDefinitions(defs);
866e5b51
FC
203 int j = 1;
204
ce2388e0 205 Definition result = charArrayFixture.getElem(j);
866e5b51
FC
206
207 assertNotNull(result);
208 }
209
210 /**
211 * Run the boolean isString() method test.
212 */
213 @Test
214 public void testIsString_ownDefs() {
866e5b51 215
ce2388e0 216 boolean result = stringArrayFixture.isString();
866e5b51
FC
217
218 assertFalse(result);
219 }
220
ce2388e0
FC
221
222
866e5b51
FC
223 /**
224 * Run the boolean isString() method test.
225 */
226 @Test
227 public void testIsString_complex() {
228 final IntegerDeclaration id = new IntegerDeclaration(8, false, 16,
fd74e6c1 229 ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8);
866e5b51
FC
230 ArrayDeclaration ad = new ArrayDeclaration(0, id);
231 ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace,
232 "Testx"); //$NON-NLS-1$
233
234 int size = 4;
ce2388e0
FC
235 int bits = 8;
236 IntegerDefinition[] defs = createIntDefs(size, bits);
866e5b51
FC
237
238 ownFixture.setDefinitions(defs);
239 boolean result = ownFixture.isString();
240
241 assertTrue(result);
242 }
243
ce2388e0
FC
244
245
866e5b51
FC
246 /**
247 * Run the boolean isString() method test.
248 */
249 @Test
250 public void testIsString_emptyDef() {
ce2388e0
FC
251 charArrayFixture.setDefinitions(new Definition[] {});
252 boolean result = charArrayFixture.isString();
866e5b51 253
ce2388e0 254 assertTrue(result);
866e5b51
FC
255 }
256
ce2388e0
FC
257 /**
258 * Run the boolean isString() method test.
259 */
260 @Test
261 public void testIsString_emptyDefStrDecl() {
262 ArrayDefinition ownFixture = createStringArray();
263 boolean result = ownFixture.isString();
264 assertFalse(result);
265 }
866e5b51
FC
266 /**
267 * Run the void read(BitBuffer) method test.
268 */
269 @Test
270 public void testRead_noDefs() {
271 BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
272
ce2388e0 273 charArrayFixture.read(input);
866e5b51
FC
274 }
275
276 /**
277 * Run the void read(BitBuffer) method test.
278 */
279 @Test
280 public void testRead_withDefs() {
ce2388e0 281 charArrayFixture.setDefinitions(new Definition[] {});
866e5b51
FC
282 BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
283
ce2388e0 284 charArrayFixture.read(input);
866e5b51
FC
285 }
286
287 /**
288 * Run the String toString() method test.
289 */
290 @Test
ce2388e0
FC
291 public void testToString_char() {
292 String result = charArrayFixture.toString();
293 assertNotNull(result);
294 }
295 /**
296 * Run the String toString() method test.
297 */
298 @Test
299 public void testToString_long() {
300 String result = longArrayFixture.toString();
866e5b51
FC
301 assertNotNull(result);
302 }
303
ce2388e0
FC
304 /**
305 * Run the String toString() method test.
306 */
307 @Test
308 public void testToString_string() {
309 String result = stringArrayFixture.toString();
310 assertNotNull(result);
311 }
866e5b51
FC
312 /**
313 * Run the String toString() method test.
314 */
315 @Test
316 public void testToString_withDefs() {
ce2388e0
FC
317 String result = charArrayFixture.toString();
318
319 assertNotNull(result);
320 }
321 /**
322 * Run the String toString() method test.
323 */
324 @Test
325 public void testToStringStringArray() {
326 String result = stringArrayFixture.toString();
866e5b51
FC
327
328 assertNotNull(result);
329 }
330}
This page took 0.043551 seconds and 5 git commands to generate.