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