Commit | Line | Data |
---|---|---|
866e5b51 FC |
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 | ||
486efb2e | 10 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
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; | |
ce2388e0 | 15 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
866e5b51 FC |
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; | |
866e5b51 | 21 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
866e5b51 FC |
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>. | |
284fdee8 | 29 | * |
866e5b51 FC |
30 | * @author ematkho |
31 | * @version $Revision: 1.0 $ | |
32 | */ | |
33 | public class ArrayDefinitionTest { | |
34 | ||
35 | private CTFTrace trace; | |
ce2388e0 FC |
36 | private ArrayDefinition charArrayFixture; |
37 | private ArrayDefinition stringArrayFixture; | |
38 | private ArrayDefinition longArrayFixture; | |
866e5b51 FC |
39 | |
40 | /** | |
41 | * Launch the test. | |
284fdee8 | 42 | * |
866e5b51 FC |
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. | |
284fdee8 | 52 | * |
866e5b51 FC |
53 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
54 | * we can ignore the warning. | |
55 | */ | |
866e5b51 | 56 | @Before |
be6df2d8 | 57 | public void setUp() { |
ce2388e0 FC |
58 | charArrayFixture = createCharArray(); |
59 | stringArrayFixture = createStringArray(); | |
60 | longArrayFixture = createLongArray(); | |
866e5b51 FC |
61 | } |
62 | ||
ce2388e0 | 63 | private ArrayDefinition createLongArray() { |
fd74e6c1 | 64 | IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8); //$NON-NLS-1$ |
ce2388e0 FC |
65 | IntegerDefinition[] defs = createIntDefs(10, 32); |
66 | ArrayDefinition temp = setUpDeclaration(decl, defs); | |
67 | return temp; | |
68 | } | |
69 | ||
70 | private ArrayDefinition createCharArray() { | |
fd74e6c1 | 71 | IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8); //$NON-NLS-1$ |
ce2388e0 FC |
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 | */ | |
79cb3749 | 104 | private static IntegerDefinition[] createIntDefs(int size, int bits) { |
ce2388e0 FC |
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, | |
fd74e6c1 | 110 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content); |
ce2388e0 FC |
111 | defs[i].setValue(i); |
112 | } | |
113 | return defs; | |
114 | } | |
866e5b51 FC |
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() { | |
ce2388e0 | 142 | ArrayDeclaration declaration = charArrayFixture.getDeclaration(); |
866e5b51 FC |
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() { | |
ce2388e0 FC |
173 | charArrayFixture.setDefinitions(new Definition[] {}); |
174 | ArrayDeclaration result = charArrayFixture.getDeclaration(); | |
866e5b51 FC |
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; | |
ce2388e0 | 185 | Definition result = charArrayFixture.getElem(i); |
866e5b51 FC |
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(); | |
ce2388e0 | 196 | charArrayFixture.setDefinitions(defs); |
866e5b51 FC |
197 | int j = 1; |
198 | ||
ce2388e0 | 199 | Definition result = charArrayFixture.getElem(j); |
866e5b51 FC |
200 | |
201 | assertNotNull(result); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Run the boolean isString() method test. | |
206 | */ | |
207 | @Test | |
208 | public void testIsString_ownDefs() { | |
866e5b51 | 209 | |
ce2388e0 | 210 | boolean result = stringArrayFixture.isString(); |
866e5b51 FC |
211 | |
212 | assertFalse(result); | |
213 | } | |
214 | ||
ce2388e0 FC |
215 | |
216 | ||
866e5b51 FC |
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, | |
fd74e6c1 | 223 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8); |
866e5b51 FC |
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; | |
ce2388e0 FC |
229 | int bits = 8; |
230 | IntegerDefinition[] defs = createIntDefs(size, bits); | |
866e5b51 FC |
231 | |
232 | ownFixture.setDefinitions(defs); | |
233 | boolean result = ownFixture.isString(); | |
234 | ||
235 | assertTrue(result); | |
236 | } | |
237 | ||
ce2388e0 FC |
238 | |
239 | ||
866e5b51 FC |
240 | /** |
241 | * Run the boolean isString() method test. | |
242 | */ | |
243 | @Test | |
244 | public void testIsString_emptyDef() { | |
ce2388e0 FC |
245 | charArrayFixture.setDefinitions(new Definition[] {}); |
246 | boolean result = charArrayFixture.isString(); | |
866e5b51 | 247 | |
ce2388e0 | 248 | assertTrue(result); |
866e5b51 FC |
249 | } |
250 | ||
ce2388e0 FC |
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 | } | |
866e5b51 FC |
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 | ||
ce2388e0 | 267 | charArrayFixture.read(input); |
866e5b51 FC |
268 | } |
269 | ||
270 | /** | |
271 | * Run the void read(BitBuffer) method test. | |
272 | */ | |
273 | @Test | |
274 | public void testRead_withDefs() { | |
ce2388e0 | 275 | charArrayFixture.setDefinitions(new Definition[] {}); |
866e5b51 FC |
276 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); |
277 | ||
ce2388e0 | 278 | charArrayFixture.read(input); |
866e5b51 FC |
279 | } |
280 | ||
281 | /** | |
282 | * Run the String toString() method test. | |
283 | */ | |
284 | @Test | |
ce2388e0 FC |
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(); | |
866e5b51 FC |
295 | assertNotNull(result); |
296 | } | |
297 | ||
ce2388e0 FC |
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 | } | |
866e5b51 FC |
306 | /** |
307 | * Run the String toString() method test. | |
308 | */ | |
309 | @Test | |
310 | public void testToString_withDefs() { | |
ce2388e0 FC |
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(); | |
866e5b51 FC |
321 | |
322 | assertNotNull(result); | |
323 | } | |
324 | } |