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.event; |
13 | ||
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
16 | ||
17 | import java.nio.ByteBuffer; | |
18 | import java.nio.ByteOrder; | |
19 | ||
486efb2e | 20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
21 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
22 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
23 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
24 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
25 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
26 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
27 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
28 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
29 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
31 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
32 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
25a9b50c | 33 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
34 | import org.junit.After; |
35 | import org.junit.Before; | |
36 | import org.junit.Test; | |
37 | ||
38 | /** | |
39 | * The class <code>CTFEventFieldTest</code> contains tests for the class | |
40 | * <code>{@link CTFEventField}</code>. | |
284fdee8 | 41 | * |
866e5b51 FC |
42 | * @author ematkho |
43 | * @version $Revision: 1.0 $ | |
866e5b51 | 44 | */ |
be6df2d8 | 45 | @SuppressWarnings("javadoc") |
866e5b51 | 46 | public class CTFEventFieldTest { |
284fdee8 | 47 | |
4a9c1f07 | 48 | private static final String fieldName = "id"; |
284fdee8 MK |
49 | |
50 | ||
866e5b51 FC |
51 | /** |
52 | * Perform pre-test initialization. | |
53 | */ | |
54 | @Before | |
55 | public void setUp() { | |
56 | // add additional set up code here | |
57 | } | |
58 | ||
59 | /** | |
60 | * Perform post-test clean-up. | |
61 | */ | |
62 | @After | |
63 | public void tearDown() { | |
64 | // Add additional tear down code here | |
65 | } | |
66 | ||
67 | /** | |
68 | * Launch the test. | |
284fdee8 | 69 | * |
866e5b51 FC |
70 | * @param args |
71 | * the command line arguments | |
72 | */ | |
73 | public static void main(String[] args) { | |
74 | new org.junit.runner.JUnitCore().run(CTFEventFieldTest.class); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Run the CTFEventField parseField(Definition,String) method test. | |
fd74e6c1 | 79 | * @throws CTFReaderException |
866e5b51 FC |
80 | */ |
81 | @Test | |
25a9b50c | 82 | public void testParseField_complex() throws CTFReaderException { |
866e5b51 FC |
83 | int len = 32; |
84 | IntegerDeclaration id = new IntegerDeclaration(len, false, len, | |
fd74e6c1 | 85 | ByteOrder.LITTLE_ENDIAN, Encoding.ASCII, null, 32); |
4a9c1f07 | 86 | String lengthName = "LengthName"; |
866e5b51 FC |
87 | StructDeclaration structDec = new StructDeclaration(0); |
88 | structDec.addField(lengthName, id); | |
89 | StructDefinition structDef = new StructDefinition(structDec, null, | |
90 | lengthName); | |
91 | ||
92 | structDef.lookupInteger(lengthName).setValue(32); | |
93 | SequenceDeclaration sd = new SequenceDeclaration(lengthName, id); | |
4a9c1f07 | 94 | Definition fieldDef = new SequenceDefinition(sd, structDef, "TestX"); |
866e5b51 FC |
95 | ByteBuffer byb = ByteBuffer.allocate(1024); |
96 | for (int i = 0; i < 1024; i++) { | |
97 | byb.put((byte) i); | |
98 | } | |
99 | BitBuffer bb = new BitBuffer(byb); | |
100 | fieldDef.read(bb); | |
101 | ||
102 | assertNotNull(fieldDef); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Run the CTFEventField parseField(Definition,String) method test. | |
fd74e6c1 | 107 | * @throws CTFReaderException |
866e5b51 FC |
108 | */ |
109 | @Test | |
4c9d2941 MK |
110 | public void testParseField_simple() { |
111 | final StringDeclaration elemType = new StringDeclaration(); | |
112 | Definition fieldDef = elemType.createDefinition(null, fieldName); | |
866e5b51 FC |
113 | |
114 | assertNotNull(fieldDef); | |
115 | } | |
284fdee8 | 116 | |
866e5b51 FC |
117 | /** |
118 | * Run the CTFEventField parseField(Definition,String) method test. | |
119 | */ | |
120 | @Test | |
121 | public void testParseField_simple2() { | |
122 | IntegerDefinition fieldDef = new IntegerDefinition( | |
123 | new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, | |
fd74e6c1 | 124 | Encoding.ASCII, null, 8), null, fieldName); |
866e5b51 FC |
125 | fieldDef.setValue(1L); |
126 | ||
127 | assertNotNull(fieldDef); | |
128 | } | |
284fdee8 | 129 | |
866e5b51 | 130 | /** |
284fdee8 | 131 | * |
866e5b51 FC |
132 | */ |
133 | @Test | |
134 | public void testParseField_simple3() { | |
135 | StringDefinition fieldDef = new StringDefinition( | |
136 | new StringDeclaration(), null, fieldName); | |
4a9c1f07 | 137 | fieldDef.setString(new StringBuilder("Hello World")); |
866e5b51 | 138 | |
4a9c1f07 | 139 | String other = "\"Hello World\""; |
866e5b51 FC |
140 | assertNotNull(fieldDef); |
141 | assertEquals(fieldDef.toString(), other); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Run the CTFEventField parseField(Definition,String) method test. | |
146 | */ | |
147 | @Test | |
148 | public void testParseField_manual() { | |
149 | Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(20, | |
fd74e6c1 | 150 | new IntegerDeclaration(8, false, 8, null, Encoding.UTF8, null, 8)), |
866e5b51 FC |
151 | null, fieldName); |
152 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H'); | |
153 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e'); | |
154 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l'); | |
155 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l'); | |
156 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o'); | |
157 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' '); | |
158 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W'); | |
159 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o'); | |
160 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r'); | |
161 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l'); | |
162 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d'); | |
163 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0); | |
164 | ||
165 | assertNotNull(fieldDef); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Run the CTFEventField parseField(Definition,String) method test. | |
170 | */ | |
171 | @Test | |
172 | public void testParseField_manual2() { | |
173 | Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(12, | |
fd74e6c1 | 174 | new IntegerDeclaration(32, false, 32, null, null, null, 8)), null, |
866e5b51 FC |
175 | fieldName); |
176 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H'); | |
177 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e'); | |
178 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l'); | |
179 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l'); | |
180 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o'); | |
181 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' '); | |
182 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W'); | |
183 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o'); | |
184 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r'); | |
185 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l'); | |
186 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d'); | |
187 | ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0); | |
188 | ||
189 | assertNotNull(fieldDef); | |
4a9c1f07 | 190 | String other = "[ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0 ]"; |
866e5b51 FC |
191 | assertEquals(other, fieldDef.toString()); |
192 | } | |
193 | } |