ctf: Fix API inconsistencies
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / ArrayDefinitionTest.java
index adb35a2d416bf7c4fb1157c66edc4ac234f57b0f..b2df5813a5109632f46593c0ab7df0ad36039e86 100644 (file)
@@ -7,22 +7,18 @@ import static org.junit.Assert.assertTrue;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
-import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 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.IDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 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.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
-import org.eclipse.linuxtools.ctf.core.tests.TestParams;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
-import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -37,7 +33,9 @@ import org.junit.Test;
 public class ArrayDefinitionTest {
 
     private CTFTrace trace;
-    private ArrayDefinition fixture;
+    private ArrayDefinition charArrayFixture;
+    private ArrayDefinition stringArrayFixture;
+    private ArrayDefinition longArrayFixture;
 
     /**
      * Launch the test.
@@ -54,34 +52,66 @@ public class ArrayDefinitionTest {
      *
      * structDef shouldn't be null after parsing the CTFTraceReader object, so
      * we can ignore the warning.
-     * 
-     * @throws CTFReaderException 
      */
     @Before
-    public void setUp() throws CTFReaderException {
-        this.trace = TestParams.createTrace();
-
-        CTFTraceReader tr = new CTFTraceReader(this.trace);
-        String name = ""; //$NON-NLS-1$
-        StructDefinition structDef = null;
-        boolean foundArray = false;
-
-        while (tr.hasMoreEvents() && !foundArray) {
-            tr.advance();
-            EventDefinition ed = tr.getCurrentEventDef();
-            for (String key : ed.fields.getDefinitions().keySet()) {
-                structDef = ed.fields;
-                Definition d = structDef.lookupDefinition(key);
-                if (d instanceof ArrayDefinition) {
-                    foundArray = true;
-                    name = key;
-                    break;
-                }
-            }
-        }
-        fixture = structDef.lookupArray(name);
+    public void setUp() {
+        charArrayFixture = createCharArray();
+        stringArrayFixture = createStringArray();
+        longArrayFixture = createLongArray();
+    }
+
+    private ArrayDefinition createLongArray() {
+        IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8); //$NON-NLS-1$
+        IntegerDefinition[] defs = createIntDefs(10, 32);
+        ArrayDefinition temp = setUpDeclaration(decl, defs);
+        return temp;
+    }
+
+    private ArrayDefinition createCharArray() {
+        IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8); //$NON-NLS-1$
+        IntegerDefinition[] defs = createIntDefs(4,8);
+        ArrayDefinition temp = setUpDeclaration(decl, defs);
+        return temp;
     }
 
+
+    /**
+     * @return
+     */
+    private ArrayDefinition createStringArray() {
+        StringDeclaration strDecl = new StringDeclaration();
+        StringDefinition[] defs = createDefs();
+        ArrayDefinition temp = setUpDeclaration(strDecl, defs);
+        return temp;
+    }
+    /**
+     * @param decl
+     * @param defs
+     * @return
+     */
+    private ArrayDefinition setUpDeclaration(IDeclaration decl,
+            Definition[] defs) {
+        ArrayDeclaration ad = new ArrayDeclaration(0, decl);
+        ArrayDefinition temp = new ArrayDefinition(ad , this.trace , "Testx"); //$NON-NLS-1$
+        temp.setDefinitions(defs);
+        return temp;
+    }
+    /**
+     * @param size
+     * @param bits
+     * @return
+     */
+    private static IntegerDefinition[] createIntDefs(int size, int bits) {
+        IntegerDefinition[] defs = new IntegerDefinition[size];
+        for (int i = 0; i < size; i++) {
+
+            String content = "test" + i; //$NON-NLS-1$
+            defs[i] = new IntegerDefinition(new IntegerDeclaration(bits, false,
+                    16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content);
+            defs[i].setValue(i);
+        }
+        return defs;
+    }
     /**
      * Perform post-test clean-up.
      */
@@ -109,7 +139,7 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testArrayDefinition_baseDeclaration() {
-        ArrayDeclaration declaration = fixture.getDeclaration();
+        ArrayDeclaration declaration = charArrayFixture.getDeclaration();
         String fieldName = ""; //$NON-NLS-1$
 
         ArrayDefinition result = new ArrayDefinition(declaration, this.trace,
@@ -140,8 +170,8 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testGetDeclaration() {
-        fixture.setDefinitions(new Definition[] {});
-        ArrayDeclaration result = fixture.getDeclaration();
+        charArrayFixture.setDefinitions(new Definition[] {});
+        ArrayDeclaration result = charArrayFixture.getDeclaration();
 
         assertNotNull(result);
     }
@@ -152,7 +182,7 @@ public class ArrayDefinitionTest {
     @Test
     public void testGetElem_noDefs() {
         int i = 0;
-        Definition result = fixture.getElem(i);
+        Definition result = charArrayFixture.getElem(i);
 
         assertNotNull(result);
     }
@@ -163,10 +193,10 @@ public class ArrayDefinitionTest {
     @Test
     public void testGetElem_withDefs() {
         Definition defs[] = createDefs();
-        fixture.setDefinitions(defs);
+        charArrayFixture.setDefinitions(defs);
         int j = 1;
 
-        Definition result = fixture.getElem(j);
+        Definition result = charArrayFixture.getElem(j);
 
         assertNotNull(result);
     }
@@ -176,34 +206,28 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testIsString_ownDefs() {
-        StringDefinition[] defs = createDefs();
-        fixture.setDefinitions(defs);
 
-        boolean result = fixture.isString();
+        boolean result = stringArrayFixture.isString();
 
         assertFalse(result);
     }
 
+
+
     /**
      * Run the boolean isString() method test.
      */
     @Test
     public void testIsString_complex() {
         final IntegerDeclaration id = new IntegerDeclaration(8, false, 16,
-                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null);
+                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8);
         ArrayDeclaration ad = new ArrayDeclaration(0, id);
         ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace,
                 "Testx"); //$NON-NLS-1$
 
         int size = 4;
-        IntegerDefinition[] defs = new IntegerDefinition[size];
-        for (int i = 0; i < size; i++) {
-
-            String content = "test" + i; //$NON-NLS-1$
-            defs[i] = new IntegerDefinition(new IntegerDeclaration(8, false,
-                    16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content), null, content);
-            defs[i].setValue(i);
-        }
+        int bits = 8;
+        IntegerDefinition[] defs = createIntDefs(size, bits);
 
         ownFixture.setDefinitions(defs);
         boolean result = ownFixture.isString();
@@ -211,17 +235,28 @@ public class ArrayDefinitionTest {
         assertTrue(result);
     }
 
+
+
     /**
      * Run the boolean isString() method test.
      */
     @Test
     public void testIsString_emptyDef() {
-        fixture.setDefinitions(new Definition[] {});
-        boolean result = fixture.isString();
+        charArrayFixture.setDefinitions(new Definition[] {});
+        boolean result = charArrayFixture.isString();
 
-        assertFalse(result);
+        assertTrue(result);
     }
 
+    /**
+     * Run the boolean isString() method test.
+     */
+    @Test
+    public void testIsString_emptyDefStrDecl() {
+        ArrayDefinition ownFixture = createStringArray();
+        boolean result = ownFixture.isString();
+        assertFalse(result);
+    }
     /**
      * Run the void read(BitBuffer) method test.
      */
@@ -229,7 +264,7 @@ public class ArrayDefinitionTest {
     public void testRead_noDefs() {
         BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
 
-        fixture.read(input);
+        charArrayFixture.read(input);
     }
 
     /**
@@ -237,34 +272,52 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testRead_withDefs() {
-        fixture.setDefinitions(new Definition[] {});
+        charArrayFixture.setDefinitions(new Definition[] {});
         BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
 
-        fixture.read(input);
+        charArrayFixture.read(input);
     }
 
     /**
      * Run the String toString() method test.
      */
     @Test
-    public void testToString_base() {
-        String result = fixture.toString();
-
+    public void testToString_char() {
+        String result = charArrayFixture.toString();
+        assertNotNull(result);
+    }
+    /**
+     * Run the String toString() method test.
+     */
+    @Test
+    public void testToString_long() {
+        String result = longArrayFixture.toString();
         assertNotNull(result);
     }
 
+    /**
+     * Run the String toString() method test.
+     */
+    @Test
+    public void testToString_string() {
+        String result = stringArrayFixture.toString();
+        assertNotNull(result);
+    }
     /**
      * Run the String toString() method test.
      */
     @Test
     public void testToString_withDefs() {
-        int size = 2;
-        StringDefinition[] defs = new StringDefinition[size];
-        for (int i = 0; i < size; i++) {
-            defs[i] = new StringDefinition(null, null, ("test" + i)); //$NON-NLS-1$
-        }
-        fixture.setDefinitions(defs);
-        String result = fixture.toString();
+        String result = charArrayFixture.toString();
+
+        assertNotNull(result);
+    }
+    /**
+     * Run the String toString() method test.
+     */
+    @Test
+    public void testToStringStringArray() {
+        String result = stringArrayFixture.toString();
 
         assertNotNull(result);
     }
This page took 0.115905 seconds and 5 git commands to generate.