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