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