aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Christian Kouamé2013-05-10 10:06:07 (EDT)
committerAlexandre Montplaisir2013-05-10 16:17:38 (EDT)
commit78789c96d2c5a246073a487363413afa6bdbf08b (patch)
tree1287791a6fa26d6aa3bcdbe7716f10d5956fec74
parent7fe20864630cb3058a62a4f18574dd361ae82ef7 (diff)
downloadorg.eclipse.linuxtools-78789c96d2c5a246073a487363413afa6bdbf08b.zip
org.eclipse.linuxtools-78789c96d2c5a246073a487363413afa6bdbf08b.tar.gz
org.eclipse.linuxtools-78789c96d2c5a246073a487363413afa6bdbf08b.tar.bz2
TMF : Bug 385217 - Display 64 unsigned integers correctlyrefs/changes/11/12711/4
Change-Id: I80709ff4c7404b8201ee144e0e98ecaa7438651b Signed-off-by: Jean-Christian Kouamé Reviewed-on: https://git.eclipse.org/r/12711 Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im> IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im> Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java45
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java74
2 files changed, 96 insertions, 23 deletions
diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
index b874071..d322e15 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
@@ -79,12 +79,12 @@ public class CtfTmfEventFieldTest {
sDec.addField(STR, strDec);
sDec.addField(ARRAY, arrDec);
sDec.addField(SEQ, seqDec);
- structDec.addField(STR,strDec);
+ structDec.addField(STR, strDec);
structDec.addField(INT, intDec);
sDec.addField(STRUCT, structDec);
enumDec.add(0, 1, LEN);
enumDec.add(2, 3, FLOAT);
- sDec.addField(ENUM, enumDec);
+ sDec.addField(ENUM, enumDec);
varDec.addField(LEN, intDec);
varDec.addField(FLOAT, flDec);
varDec.setTag(ENUM);
@@ -194,4 +194,45 @@ public class CtfTmfEventFieldTest {
CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
assertEquals("test=float=9.551467814359616E-38", result.toString());
}
+
+ /**
+ * Run the CtfTmfEventField formatNumber(Long, int, boolean) method test for
+ * unsigned values.
+ */
+ @Test
+ public void testFormatNumber_unsignedLong() {
+
+ long unsignedLongValue = -64;
+ String result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
+ // -64 + 2^64 = 18446744073709551552
+ assertEquals("18446744073709551552", result);
+
+ unsignedLongValue = -131940199973272L;
+ result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
+ // -131940199973272l + 2^64 = 18446612133509578344
+ assertEquals("18446612133509578344", result);
+
+ unsignedLongValue = 123456789L;
+ result = CtfTmfEventField.formatNumber(unsignedLongValue, 10, false);
+ assertEquals("123456789", result);
+ }
+
+ /**
+ * Run the CtfTmfEventField formatNumber(Long, int, boolean) method test for
+ * signed values.
+ */
+ @Test
+ public void testFormatNumber_signedLong() {
+ long signedValue = -64L;
+ String result = CtfTmfEventField.formatNumber(signedValue, 10, true);
+ assertEquals("-64", result);
+
+ signedValue = -131940199973272L;
+ result = CtfTmfEventField.formatNumber(signedValue, 10, true);
+ assertEquals("-131940199973272", result);
+
+ signedValue = 123456789L;
+ result = CtfTmfEventField.formatNumber(signedValue, 10, true);
+ assertEquals("123456789", result);
+ }
}
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
index 6ac34e3..3c4de5d 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
@@ -11,10 +11,12 @@
* Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
* Bernd Hufmann - Add Enum field handling
* Geneviève Bastien - Add Struct and Variant field handling
+ * Jean-Christian Kouame - Correct handling of unsigned integer fields
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.ctfadaptor;
+import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -89,7 +91,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
if (fieldDef instanceof IntegerDefinition) {
IntegerDefinition intDef = (IntegerDefinition) fieldDef;
int base = intDef.getDeclaration().getBase();
- field = new CTFIntegerField(fieldName, intDef.getValue(), base);
+ field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
} else if (fieldDef instanceof EnumDefinition) {
EnumDefinition enumDef = (EnumDefinition) fieldDef;
@@ -116,7 +118,8 @@ public abstract class CtfTmfEventField extends TmfEventField {
for (int i = 0; i < arrayDecl.getLength(); i++) {
values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue());
}
- field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase());
+ field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase(),
+ ((IntegerDeclaration) arrayDecl.getElementType()).isSigned());
}
/* Add other types of arrays here */
@@ -136,7 +139,8 @@ public abstract class CtfTmfEventField extends TmfEventField {
for (int i = 0; i < seqDef.getLength(); i++) {
values.add(((IntegerDefinition) seqDef.getElem(i)).getValue());
}
- field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase());
+ field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
+ ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
}
/* Add other Sequence types here */
@@ -170,7 +174,10 @@ public abstract class CtfTmfEventField extends TmfEventField {
}
} else {
- /* Safe-guard, to avoid null exceptions later, field is expected not to be null */
+ /*
+ * Safe-guard, to avoid null exceptions later, field is expected not
+ * to be null
+ */
field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
}
return field;
@@ -188,11 +195,14 @@ public abstract class CtfTmfEventField extends TmfEventField {
* The value to print as string
* @param base
* The base for this value
+ * @param signed
+ * Is the value signed or not
* @return formatted number string
* @since 2.0
*/
- protected final static String formatNumber(long value, int base) {
+ public final static String formatNumber(long value, int base, boolean signed) {
String s;
+
/* Format the number correctly according to the integer's base */
switch (base) {
case 2:
@@ -200,16 +210,29 @@ public abstract class CtfTmfEventField extends TmfEventField {
break;
case 8:
s = "0" + Long.toOctalString(value); //$NON-NLS-1$
- break;
- case 10:
- s = Long.toString(value);
+
break;
case 16:
s = "0x" + Long.toHexString(value); //$NON-NLS-1$
break;
+ case 10:
default:
- /* Non-standard base, we'll just print it as a decimal number */
- s = Long.toString(value);
+ /* For non-standard base, we'll just print it as a decimal number */
+ if (!signed && value < 0) {
+ /* Since there are no 'unsigned long', handle this case with BigInteger */
+ BigInteger bigInteger = BigInteger.valueOf(value);
+ /*
+ * powerOfTwo = 2^64 we add 2^64 to the negative number to get
+ * the real unsigned value
+ */
+ BigInteger powerOfTwo = (BigInteger.valueOf(Long.MAX_VALUE)).add(BigInteger.valueOf(1));
+ powerOfTwo = powerOfTwo.multiply(BigInteger.valueOf(2));
+ bigInteger = bigInteger.add(powerOfTwo);
+
+ s = bigInteger.toString();
+ } else {
+ s = Long.toString(value);
+ }
break;
}
return s;
@@ -225,18 +248,22 @@ public abstract class CtfTmfEventField extends TmfEventField {
final class CTFIntegerField extends CtfTmfEventField {
private final int base;
+ private final boolean signed;
/**
* A CTF "IntegerDefinition" can be an integer of any byte size, so in the
* Java parser this is interpreted as a long.
*
- * @param longValue
- * The integer value of this field
* @param name
* The name of this field
+ * @param longValue
+ * The integer value of this field
+ * @param signed
+ * Is the value signed or not
*/
- CTFIntegerField(String name, long longValue, int base) {
+ CTFIntegerField(String name, long longValue, int base, boolean signed) {
super(name, longValue, null);
+ this.signed = signed;
this.base = base;
}
@@ -247,7 +274,7 @@ final class CTFIntegerField extends CtfTmfEventField {
@Override
public String getFormattedValue() {
- return formatNumber(getValue(), base);
+ return formatNumber(getValue(), base, signed);
}
}
@@ -285,20 +312,24 @@ final class CTFStringField extends CtfTmfEventField {
final class CTFIntegerArrayField extends CtfTmfEventField {
private final int base;
+ private final boolean signed;
private String formattedValue = null;
/**
* Constructor for CTFIntegerArrayField.
*
+ * @param name
+ * The name of this field
* @param longValues
* The array of integers (as longs) that compose this field's
* value
- * @param name
- * The name of this field
+ * @param signed
+ * Are the values in the array signed or not
*/
- CTFIntegerArrayField(String name, List<Long> longValues, int base) {
+ CTFIntegerArrayField(String name, List<Long> longValues, int base, boolean signed) {
super(name, longValues, null);
this.base = base;
+ this.signed = signed;
}
@Override
@@ -311,7 +342,7 @@ final class CTFIntegerArrayField extends CtfTmfEventField {
if (formattedValue == null) {
List<String> strings = new ArrayList<String>();
for (Long value : getValue()) {
- strings.add(formatNumber(value, base));
+ strings.add(formatNumber(value, base, signed));
}
formattedValue = strings.toString();
}
@@ -356,13 +387,14 @@ final class CTFEnumField extends CtfTmfEventField {
* Constructor for CTFEnumField.
*
* @param enumValue
- * The Enum value consisting of a pair of Enum value name and its long value
+ * The Enum value consisting of a pair of Enum value name and its
+ * long value
* @param name
* The name of this field
*/
CTFEnumField(String name, CtfEnumPair enumValue) {
super(name, new CtfEnumPair(enumValue.getFirst(),
- enumValue.getSecond().longValue()), null);
+ enumValue.getSecond().longValue()), null);
}
@Override
@@ -418,7 +450,7 @@ final class CTFVariantField extends CtfTmfEventField {
* The name of this field
*/
CTFVariantField(String name, CtfTmfEventField field) {
- super(name, field, new CtfTmfEventField[]{ field });
+ super(name, field, new CtfTmfEventField[] { field });
}
@Override