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; | |
db8e8f7d | 32 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 | 33 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
866e5b51 FC |
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 | 50 | |
866e5b51 FC |
51 | /** |
52 | * Perform pre-test initialization. | |
284fdee8 | 53 | * |
866e5b51 FC |
54 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
55 | * we can ignore the warning. | |
56 | */ | |
866e5b51 | 57 | @Before |
be6df2d8 | 58 | public void setUp() { |
ce2388e0 FC |
59 | charArrayFixture = createCharArray(); |
60 | stringArrayFixture = createStringArray(); | |
61 | longArrayFixture = createLongArray(); | |
866e5b51 FC |
62 | } |
63 | ||
ce2388e0 | 64 | private ArrayDefinition createLongArray() { |
4a9c1f07 | 65 | IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8); |
ce2388e0 FC |
66 | IntegerDefinition[] defs = createIntDefs(10, 32); |
67 | ArrayDefinition temp = setUpDeclaration(decl, defs); | |
68 | return temp; | |
69 | } | |
70 | ||
71 | private ArrayDefinition createCharArray() { | |
4a9c1f07 | 72 | IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8); |
ce2388e0 FC |
73 | IntegerDefinition[] defs = createIntDefs(4,8); |
74 | ArrayDefinition temp = setUpDeclaration(decl, defs); | |
75 | return temp; | |
76 | } | |
77 | ||
ce2388e0 FC |
78 | private ArrayDefinition createStringArray() { |
79 | StringDeclaration strDecl = new StringDeclaration(); | |
80 | StringDefinition[] defs = createDefs(); | |
81 | ArrayDefinition temp = setUpDeclaration(strDecl, defs); | |
82 | return temp; | |
83 | } | |
a34d8eee | 84 | |
ce2388e0 FC |
85 | private ArrayDefinition setUpDeclaration(IDeclaration decl, |
86 | Definition[] defs) { | |
87 | ArrayDeclaration ad = new ArrayDeclaration(0, decl); | |
4a9c1f07 | 88 | ArrayDefinition temp = new ArrayDefinition(ad , this.trace , "Testx"); |
ce2388e0 FC |
89 | temp.setDefinitions(defs); |
90 | return temp; | |
91 | } | |
a34d8eee AM |
92 | |
93 | ||
79cb3749 | 94 | private static IntegerDefinition[] createIntDefs(int size, int bits) { |
ce2388e0 FC |
95 | IntegerDefinition[] defs = new IntegerDefinition[size]; |
96 | for (int i = 0; i < size; i++) { | |
97 | ||
4a9c1f07 | 98 | String content = "test" + i; |
ce2388e0 | 99 | defs[i] = new IntegerDefinition(new IntegerDeclaration(bits, false, |
fd74e6c1 | 100 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content); |
ce2388e0 FC |
101 | defs[i].setValue(i); |
102 | } | |
103 | return defs; | |
104 | } | |
866e5b51 FC |
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 | ||
4a9c1f07 | 111 | String content = "test" + i; |
866e5b51 FC |
112 | defs[i] = new StringDefinition( |
113 | new StringDeclaration(Encoding.UTF8), null, content); | |
114 | defs[i].setString(new StringBuilder(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() { | |
ce2388e0 | 125 | ArrayDeclaration declaration = charArrayFixture.getDeclaration(); |
4a9c1f07 | 126 | String fieldName = ""; |
866e5b51 | 127 | |
4a9c1f07 | 128 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName); |
866e5b51 FC |
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; | |
4a9c1f07 | 141 | String fieldName = ""; |
866e5b51 | 142 | |
4a9c1f07 | 143 | ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName); |
866e5b51 FC |
144 | assertNotNull(result); |
145 | } | |
146 | ||
147 | /** | |
148 | * Run the ArrayDeclaration getDeclaration() method test. | |
149 | */ | |
150 | @Test | |
151 | public void testGetDeclaration() { | |
ce2388e0 FC |
152 | charArrayFixture.setDefinitions(new Definition[] {}); |
153 | ArrayDeclaration result = charArrayFixture.getDeclaration(); | |
866e5b51 FC |
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; | |
ce2388e0 | 164 | Definition result = charArrayFixture.getElem(i); |
866e5b51 FC |
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(); | |
ce2388e0 | 175 | charArrayFixture.setDefinitions(defs); |
866e5b51 FC |
176 | int j = 1; |
177 | ||
ce2388e0 | 178 | Definition result = charArrayFixture.getElem(j); |
866e5b51 FC |
179 | |
180 | assertNotNull(result); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Run the boolean isString() method test. | |
185 | */ | |
186 | @Test | |
187 | public void testIsString_ownDefs() { | |
866e5b51 | 188 | |
ce2388e0 | 189 | boolean result = stringArrayFixture.isString(); |
866e5b51 FC |
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, | |
fd74e6c1 | 200 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8); |
866e5b51 | 201 | ArrayDeclaration ad = new ArrayDeclaration(0, id); |
4a9c1f07 | 202 | ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace, "Testx"); |
866e5b51 FC |
203 | |
204 | int size = 4; | |
ce2388e0 FC |
205 | int bits = 8; |
206 | IntegerDefinition[] defs = createIntDefs(size, bits); | |
866e5b51 FC |
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() { | |
ce2388e0 FC |
219 | charArrayFixture.setDefinitions(new Definition[] {}); |
220 | boolean result = charArrayFixture.isString(); | |
866e5b51 | 221 | |
ce2388e0 | 222 | assertTrue(result); |
866e5b51 FC |
223 | } |
224 | ||
ce2388e0 FC |
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 | } | |
866e5b51 FC |
234 | /** |
235 | * Run the void read(BitBuffer) method test. | |
db8e8f7d | 236 | * @throws CTFReaderException error |
866e5b51 FC |
237 | */ |
238 | @Test | |
db8e8f7d | 239 | public void testRead_noDefs() throws CTFReaderException { |
866e5b51 FC |
240 | BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); |
241 | ||
ce2388e0 | 242 | charArrayFixture.read(input); |
866e5b51 FC |
243 | } |
244 | ||
245 | /** | |
246 | * Run the void read(BitBuffer) method test. | |
db8e8f7d | 247 | * @throws CTFReaderException error |
866e5b51 FC |
248 | */ |
249 | @Test | |
db8e8f7d | 250 | public void testRead_withDefs() throws CTFReaderException { |
ce2388e0 | 251 | charArrayFixture.setDefinitions(new Definition[] {}); |
866e5b51 FC |
252 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); |
253 | ||
ce2388e0 | 254 | charArrayFixture.read(input); |
866e5b51 FC |
255 | } |
256 | ||
257 | /** | |
258 | * Run the String toString() method test. | |
259 | */ | |
260 | @Test | |
ce2388e0 FC |
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(); | |
866e5b51 FC |
271 | assertNotNull(result); |
272 | } | |
273 | ||
ce2388e0 FC |
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 | } | |
866e5b51 FC |
282 | /** |
283 | * Run the String toString() method test. | |
284 | */ | |
285 | @Test | |
286 | public void testToString_withDefs() { | |
ce2388e0 FC |
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(); | |
866e5b51 FC |
297 | |
298 | assertNotNull(result); | |
299 | } | |
300 | } |