improve test coverage and fix go to last event bug
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 12 Apr 2012 21:57:16 +0000 (17:57 -0400)
committerFrancois Chouinard <fchouinard@gmail.com>
Mon, 16 Apr 2012 18:20:41 +0000 (14:20 -0400)
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceReaderTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputPacketIndexEntryTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTraceReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java

index 3177f5489ff4445b05d6b8d65bc113233b9a529d..1e7f7b6b490a12fe3190279cdd40f03dc78ee2e1 100644 (file)
@@ -201,7 +201,8 @@ public class CTFTraceReaderTest {
         fixture.goToLastEvent();
         long ts1 = getTimestamp();
         long ts2 = fixture.getEndTime();
-        assertTrue(ts1 == ts2);
+        // the end time can be later than the last event.
+        assertTrue(ts1 <= ts2);
     }
 
     /**
index 260b17b569219b92d214d6071065637c264a62d0..39537d09a75a2460654e453803426a477c7609cd 100644 (file)
@@ -99,7 +99,7 @@ public class CTFTraceTest {
      * Run the void addStream(Stream) method test.
      *
      * @throws ParseException
-     * @throws CTFReaderException 
+     * @throws CTFReaderException
      */
     @Test
     public void testAddStream() throws ParseException, CTFReaderException {
@@ -313,7 +313,7 @@ public class CTFTraceTest {
     @Test
     public void testGetClock_1() {
         CTFClock result = fixture.getClock();
-        assertNull(result);
+        assertNotNull(result);
     }
 
     /**
index 0b558ae3a093b21f720c28d6e883822abaa231e7..52493974c11f37e6983afb1fd099f3c129925860 100644 (file)
@@ -50,9 +50,9 @@ public class StreamInputPacketIndexEntryTest {
      */
     @Test
     public void testStreamInputPacketIndexEntry_1() {
-        String expectedResult = "PacketIndexEntry [offset=1, " + //$NON-NLS-1$
+        String expectedResult = "StreamInputPacketIndexEntry [offsetBytes=1, " + //$NON-NLS-1$
                 "timestampBegin=0, timestampEnd=0, " + //$NON-NLS-1$
-                "dataOffset=0, packetSize=0, contentSize=0]"; //$NON-NLS-1$
+                "indexBegin=9223372036854775807, indexEnd=9223372036854775807]"; //$NON-NLS-1$
 
         assertNotNull(fixture);
         assertEquals(expectedResult, fixture.toString());
@@ -63,9 +63,9 @@ public class StreamInputPacketIndexEntryTest {
      */
     @Test
     public void testToString() {
-        String expectedResult = "PacketIndexEntry [offset=1, " + //$NON-NLS-1$
-                "timestampBegin=1, timestampEnd=1, " + //$NON-NLS-1$
-                "dataOffset=1, packetSize=1, contentSize=1]"; //$NON-NLS-1$
+        String expectedResult = "StreamInputPacketIndexEntry [offsetBytes=1,"+ //$NON-NLS-1$
+                " timestampBegin=1, timestampEnd=1, indexBegin=9223372036854775807,"+ //$NON-NLS-1$
+                " indexEnd=9223372036854775807]"; //$NON-NLS-1$
 
         fixture.setContentSizeBits(1);
         fixture.setDataOffsetBits(1);
index d995e26992da2572778758481f872342ca1097bd..3bdf3f9c08d746929b643117efcde35aa6100368 100644 (file)
@@ -7,21 +7,19 @@ 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.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.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
 import org.junit.After;
 import org.junit.Before;
@@ -37,7 +35,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 +54,70 @@ public class ArrayDefinitionTest {
      *
      * structDef shouldn't be null after parsing the CTFTraceReader object, so
      * we can ignore the warning.
-     * 
-     * @throws CTFReaderException 
+     *
+     * @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);
+        charArrayFixture = createCharArray();
+        stringArrayFixture = createStringArray();
+        longArrayFixture = createLongArray();
     }
 
+    private ArrayDefinition createLongArray() {
+        IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none"); //$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"); //$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 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), null, content);
+            defs[i].setValue(i);
+        }
+        return defs;
+    }
     /**
      * Perform post-test clean-up.
      */
@@ -109,7 +145,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 +176,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 +188,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 +199,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,14 +212,14 @@ 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.
      */
@@ -196,14 +232,8 @@ public class ArrayDefinitionTest {
                 "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 +241,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 +270,7 @@ public class ArrayDefinitionTest {
     public void testRead_noDefs() {
         BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
 
-        fixture.read(input);
+        charArrayFixture.read(input);
     }
 
     /**
@@ -237,34 +278,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);
     }
index 5e312b6fd06b92e75405c6d08d8ad00936ee9380..56c17d53fdbf3f04e317c5a044708e8167ad4977 100644 (file)
@@ -28,6 +28,7 @@ public class SequenceDeclarationTest {
 
     private SequenceDeclaration fixture;
 
+    static final String fieldName = "LengthName"; //$NON-NLS-1$
     /**
      * Launch the test.
      *
@@ -40,7 +41,7 @@ public class SequenceDeclarationTest {
 
     @Before
     public void setUp() {
-        fixture = new SequenceDeclaration("", new StringDeclaration()); //$NON-NLS-1$
+        fixture = new SequenceDeclaration(fieldName, new StringDeclaration());
     }
 
     @After
@@ -71,7 +72,7 @@ public class SequenceDeclarationTest {
     public void testCreateDefinition() {
         IntegerDeclaration id = new IntegerDeclaration(8, false, 8,
                 ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null);
-        String fieldName = "LengthName"; //$NON-NLS-1$
+
         StructDeclaration structDec = new StructDeclaration(0);
         structDec.addField(fieldName, id);
         StructDefinition structDef = new StructDefinition(structDec, null, "x"); //$NON-NLS-1$
index 136581c120718ad60e8bc4129818dbf9e4a8efdc..2ab82a028558e89732d83bc2b72c8925c93b7949 100644 (file)
@@ -21,7 +21,6 @@ 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.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -29,7 +28,7 @@ import org.junit.Test;
 /**
  * The class <code>StructDefinitionTest</code> contains tests for the class
  * <code>{@link StructDefinition}</code>.
- * 
+ *
  * @author ematkho
  * @version $Revision: 1.0 $
  */
@@ -41,7 +40,7 @@ public class StructDefinitionTest {
 
     /**
      * Launch the test.
-     * 
+     *
      * @param args
      *            the command line arguments
      */
@@ -51,8 +50,8 @@ public class StructDefinitionTest {
 
     /**
      * Perform pre-test initialization.
-     * 
-     * @throws CTFReaderException 
+     *
+     * @throws CTFReaderException
      */
     @Before
     public void setUp() throws CTFReaderException {
@@ -104,7 +103,7 @@ public class StructDefinitionTest {
      */
     @Test
     public void testLookupDefinition() {
-        String lookupPath = "id"; //$NON-NLS-1$
+        String lookupPath = "args"; //$NON-NLS-1$
         Definition result = fixture.lookupDefinition(lookupPath);
 
         assertNotNull(result);
@@ -127,7 +126,7 @@ public class StructDefinitionTest {
      */
     @Test
     public void testLookupInteger_1() {
-        String name = "id"; //$NON-NLS-1$
+        String name = "_args"; //$NON-NLS-1$
         IntegerDefinition result = fixture.lookupInteger(name);
 
         assertNotNull(result);
index 29d946e83e6b912a80adc5ae373fc3757b8e08db..1abe32784e259b49e0ce5c1ff90605122a7898f0 100644 (file)
@@ -37,17 +37,17 @@ public class SequenceDefinition extends Definition {
             IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
         super(definitionScope, fieldName);
         Definition lenDef = null;
-        
+
         this.declaration = declaration;
 
         if (definitionScope != null) {
             lenDef = definitionScope.lookupDefinition(declaration.getLengthName());
         }
 
-         if (lenDef == null) { 
+         if (lenDef == null) {
              throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
          }
-        
+
          if (!(lenDef instanceof IntegerDefinition)) {
              throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
          }
index a4fccaa53d0e379b277729f59c66f98f9d1a8fe2..fda56132c2e308aa48548cdc8e24b0900b362cb1 100644 (file)
@@ -289,7 +289,6 @@ public class CTFTraceReader {
      */
     public void goToLastEvent() throws CTFReaderException {
 
-        this.seek(0);
         for (StreamInputReader streamInputReader : this.streamInputReaders) {
             /*
              * Seek the trace reader.
@@ -297,7 +296,7 @@ public class CTFTraceReader {
             streamInputReader.goToLastEvent();
         }
         int count = prio.size();
-        for (int i = 0; i < (count); i++) {
+        for (int i = 0; i < (count-1); i++) {
             advance();
         }
     }
index b33dd77e2db68a38b9394e66da449f5658020f10..7c4bf08676dcc0ff9d676d68bb40c3c0213f424a 100644 (file)
@@ -46,7 +46,6 @@ public class StreamInputReader {
      */
     private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
 
-
     /**
      * Reference to the current event of this trace file (iow, the last on that
      * was read, the next one to be returned)
@@ -171,7 +170,7 @@ public class StreamInputReader {
      *            The timestamp to seek to.
      */
     public long seek(long timestamp) {
-        long offset = 0 ;
+        long offset = 0;
         /*
          * Search in the index for the packet to search in.
          */
@@ -192,14 +191,12 @@ public class StreamInputReader {
         while (!done && (this.getCurrentEvent().timestamp < timestamp)) {
             readNextEvent();
             done = (this.getCurrentEvent() == null);
-            offset++ ;
+            offset++;
         }
         return offset;
     }
 
-
-    public long seekIndex(long index) throws CTFReaderException
-    {
+    public long seekIndex(long index) throws CTFReaderException {
         /*
          * Search in the index for the packet to search in.
          */
@@ -215,8 +212,9 @@ public class StreamInputReader {
         /*
          * get the current index
          */
-        if(this.packetReader.getCurrentPacket() == null){
-            throw new CTFReaderException("Current packet null in index seek, did you index your trace yet?");
+        if (this.packetReader.getCurrentPacket() == null) {
+            throw new CTFReaderException(
+                    "Current packet null in index seek, did you index your trace yet?");
         }
         return this.packetReader.getCurrentPacket().getIndexBegin();
 
@@ -227,13 +225,28 @@ public class StreamInputReader {
          * Search in the index for the packet to search in.
          */
         int len = this.streamInput.getIndex().getEntries().size();
-        StreamInputPacketIndexEntry entry = this.streamInput.getIndex().getEntries().get(len-1);
-        seek(entry.getTimestampEnd() - 1 );
+        int back = 0;
+        long desired_timestamp = -1;
+        do {
+            back++;
+            StreamInputPacketIndexEntry entry = this.streamInput.getIndex()
+                    .getEntries().get(len - back);
+            desired_timestamp = entry.getTimestampBegin() + 1;
+            seek(desired_timestamp);
+
+        } while (!this.packetReader.hasMoreEvents());
         /*
          * Go until the end of that packet
          */
+
+        int packet_size = 0;
         while (this.packetReader.hasMoreEvents()) {
             this.packetReader.readNextEvent();
+            packet_size++;
+        }
+        seek(desired_timestamp);
+        for (int i = 0; i < (packet_size - 1); i++) {
+            this.packetReader.readNextEvent();
         }
     }
 
@@ -255,6 +268,4 @@ public class StreamInputReader {
         return packetReader;
     }
 
-
-
 }
This page took 0.034784 seconds and 5 git commands to generate.