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 | ||
10 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
11 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
12 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; | |
13 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
14 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
15 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
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; | |
21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
22 | import org.eclipse.linuxtools.ctf.core.tests.TestParams; | |
13be1a8f | 23 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
24 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
25 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
26 | import org.junit.After; | |
27 | import org.junit.Before; | |
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * The class <code>ArrayDefinitionTest</code> contains tests for the class | |
32 | * <code>{@link ArrayDefinition}</code>. | |
284fdee8 | 33 | * |
866e5b51 FC |
34 | * @author ematkho |
35 | * @version $Revision: 1.0 $ | |
36 | */ | |
37 | public class ArrayDefinitionTest { | |
38 | ||
39 | private CTFTrace trace; | |
40 | private ArrayDefinition fixture; | |
41 | ||
42 | /** | |
43 | * Launch the test. | |
284fdee8 | 44 | * |
866e5b51 FC |
45 | * @param args |
46 | * the command line arguments | |
47 | */ | |
48 | public static void main(String[] args) { | |
49 | new org.junit.runner.JUnitCore().run(ArrayDefinitionTest.class); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Perform pre-test initialization. | |
284fdee8 | 54 | * |
866e5b51 FC |
55 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
56 | * we can ignore the warning. | |
13be1a8f AM |
57 | * |
58 | * @throws CTFReaderException | |
866e5b51 | 59 | */ |
866e5b51 | 60 | @Before |
13be1a8f | 61 | public void setUp() throws CTFReaderException { |
866e5b51 FC |
62 | this.trace = TestParams.createTrace(); |
63 | ||
64 | CTFTraceReader tr = new CTFTraceReader(this.trace); | |
65 | String name = ""; //$NON-NLS-1$ | |
66 | StructDefinition structDef = null; | |
67 | boolean foundArray = false; | |
68 | ||
69 | while (tr.hasMoreEvents() && !foundArray) { | |
70 | tr.advance(); | |
71 | EventDefinition ed = tr.getCurrentEventDef(); | |
72 | for (String key : ed.fields.getDefinitions().keySet()) { | |
73 | structDef = ed.fields; | |
74 | Definition d = structDef.lookupDefinition(key); | |
75 | if (d instanceof ArrayDefinition) { | |
76 | foundArray = true; | |
77 | name = key; | |
78 | break; | |
79 | } | |
80 | } | |
81 | } | |
82 | fixture = structDef.lookupArray(name); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Perform post-test clean-up. | |
87 | */ | |
88 | @After | |
89 | public void tearDown() { | |
90 | // Add additional tear down code here | |
91 | } | |
92 | ||
93 | private static StringDefinition[] createDefs() { | |
94 | int size = 4; | |
95 | StringDefinition[] defs = new StringDefinition[size]; | |
96 | for (int i = 0; i < size; i++) { | |
97 | ||
98 | String content = "test" + i; //$NON-NLS-1$ | |
99 | defs[i] = new StringDefinition( | |
100 | new StringDeclaration(Encoding.UTF8), null, content); | |
101 | defs[i].setString(new StringBuilder(content)); | |
102 | } | |
103 | return defs; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) | |
108 | * constructor test. | |
109 | */ | |
110 | @Test | |
111 | public void testArrayDefinition_baseDeclaration() { | |
112 | ArrayDeclaration declaration = fixture.getDeclaration(); | |
113 | String fieldName = ""; //$NON-NLS-1$ | |
114 | ||
115 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, | |
116 | fieldName); | |
117 | ||
118 | assertNotNull(result); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) | |
123 | * constructor test. | |
124 | */ | |
125 | @Test | |
126 | public void testArrayDefinition_newDeclaration() { | |
127 | ArrayDeclaration declaration = new ArrayDeclaration(0, | |
128 | new StringDeclaration()); | |
129 | IDefinitionScope definitionScope = null; | |
130 | String fieldName = ""; //$NON-NLS-1$ | |
131 | ||
132 | ArrayDefinition result = new ArrayDefinition(declaration, | |
133 | definitionScope, fieldName); | |
134 | ||
135 | assertNotNull(result); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Run the ArrayDeclaration getDeclaration() method test. | |
140 | */ | |
141 | @Test | |
142 | public void testGetDeclaration() { | |
143 | fixture.setDefinitions(new Definition[] {}); | |
144 | ArrayDeclaration result = fixture.getDeclaration(); | |
145 | ||
146 | assertNotNull(result); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Run the Definition getElem(int) method test. | |
151 | */ | |
152 | @Test | |
153 | public void testGetElem_noDefs() { | |
154 | int i = 0; | |
155 | Definition result = fixture.getElem(i); | |
156 | ||
157 | assertNotNull(result); | |
158 | } | |
159 | ||
160 | /** | |
161 | * Run the Definition getElem(int) method test. | |
162 | */ | |
163 | @Test | |
164 | public void testGetElem_withDefs() { | |
165 | Definition defs[] = createDefs(); | |
166 | fixture.setDefinitions(defs); | |
167 | int j = 1; | |
168 | ||
169 | Definition result = fixture.getElem(j); | |
170 | ||
171 | assertNotNull(result); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Run the boolean isString() method test. | |
176 | */ | |
177 | @Test | |
178 | public void testIsString_ownDefs() { | |
179 | StringDefinition[] defs = createDefs(); | |
180 | fixture.setDefinitions(defs); | |
181 | ||
182 | boolean result = fixture.isString(); | |
183 | ||
184 | assertFalse(result); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Run the boolean isString() method test. | |
189 | */ | |
190 | @Test | |
191 | public void testIsString_complex() { | |
192 | final IntegerDeclaration id = new IntegerDeclaration(8, false, 16, | |
284fdee8 | 193 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null); |
866e5b51 FC |
194 | ArrayDeclaration ad = new ArrayDeclaration(0, id); |
195 | ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace, | |
196 | "Testx"); //$NON-NLS-1$ | |
197 | ||
198 | int size = 4; | |
199 | IntegerDefinition[] defs = new IntegerDefinition[size]; | |
200 | for (int i = 0; i < size; i++) { | |
201 | ||
202 | String content = "test" + i; //$NON-NLS-1$ | |
203 | defs[i] = new IntegerDefinition(new IntegerDeclaration(8, false, | |
284fdee8 | 204 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content), null, content); |
866e5b51 FC |
205 | defs[i].setValue(i); |
206 | } | |
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() { | |
219 | fixture.setDefinitions(new Definition[] {}); | |
220 | boolean result = fixture.isString(); | |
221 | ||
222 | assertFalse(result); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Run the void read(BitBuffer) method test. | |
227 | */ | |
228 | @Test | |
229 | public void testRead_noDefs() { | |
230 | BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); | |
231 | ||
232 | fixture.read(input); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Run the void read(BitBuffer) method test. | |
237 | */ | |
238 | @Test | |
239 | public void testRead_withDefs() { | |
240 | fixture.setDefinitions(new Definition[] {}); | |
241 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); | |
242 | ||
243 | fixture.read(input); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Run the String toString() method test. | |
248 | */ | |
249 | @Test | |
250 | public void testToString_base() { | |
251 | String result = fixture.toString(); | |
252 | ||
253 | assertNotNull(result); | |
254 | } | |
255 | ||
256 | /** | |
257 | * Run the String toString() method test. | |
258 | */ | |
259 | @Test | |
260 | public void testToString_withDefs() { | |
261 | int size = 2; | |
262 | StringDefinition[] defs = new StringDefinition[size]; | |
263 | for (int i = 0; i < size; i++) { | |
264 | defs[i] = new StringDefinition(null, null, ("test" + i)); //$NON-NLS-1$ | |
265 | } | |
266 | fixture.setDefinitions(defs); | |
267 | String result = fixture.toString(); | |
268 | ||
269 | assertNotNull(result); | |
270 | } | |
271 | } |