Commit | Line | Data |
---|---|---|
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 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 | 13 | |
866e5b51 | 14 | import static org.junit.Assert.assertNotNull; |
866e5b51 | 15 | |
aefc5c83 | 16 | import java.nio.ByteBuffer; |
866e5b51 FC |
17 | import java.nio.ByteOrder; |
18 | ||
f357bcd4 AM |
19 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
20 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
21 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; | |
22 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
23 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
24 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
25 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
27 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
28 | import org.eclipse.tracecompass.internal.ctf.core.event.types.ByteArrayDefinition; | |
29 | import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration; | |
866e5b51 FC |
30 | import org.junit.Before; |
31 | import org.junit.Test; | |
32 | ||
a4fa4e36 MK |
33 | import com.google.common.collect.ImmutableList; |
34 | ||
866e5b51 | 35 | /** |
7b4f13e6 MK |
36 | * The class <code>SequenceDefinition2Test</code> contains tests for the class |
37 | * <code>{@link SequenceDefinition2}</code>. | |
284fdee8 | 38 | * |
866e5b51 FC |
39 | * @author ematkho |
40 | * @version $Revision: 1.0 $ | |
41 | */ | |
be6df2d8 | 42 | @SuppressWarnings("javadoc") |
7b4f13e6 | 43 | public class SequenceDefinition2Test { |
866e5b51 | 44 | |
7b4f13e6 | 45 | private ByteArrayDefinition fixture; |
866e5b51 FC |
46 | private final static int seqLen = 15; |
47 | ||
a4fa4e36 MK |
48 | private static ImmutableList<String> wrap(String s) { |
49 | return ImmutableList.<String> builder().add(s).build(); | |
50 | } | |
51 | ||
866e5b51 FC |
52 | /** |
53 | * Perform pre-test initialization. | |
a4fa4e36 | 54 | * |
fd74e6c1 | 55 | * @throws CTFReaderException |
866e5b51 FC |
56 | */ |
57 | @Before | |
25a9b50c | 58 | public void setUp() throws CTFReaderException { |
7b4f13e6 | 59 | fixture = initString(); |
866e5b51 FC |
60 | } |
61 | ||
7b4f13e6 | 62 | private static ByteArrayDefinition initString() throws CTFReaderException { |
866e5b51 FC |
63 | StructDeclaration structDec; |
64 | StructDefinition structDef; | |
65 | ||
7b4f13e6 | 66 | int len = 8; |
a4fa4e36 MK |
67 | IntegerDeclaration id = IntegerDeclaration.createDeclaration(len, false, len, |
68 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8); | |
4a9c1f07 | 69 | String lengthName = "LengthName"; |
866e5b51 FC |
70 | structDec = new StructDeclaration(0); |
71 | structDec.addField(lengthName, id); | |
866e5b51 | 72 | |
a4fa4e36 MK |
73 | structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) }); |
74 | ||
866e5b51 | 75 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); |
aefc5c83 MK |
76 | ByteBuffer allocateDirect = java.nio.ByteBuffer.allocateDirect(seqLen * len); |
77 | if( allocateDirect == null){ | |
78 | throw new IllegalStateException("Failed to allocate memory"); | |
79 | } | |
80 | BitBuffer input = new BitBuffer(allocateDirect); | |
866e5b51 FC |
81 | for (int i = 0; i < seqLen; i++) { |
82 | input.putInt(i); | |
83 | } | |
a4fa4e36 | 84 | |
7b4f13e6 | 85 | ByteArrayDefinition ret = (ByteArrayDefinition) sd.createDefinition(structDef, "TestX", input); |
866e5b51 FC |
86 | assertNotNull(ret); |
87 | return ret; | |
88 | } | |
89 | ||
90 | /** | |
7b4f13e6 | 91 | * Run the FixedStringDefinition(SequenceDeclaration,DefinitionScope,String) |
866e5b51 FC |
92 | * constructor test. |
93 | */ | |
94 | @Test | |
7b4f13e6 | 95 | public void testFixedStringDefinition() { |
866e5b51 FC |
96 | assertNotNull(fixture); |
97 | } | |
98 | ||
99 | /** | |
100 | * Run the SequenceDeclaration getDeclaration() method test. | |
101 | */ | |
102 | @Test | |
103 | public void testGetDeclaration() { | |
7b4f13e6 | 104 | SequenceDeclaration result = (SequenceDeclaration) fixture.getDeclaration(); |
866e5b51 FC |
105 | assertNotNull(result); |
106 | } | |
107 | ||
108 | /** | |
109 | * Run the Definition getElem(int) method test. | |
110 | */ | |
111 | @Test | |
112 | public void testGetElem() { | |
113 | int i = 1; | |
cc98c947 | 114 | IDefinition result = fixture.getDefinitions().get(i); |
866e5b51 FC |
115 | assertNotNull(result); |
116 | } | |
117 | ||
866e5b51 FC |
118 | /** |
119 | * Run the String toString() method test. | |
120 | */ | |
121 | @Test | |
122 | public void testToString() { | |
123 | String result = fixture.toString(); | |
124 | assertNotNull(result); | |
125 | } | |
126 | ||
866e5b51 | 127 | } |