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