java_gen/U64: fix bug with treatment of usigned long values > Long.MAX_VALUE
diff --git a/java_gen/pre-written/src/main/java/org/openflow/types/U64.java b/java_gen/pre-written/src/main/java/org/openflow/types/U64.java
index 211c3ea..c0cd475 100644
--- a/java_gen/pre-written/src/main/java/org/openflow/types/U64.java
+++ b/java_gen/pre-written/src/main/java/org/openflow/types/U64.java
@@ -20,8 +20,7 @@
import java.math.BigInteger;
public class U64 {
- private final static BigInteger TWO_POWER_64 = BigInteger.valueOf(Long.MAX_VALUE).add(
- BigInteger.valueOf(1));
+ private static final long UNSIGNED_MASK = 0x7fffffffffffffffL;
private final long raw;
@@ -38,8 +37,11 @@
}
public BigInteger getBigInteger() {
- return raw >= 0 ? BigInteger.valueOf(raw) : TWO_POWER_64.add(BigInteger
- .valueOf(raw));
+ BigInteger bigInt = BigInteger.valueOf(raw & UNSIGNED_MASK);
+ if (raw < 0) {
+ bigInt = bigInt.setBit(Long.SIZE - 1);
+ }
+ return bigInt;
}
@Override
@@ -47,8 +49,12 @@
return getBigInteger().toString();
}
- public static BigInteger f(final long i) {
- return new BigInteger(Long.toBinaryString(i), 2);
+ public static BigInteger f(final long value) {
+ BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
+ if (value < 0) {
+ bigInt = bigInt.setBit(Long.SIZE - 1);
+ }
+ return bigInt;
}
public static long t(final BigInteger l) {
diff --git a/java_gen/pre-written/src/test/java/org/openflow/types/U64Test.java b/java_gen/pre-written/src/test/java/org/openflow/types/U64Test.java
new file mode 100644
index 0000000..16d3d3e
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/openflow/types/U64Test.java
@@ -0,0 +1,26 @@
+package org.openflow.types;
+
+import static org.junit.Assert.assertEquals;
+
+import java.math.BigInteger;
+
+import org.junit.Test;
+
+public class U64Test {
+
+ @Test
+ public void testPositiveRaws() {
+ for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
+ assertEquals(positive, U64.of(positive).getValue());
+ assertEquals(BigInteger.valueOf(positive), U64.of(positive).getBigInteger());
+ }
+ }
+
+ @Test
+ public void testNegativeRaws() {
+ long minus_1 = 0xFFffFFffFFffFFffL;
+ assertEquals(minus_1, U64.of(minus_1).getValue());
+ assertEquals(new BigInteger("FFffFFffFFffFFff", 16), U64.of(minus_1).getBigInteger());
+ assertEquals(new BigInteger("18446744073709551615"), U64.of(minus_1).getBigInteger());
+ }
+}