btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / VariantDefinitionTest.java
index b145b2cf2537120e27ee6f3eea59a06cca2dd200..8c690bad89c290111c61dc4b25192dd9f8fd7baa 100644 (file)
@@ -1,34 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2014 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
 package org.eclipse.linuxtools.ctf.core.tests.types;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
-import java.util.HashMap;
 
-import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.AbstractArrayDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
-import org.eclipse.linuxtools.ctf.core.tests.TestParams;
-import org.junit.After;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>VariantDefinitionTest</code> contains tests for the class
  * <code>{@link VariantDefinition}</code>.
- * 
+ *
  * @author ematkho
  * @version $Revision: 1.0 $
  */
@@ -36,75 +56,118 @@ public class VariantDefinitionTest {
 
     private VariantDefinition fixture;
 
-    /**
-     * Launch the test.
-     * 
-     * @param args
-     *            the command line arguments
-     */
-    public static void main(String[] args) {
-        new org.junit.runner.JUnitCore().run(VariantDefinitionTest.class);
-    }
+    StructDefinition fStructDefinition;
+    private static final @NonNull String TEST_STRUCT_ID = "testStruct";
+    private static final @NonNull String ENUM_7 = "g";
+    private static final @NonNull String ENUM_6 = "f";
+    private static final @NonNull String ENUM_5 = "e";
+    private static final @NonNull String ENUM_4 = "d";
+    private static final @NonNull String ENUM_3 = "c";
+    private static final @NonNull String ENUM_2 = "b";
+    private static final @NonNull String ENUM_1 = "a";
+    private static final @NonNull String TAG_ID = "a";
+    private static final @NonNull String LENGTH_SEQ = "_len";
+    private static final @NonNull String VAR_FIELD_NAME = "var";
+    private static final @NonNull String ENUM_8 = "bbq ribs";
 
     /**
      * Perform pre-test initialization.
-     * 
+     *
      * Not sure it needs to be that complicated, oh well...
+     *
+     * @throws CTFReaderException
+     *             won't happen
      */
     @Before
-    public void setUp() {
-        VariantDeclaration vDecl1, vDecl2, vDecl3;
-        VariantDefinition vDef1, vDef2;
-        StructDefinition sDef1, sDef2;
-        EnumDefinition eDef;
-        String fName = ""; //$NON-NLS-1$
-
-        vDecl1 = new VariantDeclaration();
-        vDecl2 = new VariantDeclaration();
-        vDecl3 = new VariantDeclaration();
-        vDecl1.setTag(fName);
-        vDecl2.setTag(fName);
-        vDecl3.setTag(fName);
-
-        vDef1 = new VariantDefinition(vDecl2, TestParams.createTrace(), fName);
-        vDef2 = new VariantDefinition(vDecl3, TestParams.createTrace(), fName);
-
-        sDef1 = new StructDefinition(new StructDeclaration(1L), vDef1, fName);
-        sDef2 = new StructDefinition(new StructDeclaration(1L), vDef2, fName);
-
-        eDef = new EnumDefinition(new EnumDeclaration(new IntegerDeclaration(1,
-                true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII)), sDef2, fName);
-
-        fixture = new VariantDefinition(vDecl1, sDef1, fName);
-        fixture.setTagDefinition(eDef);
-        fixture.setCurrentField(fName);
-    }
-
-    /**
-     * Perform post-test clean-up.
-     */
-    @After
-    public void tearDown() {
-        // Add additional tear down code here
+    public void setUp() throws CTFReaderException {
+        StructDeclaration sDec = new StructDeclaration(12);
+        StructDeclaration smallStruct = new StructDeclaration(8);
+        IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
+        IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
+        StringDeclaration strDec = new StringDeclaration();
+        EnumDeclaration enDec = new EnumDeclaration(iDec);
+        VariantDeclaration varDec = new VariantDeclaration();
+        EnumDeclaration tagDec = new EnumDeclaration(iDec);
+        CompoundDeclaration arrDec = new ArrayDeclaration(2, iDec);
+        FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
+        tagDec.add(0, 1, ENUM_1);
+        tagDec.add(2, 3, ENUM_2);
+        tagDec.add(4, 5, ENUM_3);
+        tagDec.add(8, 9, ENUM_5);
+        tagDec.add(10, 11, ENUM_6);
+        tagDec.add(12, 13, ENUM_7);
+        varDec.addField(ENUM_4, lenDec);
+        varDec.addField(ENUM_7, fDec);
+        varDec.addField(ENUM_6, smallStruct);
+        varDec.addField(ENUM_5, enDec);
+        varDec.addField(ENUM_3, arrDec);
+        varDec.addField(ENUM_2, iDec);
+        varDec.addField(ENUM_1, strDec);
+
+        sDec.addField(TAG_ID, tagDec);
+        sDec.addField(LENGTH_SEQ, lenDec);
+
+        sDec.addField(VAR_FIELD_NAME, varDec);
+        varDec.setTag(TAG_ID);
+
+        final ByteBuffer byteBuffer = Util.testMemory(ByteBuffer.allocate(100));
+        BitBuffer bb = new BitBuffer(byteBuffer);
+        byteBuffer.mark();
+        byteBuffer.putInt(1);
+        byteBuffer.putInt(2);
+        byteBuffer.putInt(3);
+        byteBuffer.reset();
+        fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
+        fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
     }
 
     /**
      * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
+     *
+     * @throws CTFReaderException
+     *             should not happen
      */
     @Test
-    public void testVariantDefinition() {
+    public void testVariantDefinition() throws CTFReaderException {
         VariantDeclaration declaration = new VariantDeclaration();
-        declaration.setTag(""); //$NON-NLS-1$
+        declaration.setTag("");
         VariantDeclaration variantDeclaration = new VariantDeclaration();
-        variantDeclaration.setTag(""); //$NON-NLS-1$
-        VariantDefinition variantDefinition = new VariantDefinition(
-                variantDeclaration, TestParams.createTrace(), ""); //$NON-NLS-1$
+        variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
+        variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
+        declaration.addField(ENUM_3, new StringDeclaration());
+        variantDeclaration.setTag("a");
+
+        byte[] bytes = new byte[128];
+        ByteBuffer byb = ByteBuffer.wrap(bytes);
+        byb.mark();
+        byb.putInt(0);
+        byb.putShort((short) 2);
+        byb.put(new String("hello").getBytes());
+        byb.reset();
+        BitBuffer bb = new BitBuffer(byb);
+        VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
+        EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
+        declaration2.add(0, 2, ENUM_3);
+        EnumDefinition enumDefinition = new EnumDefinition(
+                declaration2,
+                null,
+                "a",
+                new IntegerDefinition(
+                        IntegerDeclaration.INT_8_DECL,
+                        null,
+                        "A",
+                        1
+                ));
         IDefinitionScope definitionScope = new StructDefinition(
-                new StructDeclaration(1L), variantDefinition, ""); //$NON-NLS-1$
-        String fieldName = ""; //$NON-NLS-1$
-
-        VariantDefinition result = new VariantDefinition(declaration,
-                definitionScope, fieldName);
+                new StructDeclaration(1L),
+                variantDefinition,
+                "",
+                ImmutableList.<String> of("", "variant"),
+                new Definition[] { enumDefinition, variantDefinition }
+                );
+        String fieldName = "";
+        declaration.setTag("");
+        VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb);
         assertNotNull(result);
     }
 
@@ -113,8 +176,8 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetCurrentField() {
-        Definition result = fixture.getCurrentField();
-        assertNull(result);
+        IDefinition result = fixture.getCurrentField();
+        assertNotNull(result);
     }
 
     /**
@@ -140,7 +203,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetDefinitions() {
-        HashMap<String, Definition> result = fixture.getDefinitions();
+        IDefinition result = fixture.getCurrentField();
         assertNotNull(result);
     }
 
@@ -149,16 +212,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetPath() {
-        String result = fixture.getPath();
-        assertNotNull(result);
-    }
-
-    /**
-     * Run the EnumDefinition getTagDefinition() method test.
-     */
-    @Test
-    public void testGetTagDefinition() {
-        EnumDefinition result = fixture.getTagDefinition();
+        String result = fixture.getScopePath().toString();
         assertNotNull(result);
     }
 
@@ -167,8 +221,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupArray() {
-        String name = ""; //$NON-NLS-1$
-        ArrayDefinition result = fixture.lookupArray(name);
+        AbstractArrayDefinition result = fixture.lookupArrayDefinition(ENUM_3);
         assertNull(result);
     }
 
@@ -177,9 +230,9 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupDefinition() {
-        String lookupPath = ""; //$NON-NLS-1$
-        Definition result = fixture.lookupDefinition(lookupPath);
-        assertNull(result);
+        IDefinition result = fixture.lookupDefinition(ENUM_1);
+        assertNotNull(result);
+        assertEquals("a", ((EnumDefinition) result).getStringValue());
     }
 
     /**
@@ -187,8 +240,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupEnum() {
-        String name = ""; //$NON-NLS-1$
-        EnumDefinition result = fixture.lookupEnum(name);
+        EnumDefinition result = fixture.lookupEnum(ENUM_5);
         assertNull(result);
     }
 
@@ -197,18 +249,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupInteger() {
-        String name = ""; //$NON-NLS-1$
-        IntegerDefinition result = fixture.lookupInteger(name);
-        assertNull(result);
-    }
-
-    /**
-     * Run the SequenceDefinition lookupSequence(String) method test.
-     */
-    @Test
-    public void testLookupSequence_1() throws Exception {
-        String name = ""; //$NON-NLS-1$
-        SequenceDefinition result = fixture.lookupSequence(name);
+        IntegerDefinition result = fixture.lookupInteger(ENUM_2);
         assertNull(result);
     }
 
@@ -217,8 +258,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupString() {
-        String name = ""; //$NON-NLS-1$
-        StringDefinition result = fixture.lookupString(name);
+        StringDefinition result = fixture.lookupString(ENUM_1);
         assertNull(result);
     }
 
@@ -227,8 +267,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupStruct() {
-        String name = ""; //$NON-NLS-1$
-        StructDefinition result = fixture.lookupStruct(name);
+        StructDefinition result = fixture.lookupStruct(ENUM_6);
         assertNull(result);
     }
 
@@ -237,57 +276,16 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testLookupVariant() {
-        String name = ""; //$NON-NLS-1$
-        VariantDefinition result = fixture.lookupVariant(name);
+        VariantDefinition result = fixture.lookupVariant(ENUM_8);
         assertNull(result);
     }
 
     /**
-     * Run the void setCurrentField(String) method test.
-     */
-    @Test
-    public void testSetCurrentField() {
-        String currentField = ""; //$NON-NLS-1$
-        fixture.setCurrentField(currentField);
-    }
-
-    /**
-     * Run the void setDeclaration(VariantDeclaration) method test.
-     */
-    @Test
-    public void testSetDeclaration() {
-        VariantDeclaration declaration = new VariantDeclaration();
-        fixture.setDeclaration(declaration);
-    }
-
-    /**
-     * Run the void setDefinitions(HashMap<String,Definition>) method test.
-     */
-    @Test
-    public void testSetDefinitions() {
-        HashMap<String, Definition> definitions = new HashMap<String, Definition>();
-        fixture.setDefinitions(definitions);
-    }
-
-    /**
-     * Run the void setTagDefinition(EnumDefinition) method test.
+     * Run the String toString() method test.
      */
     @Test
-    public void testSetTagDefinition() {
-        VariantDeclaration vDecl;
-        VariantDefinition vDef;
-        StructDefinition structDef;
-        EnumDefinition tagDefinition;
-        String fName = ""; //$NON-NLS-1$
-
-        vDecl = new VariantDeclaration();
-        vDecl.setTag(fName);
-        vDef = new VariantDefinition(vDecl, TestParams.createTrace(), fName);
-        structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName);
-        tagDefinition = new EnumDefinition(new EnumDeclaration(
-                new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN,
-                        Encoding.ASCII)), structDef, fName);
-
-        fixture.setTagDefinition(tagDefinition);
+    public void testToString() {
+        String result = fixture.toString();
+        assertEquals("{ a = \"\" }", result);
     }
 }
This page took 0.02823 seconds and 5 git commands to generate.