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 | ||
866e5b51 | 14 | import static org.junit.Assert.assertNotNull; |
866e5b51 FC |
15 | |
16 | import java.nio.ByteBuffer; | |
17 | import java.nio.ByteOrder; | |
a4fa4e36 MK |
18 | import java.util.ArrayList; |
19 | import java.util.Arrays; | |
20 | import java.util.List; | |
866e5b51 | 21 | |
a4fa4e36 | 22 | import org.eclipse.jdt.annotation.NonNull; |
486efb2e | 23 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
a4fa4e36 MK |
24 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; |
25 | import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; | |
866e5b51 FC |
26 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
27 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
28 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
29 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
ce2388e0 | 30 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
866e5b51 FC |
31 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
32 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
33 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; | |
34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
db8e8f7d | 35 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 | 36 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
866e5b51 FC |
37 | import org.junit.Before; |
38 | import org.junit.Test; | |
39 | ||
40 | /** | |
41 | * The class <code>ArrayDefinitionTest</code> contains tests for the class | |
42 | * <code>{@link ArrayDefinition}</code>. | |
284fdee8 | 43 | * |
866e5b51 FC |
44 | * @author ematkho |
45 | * @version $Revision: 1.0 $ | |
46 | */ | |
47 | public class ArrayDefinitionTest { | |
48 | ||
a4fa4e36 | 49 | @NonNull private CTFTrace trace = new CTFTrace(); |
ce2388e0 FC |
50 | private ArrayDefinition charArrayFixture; |
51 | private ArrayDefinition stringArrayFixture; | |
52 | private ArrayDefinition longArrayFixture; | |
866e5b51 | 53 | |
866e5b51 FC |
54 | /** |
55 | * Perform pre-test initialization. | |
284fdee8 | 56 | * |
866e5b51 FC |
57 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
58 | * we can ignore the warning. | |
59 | */ | |
866e5b51 | 60 | @Before |
be6df2d8 | 61 | public void setUp() { |
ce2388e0 FC |
62 | charArrayFixture = createCharArray(); |
63 | stringArrayFixture = createStringArray(); | |
64 | longArrayFixture = createLongArray(); | |
866e5b51 FC |
65 | } |
66 | ||
ce2388e0 | 67 | private ArrayDefinition createLongArray() { |
a4fa4e36 MK |
68 | IntegerDeclaration decl = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none", 8); |
69 | List<Definition> defs = createIntDefs(10, 32); | |
ce2388e0 FC |
70 | ArrayDefinition temp = setUpDeclaration(decl, defs); |
71 | return temp; | |
72 | } | |
73 | ||
74 | private ArrayDefinition createCharArray() { | |
a4fa4e36 MK |
75 | IntegerDeclaration decl = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none", 8); |
76 | List<Definition> defs = createIntDefs(4, 8); | |
ce2388e0 FC |
77 | ArrayDefinition temp = setUpDeclaration(decl, defs); |
78 | return temp; | |
79 | } | |
80 | ||
ce2388e0 FC |
81 | private ArrayDefinition createStringArray() { |
82 | StringDeclaration strDecl = new StringDeclaration(); | |
a4fa4e36 | 83 | List<Definition> defs = createDefs(); |
ce2388e0 FC |
84 | ArrayDefinition temp = setUpDeclaration(strDecl, defs); |
85 | return temp; | |
86 | } | |
a34d8eee | 87 | |
ce2388e0 | 88 | private ArrayDefinition setUpDeclaration(IDeclaration decl, |
a4fa4e36 | 89 | @NonNull List<Definition> defs) { |
ce2388e0 | 90 | ArrayDeclaration ad = new ArrayDeclaration(0, decl); |
a4fa4e36 | 91 | ArrayDefinition temp = new ArrayDefinition(ad, this.trace, "Testx", defs); |
ce2388e0 FC |
92 | return temp; |
93 | } | |
a34d8eee | 94 | |
a4fa4e36 MK |
95 | private @NonNull |
96 | static List<Definition> createIntDefs(int size, int bits) { | |
97 | List<Definition> defs = new ArrayList<>(size); | |
ce2388e0 | 98 | for (int i = 0; i < size; i++) { |
4a9c1f07 | 99 | String content = "test" + i; |
a4fa4e36 MK |
100 | defs.add(new IntegerDefinition(IntegerDeclaration.createDeclaration(bits, false, |
101 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content, i)); | |
ce2388e0 FC |
102 | } |
103 | return defs; | |
104 | } | |
866e5b51 | 105 | |
a4fa4e36 MK |
106 | private @NonNull |
107 | static List<Definition> createDefs() { | |
866e5b51 | 108 | int size = 4; |
a4fa4e36 | 109 | List<Definition> defs = new ArrayList<>(); |
866e5b51 | 110 | for (int i = 0; i < size; i++) { |
4a9c1f07 | 111 | String content = "test" + i; |
a4fa4e36 MK |
112 | defs.add(new StringDefinition( |
113 | new StringDeclaration(Encoding.UTF8), null, content, content)); | |
866e5b51 FC |
114 | } |
115 | return defs; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) | |
120 | * constructor test. | |
121 | */ | |
122 | @Test | |
123 | public void testArrayDefinition_baseDeclaration() { | |
ce2388e0 | 124 | ArrayDeclaration declaration = charArrayFixture.getDeclaration(); |
4a9c1f07 | 125 | String fieldName = ""; |
866e5b51 | 126 | |
a4fa4e36 MK |
127 | @SuppressWarnings("null") |
128 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName, Arrays.asList(new Definition[0])); | |
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()); | |
a4fa4e36 | 140 | IDefinitionScope definitionScope = getDefinitionScope(); |
866e5b51 | 141 | |
a4fa4e36 MK |
142 | String fieldName = ""; |
143 | @SuppressWarnings("null") | |
144 | ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName , Arrays.asList(new Definition[0])); | |
866e5b51 FC |
145 | assertNotNull(result); |
146 | } | |
147 | ||
148 | /** | |
149 | * Run the ArrayDeclaration getDeclaration() method test. | |
150 | */ | |
151 | @Test | |
152 | public void testGetDeclaration() { | |
ce2388e0 | 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() { | |
a4fa4e36 MK |
174 | List<Definition> defs = createDefs(); |
175 | IDefinitionScope definitionScope = getDefinitionScope(); | |
176 | ArrayDefinition ad = new ArrayDefinition(charArrayFixture.getDeclaration(), definitionScope, "test", defs); | |
866e5b51 FC |
177 | int j = 1; |
178 | ||
a4fa4e36 | 179 | Definition result = ad.getElem(j); |
866e5b51 FC |
180 | |
181 | assertNotNull(result); | |
182 | } | |
183 | ||
a4fa4e36 MK |
184 | @NonNull private static IDefinitionScope getDefinitionScope() { |
185 | return new IDefinitionScope() { | |
186 | ||
187 | @Override | |
188 | public Definition lookupDefinition(String lookupPath) { | |
189 | return null; | |
190 | } | |
866e5b51 | 191 | |
a4fa4e36 MK |
192 | @Override |
193 | public LexicalScope getScopePath() { | |
194 | return null; | |
195 | } | |
196 | }; | |
866e5b51 FC |
197 | } |
198 | ||
199 | /** | |
200 | * Run the void read(BitBuffer) method test. | |
a4fa4e36 MK |
201 | * |
202 | * @throws CTFReaderException | |
203 | * error | |
866e5b51 FC |
204 | */ |
205 | @Test | |
a4fa4e36 MK |
206 | public void testRead_noDefs() throws CTFReaderException { |
207 | BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); | |
208 | charArrayFixture.getDeclaration().createDefinition(null, "test", input); | |
866e5b51 FC |
209 | } |
210 | ||
211 | /** | |
212 | * Run the String toString() method test. | |
213 | */ | |
214 | @Test | |
ce2388e0 FC |
215 | public void testToString_char() { |
216 | String result = charArrayFixture.toString(); | |
217 | assertNotNull(result); | |
218 | } | |
a4fa4e36 | 219 | |
ce2388e0 FC |
220 | /** |
221 | * Run the String toString() method test. | |
222 | */ | |
223 | @Test | |
224 | public void testToString_long() { | |
225 | String result = longArrayFixture.toString(); | |
866e5b51 FC |
226 | assertNotNull(result); |
227 | } | |
228 | ||
ce2388e0 FC |
229 | /** |
230 | * Run the String toString() method test. | |
231 | */ | |
232 | @Test | |
233 | public void testToString_string() { | |
234 | String result = stringArrayFixture.toString(); | |
235 | assertNotNull(result); | |
236 | } | |
a4fa4e36 | 237 | |
866e5b51 FC |
238 | /** |
239 | * Run the String toString() method test. | |
240 | */ | |
241 | @Test | |
242 | public void testToString_withDefs() { | |
ce2388e0 FC |
243 | String result = charArrayFixture.toString(); |
244 | ||
245 | assertNotNull(result); | |
246 | } | |
a4fa4e36 | 247 | |
ce2388e0 FC |
248 | /** |
249 | * Run the String toString() method test. | |
250 | */ | |
251 | @Test | |
252 | public void testToStringStringArray() { | |
253 | String result = stringArrayFixture.toString(); | |
866e5b51 FC |
254 | |
255 | assertNotNull(result); | |
256 | } | |
257 | } |