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