073e5d4a6d80fa9c51c91ab433417ef49d25942a
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / SequenceDefinitionTest.java
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
12 package org.eclipse.linuxtools.ctf.core.tests.types;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17
18 import java.nio.ByteOrder;
19
20 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
21 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
22 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
23 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
24 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
28 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 /**
33 * The class <code>SequenceDefinitionTest</code> contains tests for the class
34 * <code>{@link SequenceDefinition}</code>.
35 *
36 * @author ematkho
37 * @version $Revision: 1.0 $
38 */
39 @SuppressWarnings("javadoc")
40 public class SequenceDefinitionTest {
41
42 private SequenceDefinition fixture;
43 private final static int seqLen = 15;
44
45 /**
46 * Perform pre-test initialization.
47 * @throws CTFReaderException
48 */
49 @Before
50 public void setUp() throws CTFReaderException {
51 StructDeclaration structDec;
52 StructDefinition structDef;
53
54 IntegerDeclaration id = new IntegerDeclaration(8, false, 8,
55 ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8);
56 String lengthName = "LengthName";
57 structDec = new StructDeclaration(0);
58 structDec.addField(lengthName, id);
59 structDef = new StructDefinition(structDec, null, "x");
60
61 structDef.lookupInteger(lengthName).setValue(seqLen);
62 SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
63 fixture = new SequenceDefinition(sd, structDef, "TestX");
64 BitBuffer input = new BitBuffer(
65 java.nio.ByteBuffer.allocateDirect(seqLen * 8));
66 for (int i = 0; i < seqLen; i++) {
67 input.putInt(i);
68 }
69 fixture.read(input);
70 assert (fixture != null);
71 }
72
73 private static SequenceDefinition initNonString() throws CTFReaderException {
74 StructDeclaration structDec;
75 StructDefinition structDef;
76
77 int len = 32;
78 IntegerDeclaration id = new IntegerDeclaration(len, false, len,
79 ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null,8);
80 String lengthName = "LengthName";
81 structDec = new StructDeclaration(0);
82 structDec.addField(lengthName, id);
83 structDef = new StructDefinition(structDec, null, "x");
84
85 structDef.lookupInteger(lengthName).setValue(seqLen);
86 SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
87 SequenceDefinition ret = new SequenceDefinition(sd, structDef, "TestX");
88 BitBuffer input = new BitBuffer(
89 java.nio.ByteBuffer.allocateDirect(seqLen * len));
90 for (int i = 0; i < seqLen; i++) {
91 input.putInt(i);
92 }
93 ret.read(input);
94 assertNotNull(ret);
95 return ret;
96 }
97
98 /**
99 * Run the SequenceDefinition(SequenceDeclaration,DefinitionScope,String)
100 * constructor test.
101 */
102 @Test
103 public void testSequenceDefinition() {
104 assertNotNull(fixture);
105 }
106
107 /**
108 * Run the SequenceDeclaration getDeclaration() method test.
109 */
110 @Test
111 public void testGetDeclaration() {
112 SequenceDeclaration result = fixture.getDeclaration();
113 assertNotNull(result);
114 }
115
116 /**
117 * Run the Definition getElem(int) method test.
118 */
119 @Test
120 public void testGetElem() {
121 int i = 1;
122 Definition result = fixture.getElem(i);
123 assertNotNull(result);
124 }
125
126 /**
127 * Run the int getLength() method test.
128 */
129 @Test
130 public void testGetLength() {
131 int result = fixture.getLength();
132
133 assertEquals(seqLen, result);
134 }
135
136 /**
137 * Run the boolean isString() method test.
138 */
139 @Test
140 public void testIsString() {
141 boolean result = fixture.isString();
142 assertTrue(result);
143 }
144
145 /**
146 * Run the void read(BitBuffer) method test.
147 * @throws CTFReaderException error
148 */
149 @Test
150 public void testRead() throws CTFReaderException {
151 BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
152 fixture.read(input);
153 }
154
155 /**
156 * Run the String toString() method test.
157 */
158 @Test
159 public void testToString() {
160 String result = fixture.toString();
161 assertNotNull(result);
162 }
163
164 /**
165 * Run the String toString() method test.
166 */
167 @Test
168 public void testToString_nonString() throws Exception {
169 fixture = initNonString();
170 String result = fixture.toString();
171 assertNotNull(result);
172 }
173 }
This page took 0.035338 seconds and 4 git commands to generate.