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;
public class ArrayDefinitionTest {
private CTFTrace trace;
- private ArrayDefinition fixture;
+ private ArrayDefinition charArrayFixture;
+ private ArrayDefinition stringArrayFixture;
+ private ArrayDefinition longArrayFixture;
/**
* Launch the test.
*
* 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.
*/
*/
@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,
*/
@Test
public void testGetDeclaration() {
- fixture.setDefinitions(new Definition[] {});
- ArrayDeclaration result = fixture.getDeclaration();
+ charArrayFixture.setDefinitions(new Definition[] {});
+ ArrayDeclaration result = charArrayFixture.getDeclaration();
assertNotNull(result);
}
@Test
public void testGetElem_noDefs() {
int i = 0;
- Definition result = fixture.getElem(i);
+ Definition result = charArrayFixture.getElem(i);
assertNotNull(result);
}
@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);
}
*/
@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.
*/
"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();
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.
*/
public void testRead_noDefs() {
BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
- fixture.read(input);
+ charArrayFixture.read(input);
}
/**
*/
@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);
}
*/
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)
* 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.
*/
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.
*/
/*
* 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();
* 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();
}
}
return packetReader;
}
-
-
}