import java.util.List;
import java.util.UUID;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.types.ISimpleDatatypeDeclaration;
import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
import org.junit.Before;
import org.junit.Test;
+import com.google.common.collect.Iterables;
+
/**
* The class <code>MetadataTest</code> contains tests for the class
* <code>{@link Metadata}</code>.
" };\n" +
" };";
+ private static final String ENDIAN_CHANGE_L_B =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = le; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+ private static final String ENDIAN_CHANGE_L_N =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = le; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+ private static final String ENDIAN_CHANGE_L_L =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = le; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+
+ private static final String ENDIAN_CHANGE_B_L =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = be; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+ private static final String ENDIAN_CHANGE_B_N =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = be; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+ private static final String ENDIAN_CHANGE_B_B =
+ "/* ctf 1.8 */"
+ + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
+ + "trace { byte_order = be; };"
+ + "event { "
+ + " name = \"bob\"; "
+ + " fields := struct field { INT data ; };"
+ + "};";
+
+
private Metadata fixture;
/**
assertNotNull(result);
}
+ /**
+ * Test a changing endian event field
+ *
+ * @throws CTFException
+ * won't happen
+ */
+ @Test
+ public void testEndian() throws CTFException {
+ testEndianess(ENDIAN_CHANGE_L_B, ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN);
+ testEndianess(ENDIAN_CHANGE_L_N, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+ testEndianess(ENDIAN_CHANGE_L_L, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+ testEndianess(ENDIAN_CHANGE_B_L, ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+ testEndianess(ENDIAN_CHANGE_B_N, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
+ testEndianess(ENDIAN_CHANGE_B_B, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
+ }
+
+ private void testEndianess(String tsdl, ByteOrder traceEndian, ByteOrder fieldEndian) throws CTFException {
+ fixture = new Metadata();
+ CTFTrace trace = fixture.getTrace();
+ fixture.parseText(tsdl);
+ assertEquals(traceEndian, trace.getByteOrder());
+ final Iterable<IEventDeclaration> eventDeclarations = trace.getEventDeclarations(0L);
+ assertNotNull(eventDeclarations);
+ IEventDeclaration event = Iterables.getFirst(eventDeclarations, null);
+ assertNotNull(event);
+ assertNotNull(event.getFields());
+ final @Nullable IDeclaration field = event.getFields().getField("data");
+ assertNotNull(field);
+ if (field instanceof ISimpleDatatypeDeclaration) {
+ ISimpleDatatypeDeclaration declaration = (ISimpleDatatypeDeclaration) field;
+ assertEquals(fieldEndian, declaration.getByteOrder());
+ } else {
+ fail("data is not the right type");
+ }
+ }
+
/**
* Run the void parse() method test.
*
private final int fMantissa;
private final int fExponent;
+ private final boolean fIsByteOrderSet;
private final ByteOrder fByteOrder;
private final long fAlignement;
long alignment) {
fMantissa = mantissa;
fExponent = exponent;
+ fIsByteOrderSet = byteOrder != null;
fByteOrder = (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder;
fAlignement = Math.max(alignment, 1);
// ------------------------------------------------------------------------
/**
- * @return the mant
+ * @return the mantissa
*/
public int getMantissa() {
return fMantissa;
}
/**
- * @return the exp
+ * @return the exponent
*/
public int getExponent() {
return fExponent;
}
/**
- * @return the byteOrder
+ * @since 2.0
*/
+ @Override
+ public boolean isByteOrderSet() {
+ return fIsByteOrderSet;
+ }
+
+ @Override
public ByteOrder getByteOrder() {
return fByteOrder;
}
final int prime = 31;
int result = 1;
result = prime * result + (int) (fAlignement ^ (fAlignement >>> 32));
- result = prime * result + fByteOrder.toString().hashCode(); // don't evaluate object but string
+ result = prime * result + fByteOrder.toString().hashCode(); // don't
+ // evaluate
+ // object
+ // but
+ // string
result = prime * result + fExponent;
result = prime * result + fMantissa;
return result;
final DeclarationScope parentScope, String name,
IntegerDeclaration decl) throws ParseException {
- if (decl.getByteOrder() != byteOrder) {
+ if (!decl.isByteOrderSet()) {
IntegerDeclaration newI;
newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(),
decl.getBase(), byteOrder, decl.getEncoding(),
private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, EnumDeclaration decl) throws ParseException {
final IntegerDeclaration containerType = decl.getContainerType();
- if (containerType.getByteOrder() != byteOrder) {
+ if (!decl.isByteOrderSet()) {
EnumDeclaration newEnum = new EnumDeclaration(IntegerDeclaration.createDeclaration(containerType.getLength(), containerType.isSigned(),
containerType.getBase(), byteOrder, containerType.getEncoding(),
containerType.getClock(), containerType.getAlignment()));
}
private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, FloatDeclaration decl) throws ParseException {
- if (decl.getByteOrder() != byteOrder) {
+ if (!decl.isByteOrderSet()) {
FloatDeclaration newFloat = new FloatDeclaration(decl.getExponent(), decl.getMantissa(), byteOrder, decl.getAlignment());
parentScope.replaceType(name, newFloat);
}