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