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